Skip to content
Snippets Groups Projects
Commit 1cd04b68 authored by mattwire's avatar mattwire
Browse files

Fix 387 paid multi-participant registration fails

parent 2a3429e6
Branches
Tags
No related merge requests found
......@@ -636,9 +636,36 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$intentParams['statement_descriptor_suffix'] = $this->getDescription($params, 'statement_descriptor_suffix');
$intentParams['statement_descriptor'] = $this->getDescription($params, 'statement_descriptor');
if (!$propertyBag->has('paymentIntentID') && !empty($paymentMethodID)) {
// We came in via a flow that did not know the amount before submit (eg. multiple event participants)
// We need to create a paymentIntent
$stripePaymentIntent = new CRM_Stripe_PaymentIntent($this);
$stripePaymentIntent->setDescription($this->getDescription($params));
$stripePaymentIntent->setReferrer($_SERVER['HTTP_REFERER'] ?? '');
$stripePaymentIntent->setExtraData($params['extra_data'] ?? []);
$paymentIntentParams = [
'paymentMethodID' => $paymentMethodID,
'customer' => $stripeCustomer->id,
'capture' => FALSE,
'amount' => $propertyBag->getAmount(),
'currency' => $propertyBag->getCurrency(),
];
$processIntentResult = $stripePaymentIntent->processPaymentIntent($paymentIntentParams);
if ($processIntentResult->ok && !empty($processIntentResult->data['success'])) {
$paymentIntentID = $processIntentResult->data['paymentIntent']['id'];
}
else {
\Civi::log('stripe')->error('Attempted to create paymentIntent from paymentMethod during doPayment failed: ' . print_r($processIntentResult, TRUE));
}
}
// This is where we actually charge the customer
try {
$intent = $this->stripeClient->paymentIntents->retrieve($propertyBag->getCustomProperty('paymentIntentID'));
if (empty($paymentIntentID)) {
$paymentIntentID = $propertyBag->getCustomProperty('paymentIntentID');
}
$intent = $this->stripeClient->paymentIntents->retrieve($paymentIntentID);
if ($intent->amount != $this->getAmount($params)) {
$intentParams['amount'] = $this->getAmount($params);
}
......
......@@ -308,12 +308,19 @@ class CRM_Stripe_PaymentIntent {
return $resultObject;
}
/**
* @param array $params
*
* @return object
* @throws \Stripe\Exception\ApiErrorException
*/
public function processPaymentIntent($params) {
/*
$params = [
// Either paymentIntentID or paymentMethodID must be set
'paymentIntentID' => 'pi_xx',
'paymentMethodID' => 'pm_xx',
'customer' => 'cus_xx', // required if paymentMethodID is set
'capture' => TRUE/FALSE,
'amount' => '12.05',
'currency' => 'USD',
......@@ -348,9 +355,12 @@ class CRM_Stripe_PaymentIntent {
$intentParams['capture_method'] = 'manual';
// Setup the card to be saved and used later
$intentParams['setup_future_usage'] = 'off_session';
if ($params['paymentMethodID']) {
if (isset($params['paymentMethodID'])) {
$intentParams['payment_method'] = $params['paymentMethodID'];
}
if (isset($params['customer'])) {
$intentParams['customer'] = $params['customer'];
}
$intent = $this->paymentProcessor->stripeClient->paymentIntents->create($intentParams);
} catch (Exception $e) {
// Save the "error" in the paymentIntent table in in case investigation is required.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment