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

Refactor civicrmStripeConfirm.js to exist at CRM.payment.stripeConfirm

parent 35eadf83
No related branches found
No related tags found
1 merge request!1146.4.1
/** /**
* This handles confirmation actions on the "Thankyou" pages for contribution/event workflows. * This handles confirmation actions on the "Thankyou" pages for
* contribution/event workflows.
*/ */
CRM.$(function($) { (function($, CRM, _, undefined) {
debugging("civicrmStripeConfirm loaded");
if (typeof CRM.vars.stripe === 'undefined') { var confirm = {
debugging('CRM.vars.stripe not defined! Not a Stripe processor?'); scriptName: 'stripeconfirm',
return; stripe: null,
} stripeLoading: false,
switch (CRM.vars.stripe.paymentIntentStatus) {
case 'succeeded':
case 'cancelled':
debugging('paymentIntent: ' + CRM.vars.stripe.paymentIntentStatus);
return;
}
checkAndLoad(); handleServerResponse: function(result) {
CRM.payment.debugging(confirm.scriptName, 'handleServerResponse');
if (result.error) {
// Show error from server on payment form
CRM.payment.debugging(confirm.scriptName, result.error.message);
}
else
if (result.requires_action) {
// Use Stripe.js to handle required card action
confirm.handleAction(result);
}
else {
// All good, nothing more to do
CRM.payment.debugging(confirm.scriptName, 'success - payment captured');
}
},
if (typeof stripe === 'undefined') { handleAction: function(response) {
stripe = Stripe(CRM.vars.stripe.publishableKey); switch (CRM.vars.stripe.paymentIntentMethod) {
} case 'automatic':
confirm.stripe.handleCardPayment(response.payment_intent_client_secret)
.then(function (result) {
if (result.error) {
// Show error in payment form
confirm.handleCardConfirm();
}
else {
// The card action has been handled
CRM.payment.debugging(confirm.scriptName, 'card payment success');
confirm.handleCardConfirm();
}
});
break;
handleCardConfirm(); case 'manual':
confirm.stripe.handleCardAction(response.payment_intent_client_secret)
.then(function (result) {
if (result.error) {
// Show error in payment form
confirm.handleCardConfirm();
}
else {
// The card action has been handled
CRM.payment.debugging(confirm.scriptName, 'card action success');
confirm.handleCardConfirm();
}
});
break;
}
},
// On initial load... handleCardConfirm: function() {
var stripe; CRM.payment.debugging(confirm.scriptName, 'handle card confirm');
var stripeLoading = false; // Send paymentMethod.id to server
var url = CRM.url('civicrm/stripe/confirm-payment');
$.post(url, {
payment_intent_id: CRM.vars.stripe.paymentIntentID,
capture: true,
id: CRM.vars.stripe.id,
description: document.title,
csrfToken: CRM.vars.stripe.csrfToken,
}).then(function (result) {
// Handle server response (see Step 3)
confirm.handleServerResponse(result);
});
},
// Disable the browser "Leave Page Alert" which is triggered because we mess with the form submit function. checkAndLoad: function () {
window.onbeforeunload = null; if (typeof Stripe === 'undefined') {
if (confirm.stripeLoading) {
return;
}
confirm.stripeLoading = true;
CRM.payment.debugging(confirm.scriptName, 'Stripe.js is not loaded!');
function handleServerResponse(result) { $.getScript("https://js.stripe.com/v3", function () {
debugging('handleServerResponse'); CRM.payment.debugging(confirm.scriptName, 'Script loaded and executed.');
if (result.error) { confirm.stripeLoading = false;
// Show error from server on payment form
// displayError(result);
} else if (result.requires_action) {
// Use Stripe.js to handle required card action
handleAction(result);
} else {
// All good, nothing more to do
debugging('success - payment captured');
}
}
function handleAction(response) { if (confirm.stripe === null) {
switch (CRM.vars.stripe.paymentIntentMethod) { confirm.stripe = Stripe(CRM.vars.stripe.publishableKey);
case 'automatic': }
stripe.handleCardPayment(response.payment_intent_client_secret) });
.then(function (result) { }
if (result.error) { },
// Show error in payment form };
handleCardConfirm();
}
else {
// The card action has been handled
debugging('card payment success');
handleCardConfirm();
}
});
break;
case 'manual': if (typeof CRM.payment === 'undefined') {
stripe.handleCardAction(response.payment_intent_client_secret) CRM.payment = {};
.then(function (result) {
if (result.error) {
// Show error in payment form
handleCardConfirm();
}
else {
// The card action has been handled
debugging('card action success');
handleCardConfirm();
}
});
break;
}
} }
function handleCardConfirm() { CRM.payment.stripeConfirm = confirm;
debugging('handle card confirm');
// Send paymentMethod.id to server
var url = CRM.url('civicrm/stripe/confirm-payment');
$.post(url, {
payment_intent_id: CRM.vars.stripe.paymentIntentID,
capture: true,
id: CRM.vars.stripe.id,
}).then(function (result) {
// Handle server response (see Step 3)
handleServerResponse(result);
});
}
function checkAndLoad() { CRM.payment.debugging(confirm.scriptName, 'civicrmStripeConfirm loaded');
if (typeof Stripe === 'undefined') {
if (stripeLoading) {
return;
}
stripeLoading = true;
debugging('Stripe.js is not loaded!');
$.getScript("https://js.stripe.com/v3", function () { if (typeof CRM.vars.stripe === 'undefined') {
debugging("Script loaded and executed."); CRM.payment.debugging(confirm.scriptName, 'CRM.vars.stripe not defined! Not a Stripe processor?');
stripeLoading = false; return;
});
}
} }
switch (CRM.vars.stripe.paymentIntentStatus) {
function debugging(errorCode) { case 'succeeded':
// Uncomment the following to debug unexpected returns. case 'cancelled':
if ((typeof(CRM.vars.stripe) === 'undefined') || (Boolean(CRM.vars.stripe.jsDebug) === true)) { CRM.payment.debugging(confirm.scriptName, 'paymentIntent: ' + CRM.vars.stripe.paymentIntentStatus);
console.log(new Date().toISOString() + ' civicrm_stripe.js: ' + errorCode); return;
}
} }
}); confirm.checkAndLoad();
confirm.handleCardConfirm();
// Disable the browser "Leave Page Alert" which is triggered because we mess with the form submit function.
window.onbeforeunload = null;
}(jQuery, CRM, _));
...@@ -162,7 +162,7 @@ function stripe_civicrm_buildForm($formName, &$form) { ...@@ -162,7 +162,7 @@ function stripe_civicrm_buildForm($formName, &$form) {
'path' => \Civi::resources()->getPath(E::LONG_NAME, 'js/civicrmStripeConfirm.js'), 'path' => \Civi::resources()->getPath(E::LONG_NAME, 'js/civicrmStripeConfirm.js'),
'mimetype' => 'application/javascript', 'mimetype' => 'application/javascript',
] ]
)); ), -1000);
// This is a fairly nasty way of matching and retrieving our paymentIntent as it is no longer available. // This is a fairly nasty way of matching and retrieving our paymentIntent as it is no longer available.
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String'); $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment