From ba05e1e12f3c5c9bfbadff5fb02300ab7435d427 Mon Sep 17 00:00:00 2001 From: Westley Russell Date: Wed, 15 Feb 2023 13:41:18 -0500 Subject: [PATCH 1/3] feat: (WIP) adding 3ds to stripe elements --- src/payment/payment-methods/stripe/service.js | 92 +++++++++++++------ 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/src/payment/payment-methods/stripe/service.js b/src/payment/payment-methods/stripe/service.js index e20369080..32ca94512 100644 --- a/src/payment/payment-methods/stripe/service.js +++ b/src/payment/payment-methods/stripe/service.js @@ -75,34 +75,70 @@ export default async function checkout( { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }, - ) - .then(response => { - setLocation(response.data.receipt_page_url); - }) - .catch(error => { + ) + .then(response => { + console.log(response); + handleServerResponse(response, stripe, setLocation, postData); + }) + .catch(error => { const errorData = error.response ? error.response.data : null; - if (errorData && error.response.data.sdn_check_failure) { - /* istanbul ignore next */ - if (getConfig().ENVIRONMENT !== 'test') { - // SDN failure: redirect to Ecommerce SDN error page. - setLocation(`${getConfig().ECOMMERCE_BASE_URL}/payment/sdn/failure/`); + if (errorData && error.response.data.sdn_check_failure) { + /* istanbul ignore next */ + if (getConfig().ENVIRONMENT !== 'test') { + // SDN failure: redirect to Ecommerce SDN error page. + setLocation(`${getConfig().ECOMMERCE_BASE_URL}/payment/sdn/failure/`); + } + logError(error, { + messagePrefix: 'SDN Check Error', + paymentMethod: 'Stripe', + paymentErrorType: 'SDN Check Submit Api', + basketId, + }); + throw new Error('This card holder did not pass the SDN check.'); + } else { + // Log error and tell user. + logError(error, { + messagePrefix: 'Stripe Submit Error', + paymentMethod: 'Stripe', + paymentErrorType: 'Submit Error', + basketId, + }); + handleApiError(error); } - logError(error, { - messagePrefix: 'SDN Check Error', - paymentMethod: 'Stripe', - paymentErrorType: 'SDN Check Submit Api', - basketId, - }); - throw new Error('This card holder did not pass the SDN check.'); - } else { - // Log error and tell user. - logError(error, { - messagePrefix: 'Stripe Submit Error', - paymentMethod: 'Stripe', - paymentErrorType: 'Submit Error', - basketId, - }); - handleApiError(error); - } - }); + }); +} + +const handleServerResponse = async (response, stripe, setLocation, postData) => { + + if (response.requires_action) { + console.log('b'); + const { error: errorAction, paymentIntent } = await stripe.handleNextAction({clientSecret: response.payment_intent_client_secret}); + console.log('c'); + console.log(paymentIntent); + if (errorAction) { + console.log(errorAction); + + } + else { + console.log('Actions handled success'); + await getAuthenticatedHttpClient() + .post( + `${process.env.STRIPE_RESPONSE_URL}`, + postData, + { + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + }, + ) + .then(response => { + console.log(response); + handleServerResponse(response, stripe, setLocation); + }) + + } + + } + else{ + //simple payment successful + setLocation(response.data.receipt_page_url); + } } From 361a8370ae043194688b882aedf26ba9d46a1954 Mon Sep 17 00:00:00 2001 From: Westley Russell Date: Tue, 21 Feb 2023 10:52:58 -0500 Subject: [PATCH 2/3] docs: updated comments and removed console logs --- src/payment/payment-methods/stripe/service.js | 125 +++++++++--------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/src/payment/payment-methods/stripe/service.js b/src/payment/payment-methods/stripe/service.js index 32ca94512..19c321917 100644 --- a/src/payment/payment-methods/stripe/service.js +++ b/src/payment/payment-methods/stripe/service.js @@ -8,6 +8,39 @@ import { handleApiError } from '../../data/handleRequestError'; ensureConfig(['ECOMMERCE_BASE_URL', 'STRIPE_RESPONSE_URL'], 'Stripe API service'); +const handleServerResponse = async (response, stripe, setLocation, postData) => { + if (response.data.requiresAction) { + // Call stripe handleNextAction to open the 3ds authentication page + const { error: errorAction, paymentIntent } = await stripe.handleNextAction( + { clientSecret: response.data.clientSecret }, + ); + if (errorAction) { + logError(errorAction, { + messagePrefix: 'Stripe 3DS Error', + paymentMethod: 'Stripe', + paymentErrorType: '3DS Error', + }); + handleApiError(errorAction); + } else { + // If 3DS is successful tell ecommerce to finish the payment + await getAuthenticatedHttpClient() + .post( + `${process.env.STRIPE_RESPONSE_URL}`, + postData, + { + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + }, + ) + .then(actionsResponse => { + handleServerResponse(actionsResponse, stripe, setLocation, postData); + }); + } + } else { + // If no actions needed go to receipt page + setLocation(response.data.receipt_page_url); + } +}; + /** * Checkout with Stripe * @@ -75,70 +108,34 @@ export default async function checkout( { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }, - ) - .then(response => { - console.log(response); - handleServerResponse(response, stripe, setLocation, postData); - }) - .catch(error => { + ) + .then(response => { + handleServerResponse(response, stripe, setLocation, postData); + }) + .catch(error => { const errorData = error.response ? error.response.data : null; - if (errorData && error.response.data.sdn_check_failure) { - /* istanbul ignore next */ - if (getConfig().ENVIRONMENT !== 'test') { - // SDN failure: redirect to Ecommerce SDN error page. - setLocation(`${getConfig().ECOMMERCE_BASE_URL}/payment/sdn/failure/`); - } - logError(error, { - messagePrefix: 'SDN Check Error', - paymentMethod: 'Stripe', - paymentErrorType: 'SDN Check Submit Api', - basketId, - }); - throw new Error('This card holder did not pass the SDN check.'); - } else { - // Log error and tell user. - logError(error, { - messagePrefix: 'Stripe Submit Error', - paymentMethod: 'Stripe', - paymentErrorType: 'Submit Error', - basketId, - }); - handleApiError(error); + if (errorData && error.response.data.sdn_check_failure) { + /* istanbul ignore next */ + if (getConfig().ENVIRONMENT !== 'test') { + // SDN failure: redirect to Ecommerce SDN error page. + setLocation(`${getConfig().ECOMMERCE_BASE_URL}/payment/sdn/failure/`); } - }); -} - -const handleServerResponse = async (response, stripe, setLocation, postData) => { - - if (response.requires_action) { - console.log('b'); - const { error: errorAction, paymentIntent } = await stripe.handleNextAction({clientSecret: response.payment_intent_client_secret}); - console.log('c'); - console.log(paymentIntent); - if (errorAction) { - console.log(errorAction); - - } - else { - console.log('Actions handled success'); - await getAuthenticatedHttpClient() - .post( - `${process.env.STRIPE_RESPONSE_URL}`, - postData, - { - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - }, - ) - .then(response => { - console.log(response); - handleServerResponse(response, stripe, setLocation); - }) - - } - - } - else{ - //simple payment successful - setLocation(response.data.receipt_page_url); - } + logError(error, { + messagePrefix: 'SDN Check Error', + paymentMethod: 'Stripe', + paymentErrorType: 'SDN Check Submit Api', + basketId, + }); + throw new Error('This card holder did not pass the SDN check.'); + } else { + // Log error and tell user. + logError(error, { + messagePrefix: 'Stripe Submit Error', + paymentMethod: 'Stripe', + paymentErrorType: 'Submit Error', + basketId, + }); + handleApiError(error); + } + }); } From 78bbb18a461b2c963faa64cc316ea3bd0e543c91 Mon Sep 17 00:00:00 2001 From: Westley Russell Date: Tue, 21 Feb 2023 10:53:27 -0500 Subject: [PATCH 3/3] docs: updated comments and removed console logs fixed --- src/payment/payment-methods/stripe/service.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/payment/payment-methods/stripe/service.js b/src/payment/payment-methods/stripe/service.js index 19c321917..9558a6adf 100644 --- a/src/payment/payment-methods/stripe/service.js +++ b/src/payment/payment-methods/stripe/service.js @@ -8,7 +8,7 @@ import { handleApiError } from '../../data/handleRequestError'; ensureConfig(['ECOMMERCE_BASE_URL', 'STRIPE_RESPONSE_URL'], 'Stripe API service'); -const handleServerResponse = async (response, stripe, setLocation, postData) => { +const handleServerResponse = async (response, stripe, setLocation, skus) => { if (response.data.requiresAction) { // Call stripe handleNextAction to open the 3ds authentication page const { error: errorAction, paymentIntent } = await stripe.handleNextAction( @@ -22,6 +22,11 @@ const handleServerResponse = async (response, stripe, setLocation, postData) => }); handleApiError(errorAction); } else { + const postData = formurlencoded({ + payment_intent_id: paymentIntent.id, + skus, + }); + // TODO: move this out as part of REV-3187 to confirm based on where ecommerce redirects. // If 3DS is successful tell ecommerce to finish the payment await getAuthenticatedHttpClient() .post( @@ -32,7 +37,7 @@ const handleServerResponse = async (response, stripe, setLocation, postData) => }, ) .then(actionsResponse => { - handleServerResponse(actionsResponse, stripe, setLocation, postData); + handleServerResponse(actionsResponse, stripe, setLocation, skus); }); } } else { @@ -110,7 +115,7 @@ export default async function checkout( }, ) .then(response => { - handleServerResponse(response, stripe, setLocation, postData); + handleServerResponse(response, stripe, setLocation, skus); }) .catch(error => { const errorData = error.response ? error.response.data : null;