Skip to content
Snippets Groups Projects
Commit 7277706a authored by mattwire's avatar mattwire Committed by mattwire
Browse files

Refactor and fix error handling in response to StripePaymentintent.Process API

parent e18e0edc
Branches
Tags
1 merge request!1526.6 to master
......@@ -215,7 +215,7 @@ function civicrm_api3_stripe_paymentintent_process($params) {
elseif ($e instanceof \Stripe\Exception\InvalidRequestException) {
$message = 'Invalid request';
}
return civicrm_api3_create_error(['message' => $message]);
return civicrm_api3_create_error($message);
}
}
......@@ -236,7 +236,7 @@ function civicrm_api3_stripe_paymentintent_process($params) {
// Tell the client to handle the action
return civicrm_api3_create_success([
'requires_action' => true,
'paymentintent_client_secret' => $intent->client_secret,
'paymentIntentClientSecret' => $intent->client_secret,
]);
}
elseif (($intent->status === 'requires_capture') || ($intent->status === 'requires_confirmation')) {
......@@ -257,7 +257,7 @@ function civicrm_api3_stripe_paymentintent_process($params) {
elseif ($intent->status === 'requires_payment_method') {
return civicrm_api3_create_success([
'requires_payment_method' => true,
'paymentintent_client_secret' => $intent->client_secret,
'paymentIntentClientSecret' => $intent->client_secret,
]);
}
else {
......
......@@ -51,7 +51,7 @@
handlePaymentIntentAction: function(response) {
switch (CRM.vars.stripe.paymentIntentMethod) {
case 'automatic':
confirm.stripe.handleCardPayment(response.payment_intent_client_secret)
confirm.stripe.handleCardPayment(response.paymentIntentClientSecret)
.then(function(result) {
if (result.error) {
// Show error in payment form
......@@ -66,7 +66,7 @@
break;
case 'manual':
confirm.stripe.handleCardAction(response.payment_intent_client_secret)
confirm.stripe.handleCardAction(response.paymentIntentClientSecret)
.then(function(result) {
if (result.error) {
// Show error in payment form
......
......@@ -131,6 +131,7 @@
}
triggerEvent('crmBillingFormNotValid');
if (notify) {
swalClose();
swalFire({
icon: 'error',
text: errorMessage,
......@@ -233,7 +234,7 @@
}
}, '', false);
CRM.api3('StripePaymentintent', 'Process', {
payment_method_id: result.paymentMethod.id,
payment_method_id: createPaymentMethodResult.paymentMethod.id,
amount: CRM.payment.getTotalAmount().toFixed(2),
currency: CRM.payment.getCurrency(CRM.vars.stripe.currency),
id: CRM.vars.stripe.id,
......@@ -241,14 +242,43 @@
csrfToken: CRM.vars.stripe.csrfToken,
extra_data: CRM.payment.getBillingEmail() + CRM.payment.getBillingName()
})
.done(function (result) {
// Handle server response (see Step 3)
.done(function (paymentIntentProcessResponse) {
swalClose();
handleServerResponse(result);
debugging('StripePaymentintent.Process done');
if (paymentIntentProcessResponse.is_error) {
// Triggered for api3_create_error or Exception
displayError(paymentIntentProcessResponse.error_message, true);
}
else {
paymentIntentProcessResponse = paymentIntentProcessResponse.values;
if (paymentIntentProcessResponse.requires_action) {
// Use Stripe.js to handle a pending card action (eg. 3d-secure)
paymentData.clientSecret = paymentIntentProcessResponse.paymentIntentClientSecret;
stripe.handleCardAction(paymentData.clientSecret)
.then(function(cardActionResult) {
if (cardActionResult.error) {
// Show error in payment form
displayError(cardActionResult.error.message, true);
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
successHandler('paymentIntentID', cardActionResult.paymentIntent);
}
});
}
else {
// All good, we can submit the form
successHandler('paymentIntentID', paymentIntentProcessResponse.paymentIntent);
}
}
})
.fail(function() {
swalClose();
displayError('Unknown error', true);
.fail(function(object) {
// Triggered when http code !== 200 (eg. 400 Bad request)
var error = 'Unknown error';
if (object.hasOwnProperty('statusText')) {
error = object.statusText;
}
displayError(error, true);
});
}
}
......@@ -274,55 +304,32 @@
id: CRM.vars.stripe.id,
description: document.title,
csrfToken: CRM.vars.stripe.csrfToken
//metadata: {integration_check: accept_a_payment}
})
.done(function(result) {
// Handle server response (see Step 3)
.done(function(paymentIntentProcessResponse) {
swalClose();
// Trigger the paymentRequest dialog
paymentData.clientSecret = result.values.payment_intent_client_secret;
paymentData.paymentRequest.show();
// From here the on 'paymentmethod' of the paymentRequest handles completion/failure
debugging('StripePaymentintent.Process done');
if (paymentIntentProcessResponse.is_error) {
// Triggered for api3_create_error or Exception
displayError(paymentIntentProcessResponse.error_message, true);
}
else {
paymentIntentProcessResponse = paymentIntentProcessResponse.values;
// Trigger the paymentRequest dialog
paymentData.clientSecret = paymentIntentProcessResponse.paymentIntentClientSecret;
paymentData.paymentRequest.show();
// From here the on 'paymentmethod' of the paymentRequest handles completion/failure
}
})
.fail(function(result) {
.fail(function(object) {
// Triggered when http code !== 200 (eg. 400 Bad request)
var error = 'Unknown error';
if (result.hasOwnProperty('statusText')) {
error = result.statusText;
if (object.hasOwnProperty('statusText')) {
error = object.statusText;
}
swalClose();
displayError(error, true);
});
}
function handleServerResponse(result) {
debugging('handleServerResponse');
if (result.error) {
// Show error from server on payment form
displayError(result.error.message, true);
} else if (result.requires_action) {
// Use Stripe.js to handle required card action
handleAction(result);
} else {
// All good, we can submit the form
successHandler('paymentIntentID', result.paymentIntent);
}
}
function handleAction(result) {
paymentData.clientSecret = result.payment_intent_client_secret;
stripe.handleCardAction(paymentData.clientSecret)
.then(function(cardActionResult) {
if (cardActionResult.error) {
// Show error in payment form
displayError(cardActionResult.error.message, true);
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
successHandler('paymentIntentID', cardActionResult.paymentIntent);
}
});
}
/**
* Payment processor is not Stripe - cleanup
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment