You are here

function commerce_stripe_submit_form in Commerce Stripe 7.3

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

Payment method callback: checkout and terminal form.

File

./commerce_stripe.module, line 508
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($payment_method, $pane_values, $checkout_pane, $order) {
  $integration_type = !empty($payment_method['settings']['integration_type']) ? $payment_method['settings']['integration_type'] : COMMERCE_STRIPE_DEFAULT_INTEGRATION;
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $field = field_info_field('commerce_customer_address');
  $instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', 'billing');
  $available_countries = NULL;
  if (isset($form_state['input']['country'])) {
    $available_countries = array(
      $form_state['input']['country'] => NULL,
    );
  }

  // Attempt to load the billing address from the order data.
  $billing_address = addressfield_default_values($field, $instance, array(
    $available_countries,
  ));
  if (!empty($order->commerce_customer_billing)) {
    if (!empty($order_wrapper->commerce_customer_billing->commerce_customer_address)) {
      $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address
        ->value();
    }
  }

  // Pass the billing address values to JS, so they can be included in
  // the token creation sent to Stripe.
  $address = array(
    'address_line1' => !empty($billing_address['thoroughfare']) ? $billing_address['thoroughfare'] : '',
    'address_line2' => !empty($billing_address['premise']) ? $billing_address['premise'] : '',
    'address_city' => !empty($billing_address['locality']) ? $billing_address['locality'] : '',
    'address_state' => !empty($billing_address['administrative_area']) ? $billing_address['administrative_area'] : '',
    'address_zip' => !empty($billing_address['postal_code']) ? $billing_address['postal_code'] : '',
    'address_country' => !empty($billing_address['country']) ? $billing_address['country'] : '',
    'name' => !empty($billing_address['name_line']) ? $billing_address['name_line'] : '',
  );

  // Store them in Drupal.settings for easier access.
  drupal_add_js(array(
    'commerce_stripe_address' => $address,
  ), array(
    'type' => 'setting',
  ));
  if ($integration_type === 'checkout' && isset($checkout_pane)) {
    $form = array();

    // Add pay button.
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $order_total = $order_wrapper->commerce_order_total
      ->value();

    // Add Checkout settings.
    $checkout_settings = $payment_method['settings']['checkout_settings'];

    // @todo: Use _commerce_stripe_form_configure_stripe_checkout().
    // Convert JS settings to Booleans before adding them to the page.
    $checkout_settings = array_map(function ($value) {
      if ($value === 0 or $value === 1) {
        $value = (bool) $value;
      }
      return $value;
    }, $checkout_settings);

    // Get the image file if one is set.
    if (isset($checkout_settings['image']['fid'])) {
      $image = file_load($checkout_settings['image']['fid']);
      if (is_object($image)) {
        $checkout_settings['image'] = file_create_url($image->uri);
      }
      else {

        // Empty image setting will cause a broken image to be displayed in checkout
        // iframe.
        unset($checkout_settings['image']);
      }
    }

    // @todo: Add a customer if we have one.
    $checkout_settings += array(
      'currency' => $payment_method['settings']['stripe_currency'],
      'email' => $order->mail,
      'amount' => $order_total['amount'],
    );
    drupal_add_js(array(
      'stripe' => array(
        'checkout' => $checkout_settings,
      ),
    ), 'setting');
  }
  elseif ($integration_type === 'elements' && (empty($pane_values) || empty($pane_values['payment_details']['cardonfile']) || !is_numeric($pane_values['payment_details']['cardonfile']))) {
    $form = _commerce_stripe_elements_form();
  }
  elseif ($integration_type === 'stripejs' && (empty($pane_values) || empty($pane_values['payment_details']['cardonfile']) || !is_numeric($pane_values['payment_details']['cardonfile']))) {
    $form = _commerce_stripe_credit_card_form();
  }

  // Stripe token should be empty? Need to store it in form storage to prevent overwriting during FAPI.
  $stripe_token = !empty($pane_values['stripe_token']) ? check_plain(trim($pane_values['stripe_token'])) : '';
  _commerce_stripe_form_configure_stripe_common($form, $stripe_token, $integration_type);

  // To display validation errors.
  $form['errors'] = array(
    '#type' => 'markup',
    '#markup' => '<div class="payment-errors"></div>',
  );
  return $form;
}