You are here

function commerce_payflow_link_refund_form_submit in Commerce PayPal 7.2

Submit handler: process a refund via Payflow Pro.

File

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

Code

function commerce_payflow_link_refund_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' => 'C',
    'ORIGID' => $transaction->remote_id,
    'AMT' => $amount,
  );

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

  // If the credit succeeded...
  if (intval($response['RESULT']) === 0) {
    $credit_amount = commerce_currency_decimal_to_amount($amount, $transaction->currency_code);
    drupal_set_message(t('Refund for @amount issued successfully.', array(
      '@amount' => commerce_currency_format($credit_amount, $transaction->currency_code),
    )));

    // Create a new transaction to record the credit.
    $credit_transaction = commerce_payment_transaction_new($payment_method['method_id'], $order->order_id);
    $credit_transaction->instance_id = $payment_method['instance_id'];
    $credit_transaction->remote_id = $response['PNREF'];
    $credit_transaction->amount = $credit_amount * -1;
    $credit_transaction->currency_code = $transaction->currency_code;
    $credit_transaction->payload[REQUEST_TIME] = $response;
    $credit_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    $credit_transaction->remote_status = 'C';
    $credit_transaction->message = t('Refunded to @remote_id.', array(
      '@remote_id' => $transaction->remote_id,
    ));

    // Save the credit transaction.
    commerce_payment_transaction_save($credit_transaction);
  }
  else {

    // Display a failure message and response reason from Payflow.
    drupal_set_message(t('Refund failed: @reason', array(
      '@reason' => $response['RESPMSG'],
    )), 'error');

    // Save the failure response message to the original transaction.
    $transaction->payload[REQUEST_TIME . '-refund'] = $response;
    commerce_payment_transaction_save($transaction);
  }
  $form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id . '/payment';
}