From 09a8470a3b16c410636c1544033aa0f2e76a59b1 Mon Sep 17 00:00:00 2001 From: sakksham7 <130480324+sakksham7@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:14:34 +0530 Subject: [PATCH] fix: paypal flow fixed for one click handler (#711) Co-authored-by: Saksham Sharma Co-authored-by: Pritish Budhiraja --- src/Payments/PaypalSDKHelpers.res | 140 +++++++++++++++--------------- src/Utilities/Utils.res | 6 +- 2 files changed, 77 insertions(+), 69 deletions(-) diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 6db9a71b..71c08e1e 100644 --- a/src/Payments/PaypalSDKHelpers.res +++ b/src/Payments/PaypalSDKHelpers.res @@ -26,17 +26,16 @@ let loadPaypalSDK = ( ~eventName=PAYPAL_SDK_FLOW, ~paymentMethod="PAYPAL", ) - Utils.makeOneClickHandlerPromise(sdkHandleOneClickConfirmPayment) - ->then(result => { - let result = result->JSON.Decode.bool->Option.getOr(false) - if result { - let paypalWrapper = GooglePayType.getElementById(Utils.document, "paypal-button") - paypalWrapper.innerHTML = "" - setIsCompleted(_ => true) - paypal["Buttons"]({ - style: buttonStyle, - fundingSource: paypal["FUNDING"]["PAYPAL"], - createOrder: () => { + let paypalWrapper = GooglePayType.getElementById(Utils.document, "paypal-button") + paypalWrapper.innerHTML = "" + setIsCompleted(_ => true) + paypal["Buttons"]({ + style: buttonStyle, + fundingSource: paypal["FUNDING"]["PAYPAL"], + createOrder: () => { + Utils.makeOneClickHandlerPromise(sdkHandleOneClickConfirmPayment)->then(result => { + let result = result->JSON.Decode.bool->Option.getOr(false) + if result { Utils.messageParentWindow([ ("fullscreen", true->JSON.Encode.bool), ("param", "paymentloader"->JSON.Encode.string), @@ -63,68 +62,73 @@ let loadPaypalSDK = ( ~manualRetry=isManualRetryEnabled, ) }) - }, - onApprove: (_data, actions) => { - if !options.readOnly { - actions.order.get() - ->then(val => { - let purchaseUnit = - val - ->Utils.getDictFromJson - ->Utils.getArray("purchase_units") - ->Array.get(0) - ->Option.flatMap(JSON.Decode.object) - ->Option.getOr(Dict.make()) - let payerDetails = - val - ->Utils.getDictFromJson - ->Dict.get("payer") - ->Option.flatMap(JSON.Decode.object) - ->Option.getOr(Dict.make()) - ->PaymentType.itemToPayerDetailsObjectMapper + } else { + loggerState.setLogInfo( + ~value="Paypal SDK oneClickDoSubmit - false", + ~eventName=PAYPAL_SDK_FLOW, + ~paymentMethod="PAYPAL", + ) + resolve("") + } + }) + }, + onApprove: (_data, actions) => { + if !options.readOnly { + actions.order.get() + ->then(val => { + let purchaseUnit = + val + ->Utils.getDictFromJson + ->Utils.getArray("purchase_units") + ->Array.get(0) + ->Option.flatMap(JSON.Decode.object) + ->Option.getOr(Dict.make()) + let payerDetails = + val + ->Utils.getDictFromJson + ->Dict.get("payer") + ->Option.flatMap(JSON.Decode.object) + ->Option.getOr(Dict.make()) + ->PaymentType.itemToPayerDetailsObjectMapper - let details = purchaseUnit->paypalShippingDetails(payerDetails) - let requiredFieldsBody = DynamicFieldsUtils.getPaypalRequiredFields( - ~details, - ~paymentMethodTypes, - ~statesList=stateJson, - ) + let details = purchaseUnit->paypalShippingDetails(payerDetails) + let requiredFieldsBody = DynamicFieldsUtils.getPaypalRequiredFields( + ~details, + ~paymentMethodTypes, + ~statesList=stateJson, + ) - let bodyArr = - requiredFieldsBody - ->JSON.Encode.object - ->Utils.unflattenObject - ->Utils.getArrayOfTupleFromDict + let bodyArr = + requiredFieldsBody + ->JSON.Encode.object + ->Utils.unflattenObject + ->Utils.getArrayOfTupleFromDict - completeAuthorize( - ~bodyArr, - ~confirmParam={ - return_url: options.wallets.walletReturnUrl, - publishableKey, - }, - ~handleUserError=true, - ) + completeAuthorize( + ~bodyArr, + ~confirmParam={ + return_url: options.wallets.walletReturnUrl, + publishableKey, + }, + ~handleUserError=true, + ) - resolve() - }) - ->ignore - } - }, - onCancel: _data => { - handleCloseLoader() - }, - onError: _err => { - handleCloseLoader() - }, - }).render("#paypal-button") - areOneClickWalletsRendered(prev => { - ...prev, - isPaypal: true, - }) - } - resolve() + resolve() + }) + ->ignore + } + }, + onCancel: _data => { + handleCloseLoader() + }, + onError: _err => { + handleCloseLoader() + }, + }).render("#paypal-button") + areOneClickWalletsRendered(prev => { + ...prev, + isPaypal: true, }) - ->ignore } let loadBraintreePaypalSdk = ( diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 016f92d1..a571fd0f 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -1214,7 +1214,11 @@ let makeOneClickHandlerPromise = sdkHandleOneClickConfirmPayment => { resolve(JSON.Encode.bool(true)) } else { let handleMessage = (event: Types.event) => { - let json = event.data->anyTypeToJson->getStringFromJson("")->safeParse + let json = try { + event.data->anyTypeToJson + } catch { + | _ => JSON.Encode.null + } let dict = json->getDictFromJson if dict->Dict.get("oneClickDoSubmit")->Option.isSome {