Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: openurl_if_required enhancement #371

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Types/ConfirmType.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type confirmParams = {
return_url: string,
publishableKey: string,
redirect?: string,
prafulkoppalkar marked this conversation as resolved.
Show resolved Hide resolved
}

type confirm = {
Expand All @@ -14,6 +15,7 @@ open Utils
let defaultConfirm = {
return_url: "",
publishableKey: "",
redirect: "if_required",
}
let getConfirmParams = (dict, str) => {
dict
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/Types/PaymentType.res
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ let getConfirmParams = dict => {
{
return_url: dict->getString("return_url", ""),
publishableKey: dict->getString("publishableKey", ""),
redirect: dict->getString("redirect", "if_required"),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ let rec intentCall = (
~sdkHandleOneClickConfirmPayment,
~counter,
~isPaymentSession=false,
~paymentSessionRedirect="if_redirect",
(),
) => {
open Promise
Expand Down Expand Up @@ -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)])
PritishBudhiraja marked this conversation as resolved.
Show resolved Hide resolved

if statusCode->String.charAt(0) !== "2" {
res
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 43 additions & 9 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -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")
prafulkoppalkar marked this conversation as resolved.
Show resolved Hide resolved

let appearance =
localOptions->Dict.get("appearance")->Option.getOr(Dict.make()->JSON.Encode.object)
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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)
prafulkoppalkar marked this conversation as resolved.
Show resolved Hide resolved
})
->ignore
->resolve
Expand All @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions src/orca-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,9 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.

let redirect =
payload
->JSON.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
Expand Down Expand Up @@ -351,6 +350,7 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
[
("return_url", url->JSON.Encode.string),
("publishableKey", publishableKey->JSON.Encode.string),
("redirect", redirect->JSON.Encode.string),
]
->Dict.fromArray
->JSON.Encode.object,
Expand Down
2 changes: 1 addition & 1 deletion src/orca-loader/PaymentSessionMethods.res
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let getCustomerSavedPaymentMethods = (
let confirmParam: ConfirmType.confirmParams = {
return_url: returnUrl,
publishableKey,
redirect,
}

let paymentIntentID = String.split(clientSecret, "_secret_")[0]->Option.getOr("")
Expand Down Expand Up @@ -125,7 +126,6 @@ let getCustomerSavedPaymentMethods = (
~sdkHandleOneClickConfirmPayment=false,
~counter=0,
~isPaymentSession=true,
~paymentSessionRedirect=redirect,
(),
)
}
Expand Down
Loading