You are here

function commerce_worldpay_bg_transaction_process in Commerce Worldpay 7

Process the WPPR once it's been validated.

1 call to commerce_worldpay_bg_transaction_process()
commerce_worldpay_bg_response_page in includes/commerce_worldpay_bg.page.inc
Page callback that listens for transaction information from WorldPay.

File

includes/commerce_worldpay_bg.page.inc, line 302
Various page callback related functions.

Code

function commerce_worldpay_bg_transaction_process($order, $payment_method, &$wppr, &$prior_wppr = NULL) {

  // Exit when we don't get a payment status we recognize.
  if (!in_array($wppr['transStatus'], array(
    'Y',
    'C',
  ))) {
    commerce_payment_redirect_pane_previous_page($order);
    return FALSE;
  }

  // If this is a prior authorization capture WorldPay Payment Response for
  // which we've already created a transaction...
  if ($prior_wppr) {

    // Load the prior IPN's transaction and update that with the capture values.
    $transaction = commerce_payment_transaction_load($prior_wppr['transaction_id']);
  }
  else {

    // Create a new payment transaction for the order.
    $transaction = commerce_payment_transaction_new('commerce_worldpay_bg', $order->order_id);
    $transaction->instance_id = $payment_method['instance_id'];
  }
  $transaction->remote_id = $wppr['transId'];
  $transaction->amount = commerce_currency_decimal_to_amount($wppr['authAmount'], $wppr['authCurrency']);
  $transaction->currency_code = $wppr['authCurrency'];
  $transaction->payload[REQUEST_TIME] = $wppr;

  // Remove sensitive data.
  unset($transaction->payload[REQUEST_TIME]['callbackPW']);

  // Set the transaction's statuses based on the IPN's payment_status.
  $transaction->remote_status = $wppr['transStatus'];

  // @todo - Figure out how best to  set status based on SecureCode
  //   authentication.
  if (isset($wppr['authentication'])) {
    switch ($wppr['authentication']) {
      case 'ARespH.card.authentication.0':
        $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
        $transaction->message = t("Cardholder authenticated by SecureCode.");
        break;
      case 'ARespH.card.authentication.1':
        $transaction->message = t('Cardholder/Issuing Bank not enrolled for authentication.');
        break;
      case 'ARespH.card.authentication.6':
        $transaction->message = t('Cardholder authentication not available');
        break;
      case 'ARespH.card.authentication.7':
        $transaction->message = t('Cardholder did not complete authentication.');
        break;
    }
  }

  // They don't give us very detailed transaction information do they?
  switch ($wppr['transStatus']) {
    case 'Y':
      $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $transaction->message = t("WorldPay accepted the user's transaction.");
      break;

    // I don't think we should ever see this status at this point but incase
    // we do...
    case 'C':

      // @todo - Is this a suitable status?
      $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      $transaction->message = t('The user cancelled the payment.');
      break;
  }

  // Save the transaction information.
  commerce_payment_transaction_save($transaction);
  $wppr['transaction_id'] = $transaction->transaction_id;
  commerce_payment_redirect_pane_next_page($order);
  watchdog('commerce_worldpay_bg', 'Payment Response processed for Order @order_number with ID @txn_id.', array(
    '@txn_id' => $wppr['transId'],
    '@order_number' => $order->order_number,
  ), WATCHDOG_INFO);
}