You are here

function commerce_stripe_submit_form_submit in Commerce Stripe 7.2

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

Payment method callback: checkout form submission.

File

./commerce_stripe.module, line 196
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_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {

  // If instructed to do so, try using the specified card on file.
  if (module_exists('commerce_cardonfile') && $payment_method['settings']['cardonfile'] && !empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
    $card_data = commerce_cardonfile_load($pane_values['cardonfile']);
    if (empty($card_data) || $card_data->status == 0) {
      drupal_set_message(t('The requested card on file is no longer valid.'), 'error');
      return FALSE;
    }
    return commerce_stripe_cardonfile_charge($payment_method, $card_data, $order, $charge);
  }

  // The card is new.  Either charge and forget, or charge and save.
  if (!commerce_stripe_load_library()) {
    drupal_set_message(t('Error making the payment. Please contact shop admin to proceed.'), 'error');
    return FALSE;
  }

  // Begin assembling charge parameters.
  Stripe::setApiKey($payment_method['settings']['secret_key']);
  $c = array(
    'amount' => $charge['amount'],
    'currency' => $payment_method['settings']['stripe_currency'],
    'card' => $_POST['stripeToken'],
    'description' => t('Order Number: @order_number', array(
      '@order_number' => $order->order_number,
    )),
  );

  // To later store the card with all required fields, carry out necessary steps before making the charge request.
  if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) && !empty($pane_values['credit_card']['cardonfile_store']) && $pane_values['credit_card']['cardonfile_store']) {
    $save_card = TRUE;
    $card = _commerce_stripe_create_card($_POST['stripeToken'], $order->uid, $payment_method);
    $stripe_card_id = $card->id;
    $stripe_customer_id = $card->customer;
    $c['card'] = $stripe_card_id;
    $c['customer'] = $stripe_customer_id;
  }
  $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);
  } catch (Exception $e) {
    drupal_set_message(t('We received the following error processing your card. Please enter your information again or try a different card.'), 'error');
    drupal_set_message(check_plain($e
      ->getMessage()), 'error');
    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;
  }

  // If so instructed by the customer, save the card.
  if (!empty($save_card)) {

    /**
       // Try fetching the name to store with the card from the billing pane.
       $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
       if ($order_wrapper->commerce_customer_billing->value()) {
         $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
        }
       else {
         $billing_address = array();
       }
    *
    */
    _commerce_stripe_save_cardonfile($response->card, $order->uid, $payment_method, $pane_values['cardonfile_instance_default']);
  }
}