function commerce_stripe_submit_form_submit in Commerce Stripe 7.3
Same name and namespace in other branches
- 7 commerce_stripe.module \commerce_stripe_submit_form_submit()
- 7.2 commerce_stripe.module \commerce_stripe_submit_form_submit()
Payment method callback: checkout form submission.
File
- ./
commerce_stripe.module, line 612 - 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') && !empty($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;
}
$txn_capture_bool = _commerce_stripe_get_txn_capture_bool($payment_method, $pane_values);
// Build a default description and offer modules the possibility to alter it.
$description = t('Order Number: @order_number', array(
'@order_number' => $order->order_number,
));
if (isset($charge['currency_code'])) {
$currency_code = $charge['currency_code'];
}
else {
$currency_code = $payment_method['settings']['stripe_currency'];
}
$c = array(
'amount' => $charge['amount'],
'currency' => $currency_code,
'card' => $pane_values['stripe_token'],
'capture' => $txn_capture_bool,
'description' => $description,
);
// Specify that we want to send a receipt email if we are configured to do so.
if (!empty($payment_method['settings']['receipt_email'])) {
$c['receipt_email'] = $order->mail;
}
// Include the payment method settings array on the order object in a
// temporary field for use in the ensuing hooks.
$order->payment_method_settings = $payment_method['settings'];
// The metadata could be added via the alter below but for compatibility
// reasons it may stay.
commerce_stripe_add_metadata($c, $order);
// Let modules alter the charge object to add attributes.
drupal_alter('commerce_stripe_order_charge', $c, $order);
// 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'])) {
$user = user_load($order->uid);
$card = _commerce_stripe_create_card($pane_values['stripe_token'], $user, $payment_method);
// If the card is not declined or otherwise is error-free, we can save it.
if ($card && !empty($card->id)) {
$stripe_card_id = $card->id;
$stripe_customer_id = $card->customer;
$c['card'] = $stripe_card_id;
$c['customer'] = $stripe_customer_id;
$save_card = TRUE;
}
}
$transaction = commerce_payment_transaction_new('commerce_stripe', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $charge['amount'];
$transaction->currency_code = $currency_code;
/*
* save the transaction as pending. this will cause an exception to be thrown
* if the transaction cannot be saved. this prevents the scenario where it
* can go all the way through the try/catch below with success in stripe but
* failing to ever save the transaction. saving the transaction here also acts as
* an early catch to prevent the stripe charge from going through if the Drupal
* side will be unable to save it for some reason.
*/
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
if (!_commerce_stripe_commerce_payment_transaction_save($transaction)) {
return FALSE;
}
try {
// Stripe does not appreciate $0 transfers.
if ($charge['amount'] > 0) {
$response = Stripe\Charge::create($c);
$transaction->remote_id = $response->id;
$transaction->payload[REQUEST_TIME] = $response
->__toJSON();
$transaction->message = commerce_stripe_get_txn_message_success($txn_capture_bool);
$transaction->status = commerce_stripe_get_txn_status($txn_capture_bool);
$transaction->remote_status = commerce_stripe_get_remote_status($txn_capture_bool);
_commerce_stripe_commerce_payment_transaction_save($transaction);
}
// If the total is $0 we just put the card on file.
} catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card: :error
Please enter your information again or try a different card.', array(
':error' => $e
->getMessage(),
)), 'error');
watchdog('commerce_stripe', 'Following error received when processing card @stripe_error', array(
'@stripe_error' => $e
->getMessage(),
), WATCHDOG_NOTICE);
if (is_a($e, 'Stripe\\Error\\Base')) {
$transaction->remote_id = $e
->getHttpStatus();
$transaction->payload[REQUEST_TIME] = $e->jsonBody;
}
else {
$transaction->remote_id = $e
->getCode();
$transaction->payload = $e
->getTraceAsString();
}
$transaction->message = t('Card processing error: @stripe_error', array(
'@stripe_error' => $e
->getMessage(),
));
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
$transaction->remote_status = 'FAILED';
_commerce_stripe_commerce_payment_transaction_save($transaction);
return FALSE;
}
// If so instructed by the customer, save the card.
if (!empty($save_card)) {
$profile = NULL;
if (!empty($order->commerce_customer_billing)) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Get the billing profile in use and associate it with the card
$profile = $order_wrapper->commerce_customer_billing
->value();
}
_commerce_stripe_save_cardonfile($response->source, $order->uid, $payment_method, $pane_values['cardonfile_instance_default'], $profile);
}
}