You are here

function commerce_payflow_link_capture_form_submit in Commerce PayPal 7.2

Submit handler: process a prior authorization capture via Payflow Pro.

File

modules/payflow/includes/commerce_payflow.admin.inc, line 94
Administrative forms for the Payflow Link module.

Code

function commerce_payflow_link_capture_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $amount = $form_state['values']['amount'];
  $order = $form_state['order'];
  $payment_method = $form_state['payment_method'];

  // Prepare a name-value pair array to capture the requested amount.
  $nvp = array(
    'TRXTYPE' => 'D',
    'ORIGID' => $transaction->remote_id,
    'AMT' => $amount,
    'CAPTURECOMPLETE' => 'Y',
  );

  // Submit the capture request to Payflow Pro.
  $response = commerce_payflow_api_request($payment_method, 'pro', $nvp, $order);
  $transaction->payload[REQUEST_TIME . '-capture'] = $response;
  switch (intval($response['RESULT'])) {
    case 0:
      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 = commerce_currency_decimal_to_amount($amount, $transaction->currency_code);
      $transaction->remote_id = $response['PNREF'];
      $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $transaction->remote_status = 'D';

      // Note the capture in the transaction message.
      $transaction->message .= '<br />' . t('Captured: @date', array(
        '@date' => format_date(REQUEST_TIME, 'short'),
      ));

      // Add the PayPal fees to the message if available.
      if (!empty($response['FEEAMT'])) {
        $transaction->message .= '<br />' . t('PayPal fees: @feeamt', array(
          '@feeamt' => $response['FEEAMT'],
        ));
      }
      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');
      drupal_set_message(check_plain($response['RESPMSG']), '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';
}