You are here

commerce_cardonfile_testing.module in Commerce Card on File 7.2

Provides a test payment method implementing the Card on File API.

File

tests/commerce_cardonfile_testing/commerce_cardonfile_testing.module
View source
<?php

/**
 * @file
 * Provides a test payment method implementing the Card on File API.
 */

/**
 * Implements hook_commerce_payment_method_info().
 */
function commerce_cardonfile_testing_commerce_payment_method_info() {
  $payment_methods = array();
  $payment_methods['commerce_cardonfile_testing_payment'] = array(
    'title' => t('Commerce Card on File Test Payment'),
    'description' => t('Payment gateway that allows testing Commerce Card on File.'),
    'cardonfile' => array(
      'charge callback' => 'commerce_cardonfile_testing_payment_cardonfile_charge',
      'create callback' => 'commerce_cardonfile_testing_payment_cardonfile_create',
      'update callback' => 'commerce_cardonfile_testing_payment_cardonfile_update',
      'delete callback' => 'commerce_cardonfile_testing_payment_cardonfile_delete',
    ),
  );
  return $payment_methods;
}

/**
 * Payment method callback: submit form.
 */
function commerce_cardonfile_testing_payment_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  $fields = array(
    'type' => array(
      'visa',
      'mastercard',
      'amex',
    ),
    'owner' => '',
    'code' => '',
  );
  $default = array(
    'number' => '4111111111111111',
  );
  $form = commerce_payment_credit_card_form($fields, $default);
  return $form;
}

/**
 * Payment method callback: submit form validation.
 */
function commerce_cardonfile_testing_payment_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
  if (isset($pane_values['cardonfile']) && is_numeric($pane_values['cardonfile'])) {
    $card = commerce_cardonfile_load($pane_values['cardonfile']);
    if ($card) {

      // There is no need for validating a card on file, it already happened
      // when the card had been saved.
      return NULL;
    }
  }

  // Validate credit card fields.
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  $settings = array(
    'form_parents' => array_merge($form_parents, array(
      'credit_card',
    )),
  );
  if (!commerce_payment_credit_card_validate($pane_values['credit_card'], $settings)) {
    return FALSE;
  }
}

/**
 * Payment method callback: submit form submission.
 */
function commerce_cardonfile_testing_payment_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
  $order->data['commerce_cardonfile_testing_payment'] = $pane_values;
  if (isset($pane_values['cardonfile'])) {

    // Card on File form elements are added to the payment form.
    $cardonfile = $pane_values['cardonfile'];
    $cardonfile_instance_default = $pane_values['cardonfile_instance_default'];
    $cardonfile_store = $pane_values['credit_card']['cardonfile_store'];
    if (is_numeric($cardonfile)) {

      // A stored card is selected to use.
      $card = commerce_cardonfile_load($cardonfile);
    }
    elseif ($cardonfile == 'new' && $cardonfile_store) {

      // A new card is used and we are supposed to store it.
      $card = commerce_cardonfile_new(array(
        'uid' => $order->uid,
        'payment_method' => $payment_method['method_id'],
        'instance_id' => $payment_method['instance_id'],
        'remote_id' => rand(1000, 9999),
        'card_type' => $pane_values['credit_card']['type'],
        'card_name' => $pane_values['credit_card']['owner'],
        'card_number' => substr($pane_values['credit_card']['number'], -4),
        'card_exp_month' => $pane_values['credit_card']['exp_month'],
        'card_exp_year' => $pane_values['credit_card']['exp_year'],
        'status' => 1,
      ));
      commerce_cardonfile_save($card);
    }
    if (isset($card)) {
      if ($cardonfile_instance_default) {

        // Card in use is designated as default.
        $card->instance_default = 1;
        $card
          ->save();
      }
      return commerce_cardonfile_testing_payment_cardonfile_charge($payment_method, $card, $order, $charge);
    }
  }
  return commerce_cardonfile_testing_payment_transaction($payment_method, $order, $charge);
}

/**
 * Creates a payment transaction with the specified transaction status.
 */
function commerce_cardonfile_testing_payment_transaction($payment_method, $order, $charge) {
  $transaction_status_overall = TRUE;
  $transaction_status = variable_get('commerce_cardonfile_testing_next_transaction_status', COMMERCE_PAYMENT_STATUS_SUCCESS);
  if (!in_array($transaction_status, array(
    COMMERCE_PAYMENT_STATUS_SUCCESS,
    COMMERCE_PAYMENT_STATUS_PENDING,
  ))) {

    // Consider the transaction failed unless the selected status is "success"
    // or "pending".
    $transaction_status_overall = FALSE;
  }
  $transaction = commerce_payment_transaction_new('commerce_cardonfile_testing_payment', $order->order_id);
  $transaction->instance_id = $payment_method['instance_id'];
  $transaction->amount = $transaction_status_overall ? $charge['amount'] : 0;
  $transaction->currency_code = $charge['currency_code'];
  $transaction->status = $transaction_status;
  $transaction->remote_id = rand(1000, 9999);
  $transaction->message = 'Name: @name. Transaction status: !status.';
  $transaction->message_variables = array(
    '@name' => 'Commerce Card on File Test Payment',
    '!status' => $transaction_status,
  );
  commerce_payment_transaction_save($transaction);
  return $transaction_status;
}

/**
 * Card on File callback: charge credit card.
 */
function commerce_cardonfile_testing_payment_cardonfile_charge($payment_method, $card, $order, $charge = NULL) {
  if (!isset($charge)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $charge = commerce_line_items_total($wrapper->commerce_line_items);
  }
  return commerce_cardonfile_testing_payment_transaction($payment_method, $order, $charge);
}

/**
 * Card on File callback: add new credit card.
 */
function commerce_cardonfile_testing_payment_cardonfile_create($form, $form_state, $payment_method, $card) {
  $card->remote_id = rand(1000, 9999);
  return $card;
}

/**
 * Card on File callback: update credit card.
 */
function commerce_cardonfile_testing_payment_cardonfile_update($form, $form_state, $payment_method, $card) {
  return TRUE;
}

/**
 * Card on File callback: delete credit card.
 */
function commerce_cardonfile_testing_payment_cardonfile_delete($form, $form_state, $payment_method, $card) {
  return TRUE;
}

Functions

Namesort descending Description
commerce_cardonfile_testing_commerce_payment_method_info Implements hook_commerce_payment_method_info().
commerce_cardonfile_testing_payment_cardonfile_charge Card on File callback: charge credit card.
commerce_cardonfile_testing_payment_cardonfile_create Card on File callback: add new credit card.
commerce_cardonfile_testing_payment_cardonfile_delete Card on File callback: delete credit card.
commerce_cardonfile_testing_payment_cardonfile_update Card on File callback: update credit card.
commerce_cardonfile_testing_payment_submit_form Payment method callback: submit form.
commerce_cardonfile_testing_payment_submit_form_submit Payment method callback: submit form submission.
commerce_cardonfile_testing_payment_submit_form_validate Payment method callback: submit form validation.
commerce_cardonfile_testing_payment_transaction Creates a payment transaction with the specified transaction status.