You are here

function sagepay_token_commerce_payment_transaction_presave in Drupal Commerce SagePay Integration 7

Implement support for Commerce Card on File.

Parameters

commerce_payment_transaction $transaction: The transaction about to be saved.

Return value

bool Return TRUE if the transaction is modified ok. Return FALSE if the transaction does not contain a token in the payload.

File

modules/sagepay_token/sagepay_token.module, line 87

Code

function sagepay_token_commerce_payment_transaction_presave($transaction) {
  switch ($transaction->payment_method) {
    case 'commerce_sagepay_server':
    case 'commerce_sagepay_direct':

      // Check if transaction payload contains a token.
      if (!isset($transaction->payload['Token'])) {
        return FALSE;
      }

      // Check if the transaction status is OK so we don't save multiples
      // cards.
      if ($transaction->payload['Status'] != 'OK') {
        return FALSE;
      }
      $save_cardonfile = FALSE;

      // As we have a token, check if the store settings permit us to save it.
      $cardonfile_store = isset($transaction->payload['cardonfile_store']) ? $transaction->payload['cardonfile_store'] : FALSE;
      if (!empty($cardonfile_store)) {

        // card on file selector is present and selected.
        $save_cardonfile = TRUE;
      }
      else {
        if ($cardonfile_store === 0) {

          // card on file selector is present and not selected.
          $save_cardonfile = FALSE;
        }
        else {
          if (variable_get('commerce_cardonfile_storage', '') == 'required') {

            // Card on file settings is set to Required.
            $save_cardonfile = TRUE;
          }
        }
      }
      if (!$save_cardonfile) {
        return;
      }

      // Check Card on file config settings for the a value allowing us to
      // store card on file with no user opt in.
      $order = commerce_order_load($transaction->order_id);
      $uid = $order->uid;
      if ($uid == 0) {
        return FALSE;
      }
      $wrapper = entity_metadata_wrapper('commerce_order', $order);
      $address = $wrapper->commerce_customer_billing->commerce_customer_address
        ->value();
      $card_data = array(
        'uid' => $uid,
        'payment_method' => $transaction->payment_method,
        'instance_id' => $transaction->instance_id,
        'remote_id' => $transaction->payload['Token'],
        'card_type' => $transaction->payload['CardType'],
        'card_number' => $transaction->payload['Last4Digits'],
        'card_name' => $address['first_name'] . ' ' . $address['last_name'],
        'status' => '1',
      );
      $expiry_date = $transaction->payload['ExpiryDate'];
      $expiry_month = substr($expiry_date, 0, 2);
      $expiry_year = '20' . substr($expiry_date, 2, 2);
      $card_data['card_exp_month'] = $expiry_month;
      $card_data['card_exp_year'] = $expiry_year;
      $card_entity = commerce_cardonfile_new($card_data);
      commerce_cardonfile_save($card_entity);
      if ($transaction->payload['cardonfile_instance_default']) {
        commerce_cardonfile_set_default_card($card_entity->card_id);
      }
      return TRUE;
      break;
    default:
      return FALSE;
  }
}