You are here

function commerce_paypal_ec_capture_form_submit in Commerce PayPal 7.2

Submit handler: process a prior authorization capture via PayPal EC.

File

modules/ec/includes/commerce_paypal_ec.admin.inc, line 93
Administrative forms for the Paypal EC module.

Code

function commerce_paypal_ec_capture_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $order = $form_state['order'];
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_total = $order_wrapper->commerce_order_total
    ->value();
  $order_total['amount'] = commerce_currency_convert($order_total['amount'], $order_total['currency_code'], $transaction->currency_code);

  // Convert the amout to the commerce format.
  $amount = commerce_currency_decimal_to_amount($form_state['values']['amount'], $transaction->currency_code);

  // Build a name-value pair array for this transaction.
  $nvp = array(
    'METHOD' => 'DoCapture',
    'AUTHORIZATIONID' => $transaction->remote_id,
    'AMT' => commerce_paypal_price_amount($amount, $transaction->currency_code),
    'CURRENCYCODE' => $transaction->currency_code,
    'COMPLETETYPE' => $order_total['amount'] != $amount ? 'NotComplete' : 'Complete',
    'INVNUM' => commerce_paypal_ipn_invoice($order),
  );

  // Submit the capture request to Paypal.
  $response = commerce_paypal_api_request($form_state['payment_method'], $nvp, $order);
  $transaction->payload[REQUEST_TIME . '-capture'] = $response;
  switch ($response['ACK']) {
    case 'Success':
      drupal_set_message(t('Prior authorization captured successfully.'));

      // Update the original transaction amount to the actual capture amount,
      // its remote ID to the capture's transaction ID, and its statuses to
      // indicate successful payment.
      $transaction->amount = $amount;
      $transaction->remote_id = $response['TRANSACTIONID'];
      $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $transaction->remote_status = $response['PAYMENTSTATUS'];

      // Note the capture in the transaction message.
      $transaction->message .= '<br />' . t('Captured: @date', array(
        '@date' => format_date(REQUEST_TIME, 'short'),
      ));
      if ($response['PAYMENTSTATUS'] == 'Pending') {
        $transaction->message .= '<br />' . commerce_paypal_short_pending_reason($response['PENDINGREASON']);
      }
      break;
    default:

      // Display an error message but leave the transaction pending.
      drupal_set_message(t('Prior authorization capture failed, so the transaction will remain in a pending status.'), 'error');
      if (isset($response['L_LONGMESSAGE0'])) {
        drupal_set_message(check_plain($response['L_LONGMESSAGE0']), 'error');
      }
      break;
  }

  // Save the updated original transaction.
  commerce_payment_transaction_save($transaction);

  // Redirect back to the current order payment page.
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}