You are here

function commerce_payflow_link_reference_form_submit in Commerce PayPal 7.2

Submit handler: process a reference transaction via Payflow Pro.

File

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

Code

function commerce_payflow_link_reference_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(
    'BUTTONSOURCE' => $payment_method['buttonsource'],
    'TRXTYPE' => 'S',
    'ORIGID' => $transaction->remote_id,
    'AMT' => $amount,
    'TENDER' => 'C',
  );

  // If the reference transaction needs to go through PayPal, add the additional
  // parameters for the billing agreement.
  if (!empty($transaction->data['commerce_payflow']['tender']) && $transaction->data['commerce_payflow']['tender'] == 'P') {
    $nvp['TENDER'] = 'P';
    $nvp['ACTION'] = 'D';
    $nvp['BAID'] = $transaction->data['commerce_payflow']['baid'];

    // Ensure the transaction references the original PNREF.
    if (!empty($transaction->data['commerce_payflow']['pnref'])) {
      $nvp['ORIGID'] = $transaction->data['commerce_payflow']['pnref'];
    }

    // Update the BUTTONSOURCE to indicate Express Checkout.
    $ec_payment_method = commerce_payment_method_instance_load('paypal_ec|' . $payment_method['settings']['paypal_ec_instance']);
    $nvp['BUTTONSOURCE'] = $ec_payment_method['buttonsource'];
  }

  // Submit the reference transaction request to Payflow Pro.
  $response = commerce_payflow_api_request($payment_method, 'pro', $nvp, $order);

  // Create a new transaction to represent the reference transaction.
  $new_transaction = clone $transaction;
  unset($new_transaction->transaction_id, $new_transaction->revision_id, $new_transaction->created, $new_transaction->changed);
  $new_transaction->amount = commerce_currency_decimal_to_amount($amount, $transaction->currency_code);
  $new_transaction->payload[REQUEST_TIME . '-reference'] = $response;
  $new_transaction->remote_id = $response['PNREF'];
  $new_transaction->remote_status = 'S';
  if ($transaction->data['commerce_payflow']['tender'] == 'P') {
    if (!empty($response['PPREF'])) {
      $new_transaction->remote_id = $response['PPREF'];
    }
    if (!empty($response['PAYMENTSTATUS'])) {
      $new_transaction->remote_status = commerce_payflow_paypal_remote_status($response['PAYMENTSTATUS']);
    }
  }
  $message = array();
  if (isset($response['RESULT']) && intval($response['RESULT']) === 0) {
    drupal_set_message(t('Reference transaction processed successfully.'));
    $message[] = t('Reference transaction from @origid.', array(
      '@origid' => $transaction->remote_id,
    ));

    // Add the PayPal billing agreement ID and fees if given.
    if (!empty($response['BAID'])) {
      $message[] = t('Billing agreement ID: @baid', array(
        '@baid' => $response['BAID'],
      ));
    }
    if (!empty($response['FEEAMT'])) {
      $message[] = t('PayPal fees: @feeamt', array(
        '@feeamt' => $response['FEEAMT'],
      ));
    }
    $new_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
  }
  else {

    // Display an error message but leave the transaction pending.
    drupal_set_message(t('Refrence transaction failed: @reason', array(
      '@reason' => $response['RESPMSG'],
    )), 'error');
    $message[] = t('Failed reference transaction from @origid.', array(
      '@origid' => $transaction->remote_id,
    ));
    $new_transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
  }
  $new_transaction->message = implode('<br />', $message);

  // Save the new transaction.
  commerce_payment_transaction_save($new_transaction);

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