Skip to content

Commit

Permalink
fix: paypal flow fixed for one click handler (#711)
Browse files Browse the repository at this point in the history
Co-authored-by: Saksham Sharma <[email protected]>
Co-authored-by: Pritish Budhiraja <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2024
1 parent 35b46fc commit 09a8470
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 69 deletions.
140 changes: 72 additions & 68 deletions src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 = (
Expand Down
6 changes: 5 additions & 1 deletion src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 09a8470

Please sign in to comment.