Skip to content

Commit

Permalink
fix: applePay Dynamic Fields Error Handling and Dynamic Fields Postal…
Browse files Browse the repository at this point in the history
… Code Error
  • Loading branch information
ArushKapoorJuspay committed Mar 22, 2024
1 parent e930bf5 commit 384e63a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/Payments/ApplePay.res
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ let make = (
None
}, (isApplePayReady, isInvokeSDKFlow, paymentExperience, isWallet))

let submitCallback = (ev: Window.event) => {
let submitCallback = React.useCallback2((ev: Window.event) => {
if !isWallet {
let json = ev.data->JSON.parseExn
let confirm = json->getDictFromJson->ConfirmType.itemToObjMapper
Expand All @@ -353,7 +353,7 @@ let make = (
)
}
}
}
}, (areRequiredFieldsValid, areRequiredFieldsEmpty))
useSubmitPaymentData(submitCallback)

{
Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/DynamicFieldsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ let useRequiredFieldsEmptyAndValid = (
| StateAndCity => state.value !== "" && city.value !== ""
| CountryAndPincode(countryArr) =>
(country !== "" || countryArr->Array.length === 0) &&
postalCode.isValid->Option.getOr(false)
postalCode.value !== ""

| AddressCity => city.value !== ""
| AddressPincode => postalCode.isValid->Option.getOr(false)
| AddressPincode => postalCode.value !== ""
| AddressState => state.value !== ""
| BlikCode => blikCode.value !== ""
| Currency(currencyArr) => currency !== "" || currencyArr->Array.length === 0
Expand Down
93 changes: 56 additions & 37 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -529,49 +529,68 @@ let make = (
| _ => ()
}
} else {
let paymentRequest =
applePayPresent
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
->Dict.get("payment_request_data")
->Option.getOr(Dict.make()->JSON.Encode.object)
->Utils.transformKeys(Utils.CamelCase)

let ssn = applePaySession(3, paymentRequest)
switch applePaySessionRef.contents->Nullable.toOption {
| Some(session) =>
try {
session.abort()
} catch {
| error => Console.log2("Abort fail", error)
}
| None => ()
}

ssn.begin()
applePaySessionRef := ssn->Nullable.make

ssn.onvalidatemerchant = _event => {
let merchantSession =
try {
let paymentRequest =
applePayPresent
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
->Dict.get("session_token_data")
->Dict.get("payment_request_data")
->Option.getOr(Dict.make()->JSON.Encode.object)
->Utils.transformKeys(Utils.CamelCase)
ssn.completeMerchantValidation(merchantSession)
}

ssn.onpaymentauthorized = event => {
ssn.completePayment({"status": ssn.\"STATUS_SUCCESS"}->objToJson)
applePaySessionRef := Nullable.null
processPayment(event.payment.token)
}
ssn.oncancel = _ev => {
let msg = [("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray
mountedIframeRef->Window.iframePostMessage(msg)
applePaySessionRef := Nullable.null
Utils.logInfo(Console.log("Apple Pay payment cancelled"))
let ssn = applePaySession(3, paymentRequest)
switch applePaySessionRef.contents->Nullable.toOption {
| Some(session) =>
try {
session.abort()
} catch {
| error => Console.log2("Abort fail", error)
}
| None => ()
}

applePaySessionRef := ssn->Nullable.make

ssn.onvalidatemerchant = _event => {
let merchantSession =
applePayPresent
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
->Dict.get("session_token_data")
->Option.getOr(Dict.make()->JSON.Encode.object)
->Utils.transformKeys(Utils.CamelCase)
ssn.completeMerchantValidation(merchantSession)
}

ssn.onpaymentauthorized = event => {
ssn.completePayment({"status": ssn.\"STATUS_SUCCESS"}->objToJson)
applePaySessionRef := Nullable.null
processPayment(event.payment.token)
}
ssn.oncancel = _ev => {
let msg =
[("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray
mountedIframeRef->Window.iframePostMessage(msg)
applePaySessionRef := Nullable.null
Utils.logInfo(Console.log("Apple Pay payment cancelled"))
}

ssn.begin()
} catch {
| exn => {
logger.setLogInfo(
~value=exn->Utils.formatException->JSON.stringify,
~eventName=APPLE_PAY_FLOW,
~paymentMethod="APPLE_PAY",
(),
)
Utils.logInfo(Console.error2("Apple Pay Error", exn))

let msg =
[("showApplePayButton", true->JSON.Encode.bool)]->Dict.fromArray
mountedIframeRef->Window.iframePostMessage(msg)
applePaySessionRef := Nullable.null
}
}
}
} else {
Expand Down

0 comments on commit 384e63a

Please sign in to comment.