You are here

function commerce_stripe_cardonfile_charge in Commerce Stripe 7.2

Same name and namespace in other branches
  1. 7.3 commerce_stripe.module \commerce_stripe_cardonfile_charge()
  2. 7 commerce_stripe.module \commerce_stripe_cardonfile_charge()

Card on file callback: background charge payment

1 call to commerce_stripe_cardonfile_charge()
commerce_stripe_submit_form_submit in ./commerce_stripe.module
Payment method callback: checkout form submission.
1 string reference to 'commerce_stripe_cardonfile_charge'
commerce_stripe_commerce_payment_method_info in ./commerce_stripe.module
Implements hook_commerce_payment_method_info().

File

./commerce_stripe.module, line 393
This module provides Stripe (http://stripe.com/) payment gateway integration to Commerce. Commerce Stripe offers a PCI-compliant way to process payments straight from you Commerce shop.

Code

function commerce_stripe_cardonfile_charge($payment_method, $card_data, $order, $charge = NULL) {
  if (!commerce_stripe_load_library()) {
    return FALSE;
  }

  // Fetch the customer id and card id from $card_data->remote_id
  list($customer_id, $card_id) = explode('|', $card_data->remote_id);

  // Assemble charge parameters.
  Stripe::setApiKey($payment_method['settings']['secret_key']);
  $c = array(
    'amount' => $charge['amount'],
    'currency' => $payment_method['settings']['stripe_currency'],
    'customer' => $customer_id,
    'card' => $card_id,
    'description' => t('Order Number: @order_number', array(
      '@order_number' => $order->order_number,
    )),
  );
  $transaction = commerce_payment_transaction_new('commerce_stripe', $order->order_id);
  $transaction->instance_id = $payment_method['instance_id'];
  $transaction->amount = $charge['amount'];
  $transaction->currency_code = $charge['currency_code'];
  try {
    $response = Stripe_Charge::create($c);
    $transaction->remote_id = $response->id;
    $transaction->payload[REQUEST_TIME] = $response
      ->__toJSON();
    $transaction->message = t('Payment completed successfully.');
    $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    commerce_payment_transaction_save($transaction);
    return TRUE;
  } catch (Exception $e) {
    watchdog('commerce_stripe', 'Following error received when processing card @stripe_error.', array(
      '@stripe_error' => $e
        ->getMessage(),
    ), WATCHDOG_NOTICE);
    $transaction->remote_id = $e
      ->getHttpStatus();
    $transaction->payload[REQUEST_TIME] = $e->json_body;
    $transaction->message = t('Card processing error: @stripe_error', array(
      '@stripe_error' => $e
        ->getMessage(),
    ));
    $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
    commerce_payment_transaction_save($transaction);
    return FALSE;
  }
}