From ba50f1c5d998701a4fe89caf2576746fe0bb8ae8 Mon Sep 17 00:00:00 2001 From: Vrishab Srivatsa <136090360+vsrivatsa-juspay@users.noreply.github.com> Date: Wed, 6 Mar 2024 15:26:18 +0530 Subject: [PATCH] fix(intentCall): handling no response on confirm (#203) --- src/Utilities/PaymentHelpers.res | 94 ++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 16 deletions(-) diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index 30bd4b439..c4e7e54f0 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -14,7 +14,8 @@ let closePaymentLoaderIfAny = () => let retrievePaymentIntent = (clientSecret, headers, ~optLogger, ~switchToCustomPod) => { open Promise - let paymentIntentID = Js.String2.split(clientSecret, "_secret_")[0] + let paymentIntentID = + Js.String2.split(clientSecret, "_secret_")->Belt.Array.get(0)->Belt.Option.getWithDefault("") let endpoint = ApiEndpoint.getApiEndPoint() let uri = `${endpoint}/payments/${paymentIntentID}?client_secret=${clientSecret}` @@ -93,7 +94,7 @@ let rec pollRetrievePaymentIntent = (clientSecret, headers, ~optLogger, ~switchT }) } -let intentCall = ( +let rec intentCall = ( ~fetchApi: ( string, ~bodyStr: string=?, @@ -114,6 +115,7 @@ let intentCall = ( ~setIsManualRetryEnabled, ~switchToCustomPod, ~sdkHandleOneClickConfirmPayment, + ~counter, ) => { open Promise let isConfirm = uri->Js.String2.includes("/confirm") @@ -197,10 +199,36 @@ let intentCall = ( ~logCategory=API, (), ) - closePaymentLoaderIfAny() - postFailedSubmitResponse(~errortype="server_error", ~message="Something went wrong") - if handleUserError { - openUrl(url.href) + if counter >= 5 { + closePaymentLoaderIfAny() + postFailedSubmitResponse(~errortype="server_error", ~message="Something went wrong") + if handleUserError { + openUrl(url.href) + } + } else { + let paymentIntentID = + Js.String2.split(clientSecret, "_secret_") + ->Belt.Array.get(0) + ->Belt.Option.getWithDefault("") + let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey=confirmParam.publishableKey, ()) + let retrieveUri = `${endpoint}/payments/${paymentIntentID}?client_secret=${clientSecret}` + intentCall( + ~fetchApi, + ~uri=retrieveUri, + ~headers, + ~bodyStr, + ~confirmParam: ConfirmType.confirmParams, + ~clientSecret, + ~optLogger, + ~handleUserError, + ~paymentType, + ~iframeId, + ~fetchMethod=Get, + ~setIsManualRetryEnabled, + ~switchToCustomPod, + ~sdkHandleOneClickConfirmPayment, + ~counter=counter + 1, + ) } resolve() }) @@ -421,9 +449,6 @@ let intentCall = ( resolve() }) ->catch(err => { - let url = urlSearch(confirmParam.return_url) - url.searchParams.set(. "payment_intent_client_secret", clientSecret) - url.searchParams.set(. "status", "failed") let exceptionMessage = err->Utils.formatException logApi( ~optLogger, @@ -435,10 +460,39 @@ let intentCall = ( ~logCategory=API, (), ) - closePaymentLoaderIfAny() - postFailedSubmitResponse(~errortype="server_error", ~message="Something went wrong") - if handleUserError { - openUrl(url.href) + if counter >= 5 { + let url = urlSearch(confirmParam.return_url) + url.searchParams.set(. "payment_intent_client_secret", clientSecret) + url.searchParams.set(. "status", "failed") + closePaymentLoaderIfAny() + postFailedSubmitResponse(~errortype="server_error", ~message="Something went wrong") + if handleUserError { + openUrl(url.href) + } + } else { + let paymentIntentID = + Js.String2.split(clientSecret, "_secret_") + ->Belt.Array.get(0) + ->Belt.Option.getWithDefault("") + let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey=confirmParam.publishableKey, ()) + let retrieveUri = `${endpoint}/payments/${paymentIntentID}?client_secret=${clientSecret}` + intentCall( + ~fetchApi, + ~uri=retrieveUri, + ~headers, + ~bodyStr, + ~confirmParam: ConfirmType.confirmParams, + ~clientSecret, + ~optLogger, + ~handleUserError, + ~paymentType, + ~iframeId, + ~fetchMethod=Get, + ~setIsManualRetryEnabled, + ~switchToCustomPod, + ~sdkHandleOneClickConfirmPayment, + ~counter=counter + 1, + ) } resolve() }) @@ -453,7 +507,10 @@ let usePaymentSync = (optLogger: option, paymentType: pay (~handleUserError=false, ~confirmParam: ConfirmType.confirmParams, ~iframeId="", ()) => { switch keys.clientSecret { | Some(clientSecret) => - let paymentIntentID = Js.String2.split(clientSecret, "_secret_")[0] + let paymentIntentID = + Js.String2.split(clientSecret, "_secret_") + ->Belt.Array.get(0) + ->Belt.Option.getWithDefault("") let headers = [("Content-Type", "application/json"), ("api-key", confirmParam.publishableKey)] let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey=confirmParam.publishableKey, ()) let uri = `${endpoint}/payments/${paymentIntentID}?force_sync=true&client_secret=${clientSecret}` @@ -470,10 +527,11 @@ let usePaymentSync = (optLogger: option, paymentType: pay ~handleUserError, ~paymentType, ~iframeId, - ~fetchMethod=Fetch.Get, + ~fetchMethod=Get, ~setIsManualRetryEnabled, ~switchToCustomPod, ~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment, + ~counter=0, ) } switch list { @@ -538,7 +596,10 @@ let usePaymentIntent = (optLogger: option, paymentType: p ) => { switch keys.clientSecret { | Some(clientSecret) => - let paymentIntentID = Js.String2.split(clientSecret, "_secret_")[0] + let paymentIntentID = + Js.String2.split(clientSecret, "_secret_") + ->Belt.Array.get(0) + ->Belt.Option.getWithDefault("") let headers = [("Content-Type", "application/json"), ("api-key", confirmParam.publishableKey)] let returnUrlArr = [("return_url", confirmParam.return_url->Js.Json.string)] let manual_retry = isManualRetryEnabled @@ -624,6 +685,7 @@ let usePaymentIntent = (optLogger: option, paymentType: p ~setIsManualRetryEnabled, ~switchToCustomPod, ~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment, + ~counter=0, ) } }