From 096c2cb5dc850c17b63ab1433c547e244d3d736c Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja <1805317@kiit.ac.in> Date: Wed, 15 May 2024 15:44:34 +0530 Subject: [PATCH 1/4] fix: openurl_if_required enhancement --- src/Types/ConfirmType.res | 3 ++ src/Types/PaymentType.res | 1 + src/Utilities/PaymentHelpers.res | 4 +- src/orca-loader/Elements.res | 52 +++++++++++++++++++---- src/orca-loader/Hyper.res | 8 ++-- src/orca-loader/PaymentSessionMethods.res | 2 +- 6 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/Types/ConfirmType.res b/src/Types/ConfirmType.res index c7f38bcb6..724a832a6 100644 --- a/src/Types/ConfirmType.res +++ b/src/Types/ConfirmType.res @@ -1,6 +1,7 @@ type confirmParams = { return_url: string, publishableKey: string, + redirect?: string, } type confirm = { @@ -14,6 +15,7 @@ open Utils let defaultConfirm = { return_url: "", publishableKey: "", + redirect: "if_required", } let getConfirmParams = (dict, str) => { dict @@ -23,6 +25,7 @@ let getConfirmParams = (dict, str) => { { return_url: getString(json, "return_url", ""), publishableKey: getString(json, "publishableKey", ""), + redirect: getString(json, "redirect", "if_required"), } }) ->Option.getOr(defaultConfirm) diff --git a/src/Types/PaymentType.res b/src/Types/PaymentType.res index 80529bc9a..2b00dc7c8 100644 --- a/src/Types/PaymentType.res +++ b/src/Types/PaymentType.res @@ -908,6 +908,7 @@ let getConfirmParams = dict => { { return_url: dict->getString("return_url", ""), publishableKey: dict->getString("publishableKey", ""), + redirect: dict->getString("redirect", "if_required"), } } diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index 5aa53d49e..fc9339509 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -320,7 +320,6 @@ let rec intentCall = ( ~sdkHandleOneClickConfirmPayment, ~counter, ~isPaymentSession=false, - ~paymentSessionRedirect="if_redirect", (), ) => { open Promise @@ -357,6 +356,7 @@ let rec intentCall = ( let url = urlSearch(confirmParam.return_url) url.searchParams.set("payment_intent_client_secret", clientSecret) url.searchParams.set("status", "failed") + handlePostMessage([("confirmParamsPostMessage", confirmParam->Identity.anyTypeToJson)]) if statusCode->String.charAt(0) !== "2" { res @@ -516,7 +516,7 @@ let rec intentCall = ( | (Paypal, false) => if !isPaymentSession { postSubmitResponse(~jsonData=data, ~url=url.href) - } else if paymentSessionRedirect === "always" { + } else if confirmParam.redirect === Some("always") { handleOpenUrl(url.href) } else { resolve(data) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 095d1cd29..df1d60a8b 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -28,6 +28,7 @@ let make = ( let savedPaymentElement = Dict.make() let localOptions = options->JSON.Decode.object->Option.getOr(Dict.make()) let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey, ()) + let redirect = ref("if_required") let appearance = localOptions->Dict.get("appearance")->Option.getOr(Dict.make()->JSON.Encode.object) @@ -592,6 +593,10 @@ let make = ( let handlePollStatusMessage = (ev: Types.event) => { let eventDataObject = ev.data->anyTypeToJson let headers = [("Content-Type", "application/json"), ("api-key", publishableKey)] + switch eventDataObject->getOptionalJsonFromJson("confirmParamsPostMessage") { + | Some(obj) => redirect := obj->getDictFromJson->getString("redirect", "if_required") + | None => () + } switch eventDataObject->getOptionalJsonFromJson("poll_status") { | Some(val) => { handlePostMessage([ @@ -623,17 +628,21 @@ let make = ( ~isForceSync=true, ) ->then(json => { - let dict = json->JSON.Decode.object->Option.getOr(Dict.make()) - let status = dict->getString("status", "") - let returnUrl = dict->getString("return_url", "") - Window.Location.replace( - `${returnUrl}?payment_intent_client_secret=${clientSecret}&status=${status}`, - ) - resolve() + if redirect.contents === "always" { + let dict = json->JSON.Decode.object->Option.getOr(Dict.make()) + let status = dict->getString("status", "") + let returnUrl = dict->getString("return_url", "") + Window.Location.replace( + `${returnUrl}?payment_intent_client_secret=${clientSecret}&status=${status}`, + ) + resolve(JSON.Encode.null) + } else { + resolve(json) + } }) - ->catch(_ => { + ->catch(err => { Window.Location.replace(url) - resolve() + resolve(err->Identity.anyTypeToJson) }) ->ignore ->resolve @@ -643,6 +652,31 @@ let make = ( } | None => () } + + switch eventDataObject->getOptionalJsonFromJson("openurl_if_required") { + | Some(val) => + if redirect.contents === "always" { + Window.Location.replace(val->JSON.Decode.string->Option.getOr("")) + resolve(JSON.Encode.null) + } else { + PaymentHelpers.retrievePaymentIntent( + clientSecret, + headers, + ~optLogger=Some(logger), + ~switchToCustomPod, + ~isForceSync=true, + ) + ->then(json => { + resolve(json) + }) + ->catch(err => { + resolve(err->Identity.anyTypeToJson) + }) + ->finally(_ => handlePostMessage([("fullscreen", false->JSON.Encode.bool)])) + }->ignore + + | None => () + } } addSmartEventListener("message", handleApplePayMounted, "onApplePayMount") diff --git a/src/orca-loader/Hyper.res b/src/orca-loader/Hyper.res index fb39f0356..1cce620ed 100644 --- a/src/orca-loader/Hyper.res +++ b/src/orca-loader/Hyper.res @@ -279,10 +279,9 @@ let make = (publishableKey, options: option, analyticsInfo: optionJSON.Decode.object - ->Option.flatMap(x => x->Dict.get("redirect")) - ->Option.flatMap(JSON.Decode.string) - ->Option.getOr("if_required") + ->getDictFromJson + ->getDictfromDict("confirmParams") + ->getString("redirect", "if_required") let url = confirmParams @@ -351,6 +350,7 @@ let make = (publishableKey, options: option, analyticsInfo: optionJSON.Encode.string), ("publishableKey", publishableKey->JSON.Encode.string), + ("redirect", redirect->JSON.Encode.string), ] ->Dict.fromArray ->JSON.Encode.object, diff --git a/src/orca-loader/PaymentSessionMethods.res b/src/orca-loader/PaymentSessionMethods.res index bdf2693ac..0948bd87c 100644 --- a/src/orca-loader/PaymentSessionMethods.res +++ b/src/orca-loader/PaymentSessionMethods.res @@ -70,6 +70,7 @@ let getCustomerSavedPaymentMethods = ( let confirmParam: ConfirmType.confirmParams = { return_url: returnUrl, publishableKey, + redirect, } let paymentIntentID = String.split(clientSecret, "_secret_")[0]->Option.getOr("") @@ -125,7 +126,6 @@ let getCustomerSavedPaymentMethods = ( ~sdkHandleOneClickConfirmPayment=false, ~counter=0, ~isPaymentSession=true, - ~paymentSessionRedirect=redirect, (), ) } From 02e2d2f324574c68b04554197f95176c8ccf74bd Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja <1805317@kiit.ac.in> Date: Fri, 17 May 2024 12:12:13 +0530 Subject: [PATCH 2/4] fix: comments addressed --- src/Utilities/PaymentHelpers.res | 2 +- src/orca-loader/Elements.res | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index 5b56af8e0..b9f0b2259 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -355,7 +355,7 @@ let rec intentCall = ( let url = urlSearch(confirmParam.return_url) url.searchParams.set("payment_intent_client_secret", clientSecret) url.searchParams.set("status", "failed") - handlePostMessage([("confirmParamsPostMessage", confirmParam->Identity.anyTypeToJson)]) + handlePostMessage([("confirmParams", confirmParam->Identity.anyTypeToJson)]) if statusCode->String.charAt(0) !== "2" { res diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index c9c90034d..994af6386 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -589,7 +589,7 @@ let make = ( let handlePollStatusMessage = (ev: Types.event) => { let eventDataObject = ev.data->anyTypeToJson let headers = [("Content-Type", "application/json"), ("api-key", publishableKey)] - switch eventDataObject->getOptionalJsonFromJson("confirmParamsPostMessage") { + switch eventDataObject->getOptionalJsonFromJson("confirmParams") { | Some(obj) => redirect := obj->getDictFromJson->getString("redirect", "if_required") | None => () } From ba72751af894599fb2dcb1164e7f775d04d2fc40 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja <1805317@kiit.ac.in> Date: Fri, 17 May 2024 12:19:18 +0530 Subject: [PATCH 3/4] fix: comments addressed --- src/orca-loader/Elements.res | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index 994af6386..bff0639f5 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -637,7 +637,9 @@ let make = ( } }) ->catch(err => { - Window.Location.replace(url) + if redirect.contents === "always" { + Window.Location.replace(url) + } resolve(err->Identity.anyTypeToJson) }) ->ignore From 5dc0bf9d5aa6a69e9bcf5ad13ddba2b0ba0b80c9 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja <1805317@kiit.ac.in> Date: Fri, 17 May 2024 14:14:20 +0530 Subject: [PATCH 4/4] fix: post message handle --- src/orca-loader/Elements.res | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index bff0639f5..6ec08ba54 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -633,6 +633,11 @@ let make = ( ) resolve(JSON.Encode.null) } else { + handlePostMessage([ + ("fullscreen", false->JSON.Encode.bool), + ("submitSuccessful", true->JSON.Encode.bool), + ("data", json), + ]) resolve(json) } }) @@ -640,6 +645,10 @@ let make = ( if redirect.contents === "always" { Window.Location.replace(url) } + handlePostMessage([ + ("submitSuccessful", false->JSON.Encode.bool), + ("error", err->Identity.anyTypeToJson), + ]) resolve(err->Identity.anyTypeToJson) }) ->ignore @@ -665,9 +674,14 @@ let make = ( ~isForceSync=true, ) ->then(json => { + handlePostMessage([("submitSuccessful", true->JSON.Encode.bool), ("data", json)]) resolve(json) }) ->catch(err => { + handlePostMessage([ + ("submitSuccessful", false->JSON.Encode.bool), + ("error", err->Identity.anyTypeToJson), + ]) resolve(err->Identity.anyTypeToJson) }) ->finally(_ => handlePostMessage([("fullscreen", false->JSON.Encode.bool)]))