Skip to content

Commit

Permalink
fix(intentCall): handling no response on confirm (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsrivatsa-edinburgh authored Mar 6, 2024
1 parent e17cbfb commit ba50f1c
Showing 1 changed file with 78 additions and 16 deletions.
94 changes: 78 additions & 16 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

Expand Down Expand Up @@ -93,7 +94,7 @@ let rec pollRetrievePaymentIntent = (clientSecret, headers, ~optLogger, ~switchT
})
}

let intentCall = (
let rec intentCall = (
~fetchApi: (
string,
~bodyStr: string=?,
Expand All @@ -114,6 +115,7 @@ let intentCall = (
~setIsManualRetryEnabled,
~switchToCustomPod,
~sdkHandleOneClickConfirmPayment,
~counter,
) => {
open Promise
let isConfirm = uri->Js.String2.includes("/confirm")
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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,
Expand All @@ -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()
})
Expand All @@ -453,7 +507,10 @@ let usePaymentSync = (optLogger: option<OrcaLogger.loggerMake>, 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}`
Expand All @@ -470,10 +527,11 @@ let usePaymentSync = (optLogger: option<OrcaLogger.loggerMake>, paymentType: pay
~handleUserError,
~paymentType,
~iframeId,
~fetchMethod=Fetch.Get,
~fetchMethod=Get,
~setIsManualRetryEnabled,
~switchToCustomPod,
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
)
}
switch list {
Expand Down Expand Up @@ -538,7 +596,10 @@ let usePaymentIntent = (optLogger: option<OrcaLogger.loggerMake>, 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
Expand Down Expand Up @@ -624,6 +685,7 @@ let usePaymentIntent = (optLogger: option<OrcaLogger.loggerMake>, paymentType: p
~setIsManualRetryEnabled,
~switchToCustomPod,
~sdkHandleOneClickConfirmPayment=keys.sdkHandleOneClickConfirmPayment,
~counter=0,
)
}
}
Expand Down

0 comments on commit ba50f1c

Please sign in to comment.