You are here

function sagepay_token_cardonfile_charge in Drupal Commerce SagePay Integration 7

Implements Card on File Charge Callback.

Parameters

$order object: The order being charged.

$parent_order object: The parent order from which the recurring order was derived.

Return value

bool TRUE if the payment was successfully processed.

1 string reference to 'sagepay_token_cardonfile_charge'
sagepay_token_commerce_payment_method_info_alter in modules/sagepay_token/sagepay_token.module
Implements hook_commerce_payment_method_info_alter().

File

modules/sagepay_token/sagepay_token.module, line 223

Code

function sagepay_token_cardonfile_charge($payment_method, $card_data, $order, $charge = NULL) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);

  // Load customer profile.
  $profile = commerce_customer_profile_load($order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id']);

  // Get user billing address.
  $billing_address = $profile->commerce_customer_address[LANGUAGE_NONE][0];

  // Get user delivery address.
  $delivery_address = NULL;
  if (isset($order->commerce_customer_shipping)) {
    $delivery_profile = commerce_customer_profile_load($order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id']);
    $delivery_address = $delivery_profile->commerce_customer_address[LANGUAGE_NONE][0];
  }
  $pane_values = array();
  $pane_values['cardonfile'] = $card_data->card_id;
  $pane_values['commerce_recurring'] = TRUE;
  $settings = array();
  $query = _commerce_sagepay_encrypted_order($settings, $order, $charge, $billing_address, $delivery_address, SAGEPAY_DIRECT, $pane_values);

  // Turn off 3D Secure and AVS Checks for Card on File charges.
  $query['ApplyAVSCV2'] = '2';
  $query['Apply3DSecure'] = '2';

  // Create a POST to send to SagePay.
  $post = '';
  foreach ($query as $name => $value) {
    $post .= urlencode($name) . '=' . urlencode($value) . '&';
  }

  // Chop off the last &.
  $post = substr($post, 0, -1);

  // Determine the correct url based on the transaction mode.
  switch (variable_get(SAGEPAY_SETTING_TRANSACTION_MODE)) {
    case SAGEPAY_TXN_MODE_LIVE:
      $server_url = SAGEPAY_DIRECT_SERVER_LIVE;
      break;
    case SAGEPAY_TXN_MODE_TEST:
      $server_url = SAGEPAY_DIRECT_SERVER_TEST;
      break;
    case SAGEPAY_TXN_MODE_SHOWPOST:
      $server_url = SAGEPAY_DIRECT_SERVER_SHOWPOST;
      break;
    case SAGEPAY_TXN_MODE_SIMULATION:
      $server_url = SAGEPAY_DIRECT_SERVER_SIMULATION;
      break;
    default:
      $server_url = SAGEPAY_DIRECT_SERVER_SIMULATION;
  }
  drupal_alter('sagepay_url', $server_url, $pane_values);

  // Send post.
  $response = _commerce_sagepay_request_post($server_url, $post);

  // Collect additional data to store in the transaction.
  if (isset($pane_values['credit_card'])) {
    $response['Last4Digits'] = substr($pane_values['credit_card']['number'], 12, 16);
    $response['ExpMonth'] = $pane_values['credit_card']['exp_month'];
    $response['ExpYear'] = $pane_values['credit_card']['exp_year'];
    $response['cardonfile_store'] = isset($pane_values['credit_card']['cardonfile_store']) ? $pane_values['credit_card']['cardonfile_store'] : '0';
    $response['CardType'] = $pane_values['credit_card']['type'];
  }
  $response['Amount'] = $wrapper->commerce_order_total->amount
    ->value();
  $result = commerce_sagepay_process_response($payment_method, $order, $response);
  switch ($response['Status']) {
    case 'NOTAUTHED':
    case 'REJECTED':
    case 'MALFORMED':
    case 'INVALID':
    case 'ERROR':
      $cardonfile_response['code'] = COMMERCE_COF_PROCESS_CODE_CARD_NOT_CHARGEABLE;
      $cardonfile_response['message'] = $response['StatusDetail'];
      break;
    case 'OK':
    case 'AUTHENTICATED':
      $cardonfile_response['code'] = COMMERCE_COF_PROCESS_CODE_METHOD_SUCCESS;
      $cardonfile_response['message'] = $response['StatusDetail'];
      break;
    default:
      $cardonfile_response['code'] = COMMERCE_COF_PROCESS_CODE_CARD_NOT_CHARGEABLE;
      $cardonfile_response['message'] = $response['StatusDetail'];
  }
  return $cardonfile_response;
}