function uc_stripe_charge in Ubercart Stripe 7.3
Same name and namespace in other branches
- 6.2 uc_stripe.module \uc_stripe_charge()
- 6 uc_stripe.module \uc_stripe_charge()
- 7 uc_stripe.module \uc_stripe_charge()
- 7.2 uc_stripe.module \uc_stripe_charge()
Generic "charge" callback that runs on checkout and via the order's "card" terminal
Parameters
$order_id:
$amount:
$data:
Return value
array
1 string reference to 'uc_stripe_charge'
- uc_stripe_uc_payment_gateway in ./
uc_stripe.module - Implements hook_payment_gateway to register this payment gateway
File
- ./
uc_stripe.module, line 619 - A stripe.js PCI-compliant payment gateway Forked from Bitcookie's work (thanks!) which was posted at http://bitcookie.com/blog/pci-compliant-ubercart-and-stripe-js from discussion in the uc_stripe issue queue, https://www.drupal.org/node/1467886
Code
function uc_stripe_charge($order_id, $amount, $data) {
global $user;
// Load the stripe PHP API
if (!_uc_stripe_prepare_api()) {
$result = array(
'success' => FALSE,
'comment' => t('Stripe API not found.'),
'message' => t('Stripe API not found. Contact the site administrator.'),
'uid' => $user->uid,
'order_id' => $order_id,
);
return $result;
}
$order = uc_order_load($order_id);
$context = array(
'revision' => 'formatted-original',
'type' => 'amount',
);
$options = array(
'sign' => FALSE,
'thou' => FALSE,
'dec' => FALSE,
'prec' => 2,
);
// Format the amount in cents, which is what Stripe wants
$amount = uc_currency_format($amount, FALSE, FALSE, FALSE);
// Charge the stripe customer the amount in the order
//--Handle transactions for $0
// Stripe can't handle transactions < $0.50, but $0 is a common value
// so we will just return a positive result when the amount is $0.
if ($amount == 0) {
$result = array(
'success' => TRUE,
'message' => t('Payment of $0 approved'),
'uid' => $user->uid,
'trans_id' => md5(uniqid(rand())),
);
uc_order_comment_save($order_id, $user->uid, $result['message'], 'admin');
return $result;
}
$stripe_customer_id = null;
if (key_exists('stripe_customer_id', $order->data)) {
$stripe_customer_id = $order->data['stripe_customer_id'];
}
// Rexamine Payment Intent and Record payment or failure to the customer
try {
if (!key_exists('payment_intent_id', $order->data)) {
throw new Exception('The payment Intent has failed.');
}
//Bail if there's no customer ID
if (empty($stripe_customer_id) || is_null($stripe_customer_id)) {
throw new Exception('No customer ID found');
}
//Bail if there's no payment method
if (empty($_SESSION['stripe']['payment_method'])) {
throw new Exception('Token not found');
}
$stripe_payment_method_id = $_SESSION['stripe']['payment_method'];
$params = array(
"amount" => $amount,
"currency" => strtolower($order->currency),
"customer" => $stripe_customer_id,
"description" => t("Order #@order_id", array(
"@order_id" => $order_id,
)),
"metadata" => get_order_metadata($order),
"payment_method" => $stripe_payment_method_id,
"payment_method_types" => [
'card',
],
"confirm" => true,
);
$intent_id = $order->data['payment_intent_id'];
if (!empty($shipping_info)) {
$params['shipping'] = $shipping_info;
\Stripe\PaymentIntent::update($intent_id, [
'shipping' => $shipping_info,
]);
}
// charge the Customer the amount in the order
$payment_intent = \Stripe\PaymentIntent::retrieve($intent_id);
if ($payment_intent->status != 'succeeded') {
throw new Exception($payment_intent['last_payment_error']['message']);
}
$charge_id = $payment_intent->charges->data[0]['id'];
$formatted_amount = $amount / 100;
$formatted_amount = number_format($formatted_amount, 2);
// $payment_method = \Stripe\PaymentMethod::retrieve($payment_intent->payment_method);
// $payment_method->attach(['customer' => $stripe_customer_id]);
$result = array(
'success' => TRUE,
'message' => t('Payment of @amount processed successfully, Stripe transaction id @transaction_id.', array(
'@amount' => $formatted_amount,
'@transaction_id' => $charge_id,
)),
'comment' => t('Stripe transaction ID: @transaction_id', array(
'@transaction_id' => $charge_id,
)),
'uid' => $user->uid,
);
uc_order_comment_save($order_id, $user->uid, $result['message'], 'admin');
return $result;
} catch (Exception $e) {
$result = array(
'success' => FALSE,
'comment' => $e
->getCode(),
'message' => t("Stripe Charge Failed for order !order: !message", array(
"!order" => $order_id,
"!message" => $e
->getMessage(),
)),
'uid' => $user->uid,
'order_id' => $order_id,
);
uc_order_comment_save($order_id, $user->uid, $result['message'], 'admin');
watchdog('uc_stripe', 'Stripe charge failed for order @order, message: @message', array(
'@order' => $order_id,
'@message' => $result['message'],
));
return $result;
}
// Default / Fallback procedure to fail if the above conditions aren't met
$result = array(
'success' => FALSE,
'comment' => "Stripe Gateway Error",
'message' => "Stripe Gateway Error",
'uid' => $user->uid,
'order_id' => $order_id,
);
uc_order_comment_save($order_id, $user->uid, $result['message'], 'admin');
watchdog('uc_stripe', 'Stripe gateway error for order @order_id', array(
'order_id' => $order_id,
));
return $result;
}