Skip to content

Commit

Permalink
refactor: Paypal Flow Refactor (#736)
Browse files Browse the repository at this point in the history
Co-authored-by: Pritish Budhiraja <[email protected]>
  • Loading branch information
ImSagnik007 and PritishBudhiraja authored Oct 15, 2024
1 parent ba47f2f commit 07c9612
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Components/SavedPaymentManagement.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let make = (~savedMethods: array<PaymentType.customerMethods>, ~setSavedMethods)
let logger = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom)

let removeSavedMethod = (
savedMethods: array<OrcaPaymentPage.PaymentType.customerMethods>,
savedMethods: array<PaymentType.customerMethods>,
paymentMethodId,
) => {
savedMethods->Array.filter(savedMethod => {
Expand Down
10 changes: 7 additions & 3 deletions src/Payments/PaypalSDK.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =

let token = sessionObj.token
let orderDetails = sessionObj.orderDetails->getOrderDetails(paymentType)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Paypal)
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(RecoilAtoms.isManualRetryEnabled)
let intent = PaymentHelpers.usePostSessionTokens(Some(loggerState), Paypal, Wallet)
let confirm = PaymentHelpers.usePaymentIntent(Some(loggerState), Paypal)
let sessions = Recoil.useRecoilValueFromAtom(RecoilAtoms.sessions)
let completeAuthorize = PaymentHelpers.useCompleteAuthorize(Some(loggerState), Paypal)
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(RecoilAtoms.isManualRetryEnabled)
let checkoutScript =
Window.document(Window.window)->Window.getElementById("braintree-checkout")->Nullable.toOption
let clientScript =
Expand Down Expand Up @@ -84,18 +86,20 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
~iframeId,
~paymentMethodListValue,
~isGuestCustomer,
~intent,
~postSessionTokens=intent,
~isManualRetryEnabled,
~options,
~publishableKey,
~paymentMethodTypes,
~stateJson,
~confirm,
~completeAuthorize,
~handleCloseLoader,
~areOneClickWalletsRendered,
~setIsCompleted,
~isCallbackUsedVal,
~sdkHandleIsThere,
~sessions,
)
})
Window.body->Window.appendChild(paypalScript)
Expand Down
111 changes: 87 additions & 24 deletions src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
open PaypalSDKTypes
open Promise
open Utils

let loadPaypalSDK = (
~loggerState: OrcaLogger.loggerMake,
~sdkHandleOneClickConfirmPayment,
~sdkHandleOneClickConfirmPayment as _,
~buttonStyle,
~iframeId,
~isManualRetryEnabled,
~paymentMethodListValue,
~isGuestCustomer,
~intent: PaymentHelpers.paymentIntent,
~postSessionTokens: PaymentHelpers.paymentIntent,
~options: PaymentType.options,
~publishableKey,
~paymentMethodTypes,
~stateJson,
~confirm: PaymentHelpers.paymentIntent,
~completeAuthorize: PaymentHelpers.completeAuthorize,
~handleCloseLoader,
~areOneClickWalletsRendered: (
RecoilAtoms.areOneClickWalletsRendered => RecoilAtoms.areOneClickWalletsRendered
) => unit,
~setIsCompleted,
~isCallbackUsedVal: bool,
~isCallbackUsedVal as _: bool,
~sdkHandleIsThere: bool,
~sessions: PaymentType.loadType,
) => {
loggerState.setLogInfo(
~value="Paypal SDK Button Clicked",
Expand All @@ -31,6 +34,23 @@ let loadPaypalSDK = (
let paypalWrapper = GooglePayType.getElementById(Utils.document, "paypal-button")
paypalWrapper.innerHTML = ""
setIsCompleted(_ => true)
let paypalNextAction = switch sessions {
| Loaded(data) =>
data
->getDictFromJson
->getOptionalArrayFromDict("session_token")
->Option.flatMap(arr => {
arr->Array.find(ele => ele->getDictFromJson->getString("connector", "") == "paypal")
})
->Option.flatMap(ele => {
ele
->getDictFromJson
->getDictFromDict("sdk_next_action")
->getOptionString("next_action")
})
->Option.getOr("")
| _ => ""
}
paypal["Buttons"]({
style: buttonStyle,
fundingSource: paypal["FUNDING"]["PAYPAL"],
Expand All @@ -52,17 +72,36 @@ let loadPaypalSDK = (
~body,
)
Promise.make((resolve, _) => {
intent(
~bodyArr=modifiedPaymentBody,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
~intentCallback=val =>
val->Utils.getDictFromJson->Utils.getString("orderId", "")->resolve,
~manualRetry=isManualRetryEnabled,
)
if paypalNextAction == "post_session_tokens" {
postSessionTokens(
~bodyArr=modifiedPaymentBody,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
~intentCallback=val => {
val
->Utils.getDictFromJson
->Utils.getDictFromDict("nextActionData")
->Utils.getString("order_id", "")
->resolve
},
~manualRetry=isManualRetryEnabled,
)
} else {
confirm(
~bodyArr=modifiedPaymentBody,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
~intentCallback=val =>
val->Utils.getDictFromJson->Utils.getString("orderId", "")->resolve,
~manualRetry=isManualRetryEnabled,
)
}
})
} else {
loggerState.setLogInfo(
Expand Down Expand Up @@ -100,22 +139,46 @@ let loadPaypalSDK = (
~statesList=stateJson,
)

let (connectors, _) =
paymentMethodListValue->PaymentUtils.getConnectors(Wallets(Paypal(SDK)))

let orderId = val->Utils.getDictFromJson->Utils.getString("id", "")
let body = PaymentBody.paypalSdkBody(~token=orderId, ~connectors)
let modifiedPaymentBody = PaymentUtils.appendedCustomerAcceptance(
~isGuestCustomer,
~paymentType=paymentMethodListValue.payment_type,
~body,
)

let bodyArr =
requiredFieldsBody
->JSON.Encode.object
->Utils.unflattenObject
->Utils.getArrayOfTupleFromDict

completeAuthorize(
~bodyArr,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
)

resolve()
let confirmBody = bodyArr->Array.concatMany([modifiedPaymentBody])
Promise.make((_resolve, _) => {
if paypalNextAction == "post_session_tokens" {
confirm(
~bodyArr=confirmBody,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
~manualRetry=true,
)
} else {
completeAuthorize(
~bodyArr,
~confirmParam={
return_url: options.wallets.walletReturnUrl,
publishableKey,
},
~handleUserError=true,
)
}
})
})
->ignore
}
Expand Down
Loading

0 comments on commit 07c9612

Please sign in to comment.