You are here

function commerce_payflow_link_redirect_form_validate in Commerce PayPal 7.2

Payment method callback: redirect form return validation.

File

modules/payflow/commerce_payflow.module, line 682
Implements PayPal Payments Advanced (U.S. only) and Payflow Link Hosted Checkout pages and Transparent Redirect.

Code

function commerce_payflow_link_redirect_form_validate($order, $payment_method) {
  if (!empty($payment_method['settings']['silent_post_logging']) && $payment_method['settings']['silent_post_logging'] == 'full_post') {
    watchdog('commerce_payflow', 'Customer returned from Payflow with the following POST data: !data', array(
      '!data' => '<pre>' . check_plain(print_r($_POST, TRUE)) . '</pre>',
    ), WATCHDOG_NOTICE);
  }

  // If for some reason the payment redirect key in this return post does not
  // match that in the order, prevent processing.
  if (!empty($_POST['USER1']) && $_POST['USER1'] != $order->data['payment_redirect_key']) {
    watchdog('commerce_payflow', 'Customer returned from Payflow with a non-matching redirect key.', array(), WATCHDOG_WARNING);
    return FALSE;
  }

  // This may be an unnecessary step, but if for some reason the user does end
  // up returning at the success URL with a Failed payment, go back.
  if (isset($_POST['RESULT']) && !commerce_payflow_link_validate_result($_POST['RESULT'])) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $order_total = $order_wrapper->commerce_order_total
      ->value();

    // Determine the currency code used to actually process the transaction,
    // which will either be the default currency code or the currency code of
    // the charge if it's supported by PayPal if that option is enabled.
    $currency_code = $payment_method['settings']['currency_code'];
    if (!empty($payment_method['settings']['allow_supported_currencies']) && in_array($order_total['currency_code'], array_keys(commerce_paypal_currencies($payment_method['method_id'])))) {
      $currency_code = $order_total['currency_code'];
    }

    // Provide a more descriptive error message in the failed transaction and
    // the watchdog.
    $transaction = commerce_payment_transaction_new($payment_method['method_id'], $order->order_id);
    $transaction->instance_id = $payment_method['instance_id'];
    $transaction->amount = commerce_currency_decimal_to_amount(isset($_POST['AMT']) ? $_POST['AMT'] : 0, $currency_code);
    $transaction->currency_code = $currency_code;
    $transaction->payload[REQUEST_TIME] = $_POST;
    $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
    $transaction->message = commerce_payflow_link_result_message($_POST['RESULT']);
    commerce_payment_transaction_save($transaction);
    return FALSE;
  }
}