function commerce_stripe_cardonfile_charge in Commerce Stripe 7
Same name and namespace in other branches
- 7.3 commerce_stripe.module \commerce_stripe_cardonfile_charge()
- 7.2 commerce_stripe.module \commerce_stripe_cardonfile_charge()
Card on file callback: background charge payment TODO: implement proper return codes per commerce 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'
File
- ./
commerce_stripe.module, line 766 - 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);
$currency_code = $payment_method['settings']['stripe_currency'];
if (isset($charge['currency_code'])) {
$currency_code = $charge['currency_code'];
}
// Assemble charge parameters.
Stripe::setApiKey($payment_method['settings']['secret_key']);
// Build a default description and offer modules the possibility to alter it.
$description = t('Order Number: @order_number', array(
'@order_number' => $order->order_number,
));
$c = array(
'amount' => $charge['amount'],
'currency' => $currency_code,
'customer' => $customer_id,
'card' => $card_id,
'description' => $description,
);
commerce_stripe_add_metadata($c, $order);
// Let modules alter the charge object to add attributes.
drupal_alter('commerce_stripe_order_charge', $c, $order);
$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 as pending to ensure the drupal side of things is working before we attempt the stripe api call
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
if (!_commerce_stripe_commerce_payment_transaction_save($transaction)) {
return FALSE;
}
try {
$lock = __FUNCTION__ . '_' . $order->order_id;
if (lock_acquire($lock) && $charge['amount'] > 0) {
$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_stripe_commerce_payment_transaction_save($transaction);
lock_release($lock);
return TRUE;
}
} 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->jsonBody;
$transaction->message = t('Card processing error: @stripe_error', array(
'@stripe_error' => $e
->getMessage(),
));
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
_commerce_stripe_commerce_payment_transaction_save($transaction);
lock_release($lock);
return FALSE;
}
}