function commerce_stripe_submit_form in Commerce Stripe 7
Same name and namespace in other branches
- 7.3 commerce_stripe.module \commerce_stripe_submit_form()
- 7.2 commerce_stripe.module \commerce_stripe_submit_form()
Payment method callback: checkout form.
File
- ./
commerce_stripe.module, line 350 - 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) {
global $user;
$integration_type = !empty($payment_method['settings']['integration_type']) ? $payment_method['settings']['integration_type'] : 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 javacript so they can be included in
// the token request 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'] : '',
);
drupal_add_js(array(
'commerce_stripe_address' => $address,
), array(
'type' => 'setting',
));
// Differentiate form elements based on stripe integration type.
if ($integration_type == 'stripejs') {
// @todo: Use _commerce_stripe_form_add_credit_card_form().
$form = _commerce_stripe_credit_card_form();
// Include the stripe.js from stripe.com.
drupal_add_js('https://js.stripe.com/v2/', 'external');
}
elseif ($integration_type == 'checkout') {
$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']);
}
}
$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');
// Add external checkout.js library.
drupal_add_js('https://checkout.stripe.com/checkout.js', 'external');
}
// @todo: Use _commerce_stripe_form_configure_stripe_common().
// Add stripe token field. This field is a container for token received from
// Stripe API.
$form['stripe_token'] = array(
'#type' => 'hidden',
'#attributes' => array(
'id' => 'stripe_token',
),
'#default_value' => !empty($pane_values['stripe_token']) ? $pane_values['stripe_token'] : '',
);
// Set our key to settings array.
drupal_add_js(array(
'stripe' => array(
'publicKey' => $payment_method['settings']['public_key'],
'integration_type' => $integration_type,
),
), 'setting');
// Load commerce_stripe.js.
$form['#attached']['js'] = array(
drupal_get_path('module', 'commerce_stripe') . '/commerce_stripe.js',
);
// To display validation errors.
$form['errors'] = array(
'#type' => 'markup',
'#markup' => '<div class="payment-errors"></div>',
);
return $form;
}