You are here

function commerce_amex_cardonfile_charge in Commerce American Express Payment Gateway (Amex) 7

Card on file callback: background charge payment

Parameters

object $payment_method: The payment method instance definition array.

object $card_data: The stored credit card data array to be processed

object $order: The order object that is being processed

array $charge: The price array for the charge amount with keys of 'amount' and 'currency' If null the total value of the order is used.

Return value

TRUE if the transaction was successful

1 string reference to 'commerce_amex_cardonfile_charge'
commerce_amex_commerce_payment_method_info in ./commerce_amex.module
Implements hook_commerce_payment_method_info().

File

./commerce_amex.module, line 1394
Implements American Express payment gateway for use in Drupal Commerce.

Code

function commerce_amex_cardonfile_charge($payment_method, $card_data, $order, $charge = NULL) {

  // Format order total for transaction.
  if (isset($charge)) {
    $amount = commerce_currency_amount_to_decimal($charge['amount'], $charge['currency_code']);
  }
  else {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $charge = commerce_line_items_total($wrapper->commerce_line_items);
    $amount = commerce_currency_amount_to_decimal($charge->amount, $charge->currency_code);
  }
  $order_amount = number_format($amount, 2, '.', '');

  // Create a new payment transaction and setup the amount.
  $transaction = commerce_payment_transaction_new('amex_hosted', $order->order_id);
  $transaction->amount = $charge['amount'];
  $transaction->currency_code = $charge['currency_code'];
  $transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
  $transaction->instance_id = $payment_method['instance_id'];
  commerce_payment_transaction_save($transaction);
  $data = new stdClass();
  $data->apiOperation = $payment_method['settings']['txn_type'];

  // Drupal Commerce Solution ID.
  $data->partnerSolutionId = '3304010';
  if (isset($card_data->name)) {
    $data->sourceOfFunds['provided']['card']['holder']['lastName'] = $card_data->name;
  }
  $data->sourceOfFunds['token'] = $card_data->remote_id;
  $data->sourceOfFunds['type'] = 'CARD';
  $data->transaction['amount'] = $order_amount;
  $data->transaction['currency'] = $transaction->currency_code;
  $data->transaction['reference'] = $transaction->transaction_id;
  if ($payment_method['settings']['continuous']) {
    $data->transaction['frequency'] = 'RECURRING';
  }
  $data->order['reference'] = $order->order_id;

  // Add the addresses to the data object.
  $data = _commerce_amax_add_addresses($data, $order);
  $url = $payment_method['settings']['txn_url'] . AMEX_TXN_PATH . $payment_method['settings']['merchant_id'] . '/order/' . (10000000000 + $transaction->transaction_id) . '/transaction/' . $transaction->transaction_id;
  $result = _commerce_amex_put_request($url, $payment_method['settings']['password'], $data);
  $transaction->payload = $result;
  switch ($result->result) {
    case 'SUCCESS':
      switch ($payment_method['settings']['txn_type']) {
        case AMEX_OP_AUTH:
          $transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
          break;
        case AMEX_OP_PAY:
          $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      }
      $transaction->remote_status = $result->response->gatewayCode;
      $transaction->message = $result->response->acquirerMessage;
      break;
    case 'PENDING':
      $transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
      $transaction->remote_status = $result->response->gatewayCode;
      $transaction->message = $result->response->acquirerMessage;
      break;
    case 'FAILURE':
      $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      $transaction->remote_status = $result->response->gatewayCode;
      $transaction->message = $result->response->gatewayCode;
      break;
    case 'UNKNOWN':
      break;
    case 'ERROR':
      $transaction = _commerce_amex_error_process($result, $transaction);
      break;
  }
  commerce_payment_transaction_save($transaction);
}