diff --git a/src/Resources/views/payWithPaypal.html.twig b/src/Resources/views/payWithPaypal.html.twig index ef09d17a..e164e4d1 100644 --- a/src/Resources/views/payWithPaypal.html.twig +++ b/src/Resources/views/payWithPaypal.html.twig @@ -329,6 +329,8 @@ }); if (paypal.HostedFields.isEligible() === true) { + let processingOrderId; + paypal.HostedFields.render({ createOrder: function(data, actions) { document.querySelector('#paypal-payment-container').classList.add('loading'); @@ -339,6 +341,8 @@ }).then(function(res) { return res.json(); }).then(function(data) { + processingOrderId = data.orderID; + return data.orderID; }); }, @@ -419,6 +423,7 @@ if (formValid) { hostedFields.submit({ + contingencies: ['SCA_ALWAYS'], cardholderName: document.getElementById('card-holder-name').value, billingAddress: { streetAddress: document.getElementById('card-billing-address-street').value, @@ -428,20 +433,35 @@ countryCodeAlpha2: document.getElementById('card-billing-address-country').value } }).then(payload => { - return fetch(completePayPalOrderUrl, { - method: 'post' - }).then(function(res) { - return res.json(); + if (payload.authenticationReason == 'SUCCESSFUL' && payload.authenticationStatus == 'YES') { + return fetch(completePayPalOrderUrl, { + method: 'post' + }).then(function(res) { + return res.json(); + }).then(function(data) { + if (data.status == 'processing') { + return fetch(cancelPayPalPaymentUrl, { + method: 'post', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ payPalOrderId: data.orderID }) + }).then(window.location.reload()); + } + + window.location.href = data.return_url; + }); + } + + + return fetch(errorPayPalPaymentUrl, { + method: 'post', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify('Invalid 3D Secure authentication.') }).then(function(data) { - if (data.status == 'processing') { - return fetch(cancelPayPalPaymentUrl, { - method: 'post', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify({ payPalOrderId: data.orderID }) - }).then(window.location.reload()); - } - - window.location.href = data.return_url; + return fetch(cancelPayPalPaymentUrl, { + method: 'post', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ payPalOrderId: processingOrderId }) + }).then(window.location.reload()); }); }); } else {