You are here

function commerce_sagepay_transaction in Drupal Commerce SagePay Integration 7

Create a Transaction and associate it with the order.

Parameters

array $payment_method: The details of the payment method being used.

commerce_order $order: The Commerce order associated with the transaction.

array $charge: The charge details array including amount and currency.

array $tokens: Tokens available for the transaction message.

int $transaction_status: The transaction status (a constant defined by Drupal Commerce).

string $remote_status: A String indicated the status of the transaction at Sage Pay.

commerce_payment_transaction $transaction: If a transaction is being updated, this will be present.

5 calls to commerce_sagepay_transaction()
commerce_sagepay_direct_submit_form_submit in includes/commerce_sagepay_direct.inc
Payment method callback: checkout form submission.
commerce_sagepay_form_redirect_form_validate in includes/commerce_sagepay_form.inc
Implements hook_redirect_form_validate().
commerce_sagepay_paypal_handle_callback in modules/commerce_sagepay_paypal/includes/commerce_sagepay_paypal.inc
Handle a POST callback from Pay Pal to confirm the transaction.
commerce_sagepay_process_response in includes/commerce_sagepay_common.inc
Helper function to process the response from SagePay of any transaction type.
commerce_sagepay_server_handle_callback in includes/commerce_sagepay_server.inc
Process the callback that is sent by SagePay Server.

File

includes/commerce_sagepay_common.inc, line 122
Common utility functions shared by all SagePay modules.

Code

function commerce_sagepay_transaction($payment_method, $order, $charge, $tokens, $transaction_status, $remote_status, $transaction = NULL) {
  $tokens['VendorTxCode'] = $order->data['VendorTxId'];
  if (!isset($transaction)) {
    $transaction = commerce_payment_transaction_new($payment_method['method_id'], $order->order_id);
    $transaction->instance_id = $payment_method['instance_id'];
    $transaction->amount = $charge['amount'];
    $transaction->currency_code = $charge['currency_code'];
  }
  else {
    $transaction->revision = 1;
  }

  //  $transaction->payload += $tokens;
  $transaction->payload = array_merge($transaction->payload, $tokens);
  if (array_key_exists('VPSTxId', $tokens)) {
    $transaction->remote_id = $tokens['VPSTxId'];
  }
  $transaction->remote_status = $remote_status;

  // Set a status for the payment - one of COMMERCE_PAYMENT_STATUS_SUCCESS,
  // COMMERCE_PAYMENT_STATUS_PENDING or COMMERCE_PAYMENT_STATUS_FAILURE
  $transaction->status = $transaction_status;
  $transaction_message = 'Status @status, @statusdetail. ';
  $transaction_message .= 'VPSTxId=@vpstxid. ';
  $transaction_message .= 'Auth Code=@authcode. ';
  $transaction_message .= 'Address Check: @address. ';
  $transaction_message .= 'Postcode Check: @postcode. ';
  $transaction_message .= 'AVSCV2 Result: @avs. ';
  $transaction_message .= '3D Secure: @tds. ';
  $transaction_message .= 'Address Status: @addressstatus. ';
  $transaction_message .= 'Payer Status: @payerstatus. ';
  $transaction_message .= 'Card Type: @cardtype. ';
  $transaction_message .= 'last 4 Digits: @last4digits. ';
  $transaction_message .= 'Fraud Response: @fraudresponse. ';
  $transaction_message .= 'Surcharge: @surcharge. ';
  $transaction_message .= 'Bank Auth Code: @bankauthcode. ';
  $transaction_message .= 'Decline Code: @declinecode. ';
  $transaction->message = $transaction_message;
  $transaction->message_variables = array(
    '@status' => isset($tokens['Status']) ? $tokens['Status'] : $transaction->status,
    '@statusdetail' => isset($tokens['StatusDetail']) ? $tokens['StatusDetail'] : 'N/A',
    '@vpstxid' => isset($tokens['VPSTxId']) ? $tokens['VPSTxId'] : 'N/A',
    '@authcode' => isset($tokens['TxAuthNo']) ? $tokens['TxAuthNo'] : 'N/A',
    '@address' => isset($tokens['AddressResult']) ? $tokens['AddressResult'] : 'N/A',
    '@postcode' => isset($tokens['PostCodeResult']) ? $tokens['PostCodeResult'] : 'N/A',
    '@avs' => isset($tokens['AVSCV2']) ? $tokens['AVSCV2'] : 'N/A',
    '@tds' => isset($tokens['3DSecureStatus']) ? $tokens['3DSecureStatus'] : 'N/A',
    '@giftaid' => isset($tokens['GiftAid']) ? $tokens['GiftAid'] : 'No',
    '@addressstatus' => isset($tokens['AddressStatus']) ? $tokens['AddressStatus'] : 'N/A',
    '@payerstatus' => isset($tokens['PayerStatus']) ? $tokens['PayerStatus'] : 'N/A',
    '@cardtype' => isset($tokens['CardType']) ? $tokens['CardType'] : 'N/A',
    '@last4digits' => isset($tokens['Last4Digits']) ? $tokens['Last4Digits'] : 'N/A',
    '@fraudresponse' => isset($tokens['FraudResponse']) ? $tokens['FraudResponse'] : 'N/A',
    '@surcharge' => isset($tokens['Surcharge']) ? $tokens['Surcharge'] : 'N/A',
    '@bankauthcode' => isset($tokens['BankAuthCode']) ? $tokens['BankAuthCode'] : 'N/A',
    '@declinecode' => isset($tokens['DeclineCode']) ? $tokens['DeclineCode'] : 'N/A',
  );
  commerce_payment_transaction_save($transaction);
  return $transaction;
}