From e23e9b326e4876f2e804b2004434a6be0cac33bf Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Thu, 6 Jun 2024 17:53:05 +0530 Subject: [PATCH 1/4] feat: added confirmWithCustomerLastPaymentUsed --- src/orca-loader/PaymentSessionMethods.res | 278 ++++++++++++---------- src/orca-loader/Types.res | 10 + 2 files changed, 166 insertions(+), 122 deletions(-) diff --git a/src/orca-loader/PaymentSessionMethods.res b/src/orca-loader/PaymentSessionMethods.res index 6342f43be..28361542d 100644 --- a/src/orca-loader/PaymentSessionMethods.res +++ b/src/orca-loader/PaymentSessionMethods.res @@ -1,3 +1,17 @@ +let sortFunctions = (a, b) => { + open Utils + let temp1 = Date.fromString(a->getDictFromJson->getString("last_used_at", "")) + let temp2 = Date.fromString(b->getDictFromJson->getString("last_used_at", "")) + + if temp1 === temp2 { + 0. + } else if temp1 > temp2 { + -1. + } else { + 1. + } +} + let getCustomerSavedPaymentMethods = ( ~clientSecret, ~publishableKey, @@ -17,140 +31,160 @@ let getCustomerSavedPaymentMethods = ( ~isPaymentSession=true, ) ->then(customerDetails => { - let customerPaymentMethods = + let customerDetailsArray = customerDetails ->JSON.Decode.object ->Option.flatMap(x => x->Dict.get("customer_payment_methods")) ->Option.flatMap(JSON.Decode.array) ->Option.getOr([]) - ->Array.filter(customerPaymentMethod => { - customerPaymentMethod - ->JSON.Decode.object - ->Option.flatMap(x => x->Dict.get("default_payment_method_set")) - ->Option.flatMap(JSON.Decode.bool) - ->Option.getOr(false) - }) - - switch customerPaymentMethods->Array.get(0) { - | Some(customerDefaultPaymentMethod) => - let getCustomerDefaultSavedPaymentMethodData = () => { - customerDefaultPaymentMethod + + let customerPaymentMethods = customerDetailsArray->Array.filter(customerPaymentMethod => { + customerPaymentMethod + ->JSON.Decode.object + ->Option.flatMap(x => x->Dict.get("default_payment_method_set")) + ->Option.flatMap(JSON.Decode.bool) + ->Option.getOr(false) + }) + + let paymentNotExist = (~message="There is no customer default saved payment method data") => + [ + ( + "error", + [ + ("type", "no_data"->JSON.Encode.string), + ("message", message->JSON.Encode.string), + ]->getJsonFromArrayOfJson, + ), + ]->getJsonFromArrayOfJson + + let getCustomerDefaultSavedPaymentMethodData = () => + switch customerPaymentMethods->Array.get(0) { + | Some(customerDefaultPaymentMethod) => customerDefaultPaymentMethod + | None => paymentNotExist() } - let confirmWithCustomerDefaultPaymentMethod = payload => { - let customerPaymentMethod = - customerDefaultPaymentMethod->JSON.Decode.object->Option.getOr(Dict.make()) - let paymentToken = customerPaymentMethod->getJsonFromDict("payment_token", JSON.Encode.null) - let paymentMethod = - customerPaymentMethod->getJsonFromDict("payment_method", JSON.Encode.null) - let paymentMethodType = - customerPaymentMethod->getJsonFromDict("payment_method_type", JSON.Encode.null) - - let confirmParams = - payload - ->JSON.Decode.object - ->Option.flatMap(x => x->Dict.get("confirmParams")) - ->Option.getOr(JSON.Encode.null) - - let redirect = - payload - ->JSON.Decode.object - ->Option.flatMap(x => x->Dict.get("redirect")) - ->Option.flatMap(JSON.Decode.string) - ->Option.getOr("if_required") - - let returnUrl = - confirmParams - ->JSON.Decode.object - ->Option.flatMap(x => x->Dict.get("return_url")) - ->Option.flatMap(JSON.Decode.string) - ->Option.getOr("") - - let confirmParam: ConfirmType.confirmParams = { - return_url: returnUrl, - publishableKey, - redirect, - } - - let paymentIntentID = String.split(clientSecret, "_secret_")[0]->Option.getOr("") - let endpoint = ApiEndpoint.getApiEndPoint( - ~publishableKey=confirmParam.publishableKey, - ~isConfirmCall=true, - (), - ) - let uri = `${endpoint}/payments/${paymentIntentID}/confirm` - let headers = [ - ("Content-Type", "application/json"), - ("api-key", confirmParam.publishableKey), - ] - - let paymentType: PaymentHelpers.payment = switch paymentMethodType - ->JSON.Decode.string - ->Option.getOr("") { - | "apple_pay" => Applepay - | "google_pay" => Gpay - | "debit" - | "credit" - | "" => - Card - | _ => Other - } - - let broswerInfo = BrowserSpec.broswerInfo() - - let body = [ - ("client_secret", clientSecret->JSON.Encode.string), - ("payment_method", paymentMethod), - ("payment_token", paymentToken), - ("payment_method_type", paymentMethodType), - ] - - let bodyStr = body->Array.concat(broswerInfo)->getJsonFromArrayOfJson->JSON.stringify - - PaymentHelpers.intentCall( - ~fetchApi, - ~uri, - ~headers, - ~bodyStr, - ~confirmParam: ConfirmType.confirmParams, - ~clientSecret, - ~optLogger=Some(logger), - ~handleUserError=false, - ~paymentType, - ~iframeId="", - ~fetchMethod=#POST, - ~setIsManualRetryEnabled={_ => ()}, - ~switchToCustomPod=false, - ~sdkHandleOneClickConfirmPayment=false, - ~counter=0, - ~isPaymentSession=true, - (), - ) + let getCustomerLastPaymentMethodUsedData = () => { + let customerPaymentMethodsCopy = customerDetailsArray->Array.copy + customerPaymentMethodsCopy->Array.sort(sortFunctions) + + switch customerPaymentMethodsCopy->Array.get(0) { + | Some(customerLastPaymentUsed) => customerLastPaymentUsed + | None => paymentNotExist(~message="No recent payments found.") } + } + + let confirmCallForParticularPaymentObject = (~paymentMethodObject, ~payload) => { + let customerPaymentMethod = paymentMethodObject->getDictFromJson + + let paymentToken = customerPaymentMethod->getJsonFromDict("payment_token", JSON.Encode.null) + + let paymentMethod = customerPaymentMethod->getJsonFromDict("payment_method", JSON.Encode.null) + + let paymentMethodType = + customerPaymentMethod->getJsonFromDict("payment_method_type", JSON.Encode.null) - { - getCustomerDefaultSavedPaymentMethodData, - confirmWithCustomerDefaultPaymentMethod, + let confirmParams = + payload + ->getDictFromJson + ->getDictfromDict("confirmParams") + + let redirect = confirmParams->getString("redirect", "if_required") + + let returnUrl = confirmParams->getString("return_url", "") + + let confirmParam: ConfirmType.confirmParams = { + return_url: returnUrl, + publishableKey, + redirect, } - ->Identity.anyTypeToJson - ->resolve - | None => { - let updatedCustomerDetails = - [ - ( - "error", - [ - ("type", "no_data"->JSON.Encode.string), - ( - "message", - "There is no customer default saved payment method data"->JSON.Encode.string, - ), - ]->getJsonFromArrayOfJson, - ), - ]->getJsonFromArrayOfJson - updatedCustomerDetails->resolve + + let paymentIntentID = String.split(clientSecret, "_secret_")[0]->Option.getOr("") + + let endpoint = ApiEndpoint.getApiEndPoint( + ~publishableKey=confirmParam.publishableKey, + ~isConfirmCall=true, + (), + ) + let uri = `${endpoint}/payments/${paymentIntentID}/confirm` + let headers = [("Content-Type", "application/json"), ("api-key", confirmParam.publishableKey)] + + let paymentType: PaymentHelpers.payment = switch paymentMethodType + ->JSON.Decode.string + ->Option.getOr("") { + | "apple_pay" => Applepay + | "google_pay" => Gpay + | "debit" + | "credit" + | "" => + Card + | _ => Other + } + + let broswerInfo = BrowserSpec.broswerInfo() + + let body = [ + ("client_secret", clientSecret->JSON.Encode.string), + ("payment_method", paymentMethod), + ("payment_token", paymentToken), + ("payment_method_type", paymentMethodType), + ] + + let bodyStr = body->Array.concat(broswerInfo)->getJsonFromArrayOfJson->JSON.stringify + + PaymentHelpers.intentCall( + ~fetchApi, + ~uri, + ~headers, + ~bodyStr, + ~confirmParam: ConfirmType.confirmParams, + ~clientSecret, + ~optLogger=Some(logger), + ~handleUserError=false, + ~paymentType, + ~iframeId="", + ~fetchMethod=#POST, + ~setIsManualRetryEnabled={_ => ()}, + ~switchToCustomPod=false, + ~sdkHandleOneClickConfirmPayment=false, + ~counter=0, + ~isPaymentSession=true, + (), + ) + } + + let confirmWithCustomerDefaultPaymentMethod = payload => { + switch customerPaymentMethods->Array.get(0) { + | Some(customerDefaultPaymentMethod) => + confirmCallForParticularPaymentObject( + ~paymentMethodObject=customerDefaultPaymentMethod, + ~payload, + ) + | None => paymentNotExist()->resolve } } + + let confirmWithCustomerLastPaymentUsed = payload => { + let customerPaymentMethodsCopy = customerDetailsArray->Array.copy + customerPaymentMethodsCopy->Array.sort(sortFunctions) + + switch customerPaymentMethodsCopy->Array.get(0) { + | Some(customerLastPaymentUsed) => + confirmCallForParticularPaymentObject( + ~paymentMethodObject=customerLastPaymentUsed, + ~payload, + ) + | None => paymentNotExist(~message="No recent payments found.")->resolve + } + } + + { + getCustomerDefaultSavedPaymentMethodData, + getCustomerLastPaymentMethodUsedData, + confirmWithCustomerDefaultPaymentMethod, + confirmWithCustomerLastPaymentUsed, + } + ->Identity.anyTypeToJson + ->resolve }) ->catch(err => { let exceptionMessage = err->formatException->JSON.stringify diff --git a/src/orca-loader/Types.res b/src/orca-loader/Types.res index ced9a31e4..2ead3e758 100644 --- a/src/orca-loader/Types.res +++ b/src/orca-loader/Types.res @@ -41,7 +41,9 @@ type element = { type getCustomerSavedPaymentMethods = { getCustomerDefaultSavedPaymentMethodData: unit => JSON.t, + getCustomerLastPaymentMethodUsedData: unit => JSON.t, confirmWithCustomerDefaultPaymentMethod: JSON.t => Promise.t, + confirmWithCustomerLastPaymentUsed: JSON.t => Promise.t, } type initPaymentSession = {getCustomerSavedPaymentMethods: unit => Promise.t} @@ -122,10 +124,18 @@ let getCustomerDefaultSavedPaymentMethodData = () => { JSON.Encode.null } +let getCustomerLastPaymentMethodUsedData = () => { + JSON.Encode.null +} + let confirmWithCustomerDefaultPaymentMethod = _confirmParams => { Promise.resolve(Dict.make()->JSON.Encode.object) } +let confirmWithCustomerLastPaymentUsed = _confirmParams => { + Promise.resolve(Dict.make()->JSON.Encode.object) +} + let defaultGetCustomerSavedPaymentMethods = () => { // TODO: After rescript migration to v11, add this without TAG using enums // Promise.resolve({ From 2d137bcd1a20dab8e225bb00677dfca2819e5a4c Mon Sep 17 00:00:00 2001 From: Arush Date: Wed, 12 Jun 2024 17:09:13 +0530 Subject: [PATCH 2/4] fix: resolved comments --- src/orca-loader/PaymentSessionMethods.res | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/orca-loader/PaymentSessionMethods.res b/src/orca-loader/PaymentSessionMethods.res index 28361542d..987187b8c 100644 --- a/src/orca-loader/PaymentSessionMethods.res +++ b/src/orca-loader/PaymentSessionMethods.res @@ -46,7 +46,9 @@ let getCustomerSavedPaymentMethods = ( ->Option.getOr(false) }) - let paymentNotExist = (~message="There is no customer default saved payment method data") => + let paymentNotExist = ( + ~message="There is no default saved payment method data for this customer", + ) => [ ( "error", @@ -69,7 +71,7 @@ let getCustomerSavedPaymentMethods = ( switch customerPaymentMethodsCopy->Array.get(0) { | Some(customerLastPaymentUsed) => customerLastPaymentUsed - | None => paymentNotExist(~message="No recent payments found.") + | None => paymentNotExist(~message="No recent payments found for this customer.") } } @@ -173,7 +175,7 @@ let getCustomerSavedPaymentMethods = ( ~paymentMethodObject=customerLastPaymentUsed, ~payload, ) - | None => paymentNotExist(~message="No recent payments found.")->resolve + | None => paymentNotExist(~message="No recent payments found for this customer.")->resolve } } From fdb71066ce596f80042d04e08368329b4df469e9 Mon Sep 17 00:00:00 2001 From: Arush Date: Thu, 13 Jun 2024 14:35:16 +0530 Subject: [PATCH 3/4] fix: resolved conflicts --- src/orca-loader/PaymentSessionMethods.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/orca-loader/PaymentSessionMethods.res b/src/orca-loader/PaymentSessionMethods.res index 987187b8c..ae2a3765f 100644 --- a/src/orca-loader/PaymentSessionMethods.res +++ b/src/orca-loader/PaymentSessionMethods.res @@ -88,7 +88,7 @@ let getCustomerSavedPaymentMethods = ( let confirmParams = payload ->getDictFromJson - ->getDictfromDict("confirmParams") + ->getDictFromDict("confirmParams") let redirect = confirmParams->getString("redirect", "if_required") From a70a743c45c1423b01c56ff601cc947c5a4394a8 Mon Sep 17 00:00:00 2001 From: Arush Date: Thu, 13 Jun 2024 15:51:33 +0530 Subject: [PATCH 4/4] fix: renamed the props --- src/orca-loader/PaymentSessionMethods.res | 8 ++++---- src/orca-loader/Types.res | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/orca-loader/PaymentSessionMethods.res b/src/orca-loader/PaymentSessionMethods.res index ae2a3765f..819b6881f 100644 --- a/src/orca-loader/PaymentSessionMethods.res +++ b/src/orca-loader/PaymentSessionMethods.res @@ -65,7 +65,7 @@ let getCustomerSavedPaymentMethods = ( | None => paymentNotExist() } - let getCustomerLastPaymentMethodUsedData = () => { + let getCustomerLastUsedPaymentMethodData = () => { let customerPaymentMethodsCopy = customerDetailsArray->Array.copy customerPaymentMethodsCopy->Array.sort(sortFunctions) @@ -165,7 +165,7 @@ let getCustomerSavedPaymentMethods = ( } } - let confirmWithCustomerLastPaymentUsed = payload => { + let confirmWithLastUsedPaymentMethod = payload => { let customerPaymentMethodsCopy = customerDetailsArray->Array.copy customerPaymentMethodsCopy->Array.sort(sortFunctions) @@ -181,9 +181,9 @@ let getCustomerSavedPaymentMethods = ( { getCustomerDefaultSavedPaymentMethodData, - getCustomerLastPaymentMethodUsedData, + getCustomerLastUsedPaymentMethodData, confirmWithCustomerDefaultPaymentMethod, - confirmWithCustomerLastPaymentUsed, + confirmWithLastUsedPaymentMethod, } ->Identity.anyTypeToJson ->resolve diff --git a/src/orca-loader/Types.res b/src/orca-loader/Types.res index 2ead3e758..8d396c99a 100644 --- a/src/orca-loader/Types.res +++ b/src/orca-loader/Types.res @@ -41,9 +41,9 @@ type element = { type getCustomerSavedPaymentMethods = { getCustomerDefaultSavedPaymentMethodData: unit => JSON.t, - getCustomerLastPaymentMethodUsedData: unit => JSON.t, + getCustomerLastUsedPaymentMethodData: unit => JSON.t, confirmWithCustomerDefaultPaymentMethod: JSON.t => Promise.t, - confirmWithCustomerLastPaymentUsed: JSON.t => Promise.t, + confirmWithLastUsedPaymentMethod: JSON.t => Promise.t, } type initPaymentSession = {getCustomerSavedPaymentMethods: unit => Promise.t} @@ -124,7 +124,7 @@ let getCustomerDefaultSavedPaymentMethodData = () => { JSON.Encode.null } -let getCustomerLastPaymentMethodUsedData = () => { +let getCustomerLastUsedPaymentMethodData = () => { JSON.Encode.null } @@ -132,7 +132,7 @@ let confirmWithCustomerDefaultPaymentMethod = _confirmParams => { Promise.resolve(Dict.make()->JSON.Encode.object) } -let confirmWithCustomerLastPaymentUsed = _confirmParams => { +let confirmWithLastUsedPaymentMethod = _confirmParams => { Promise.resolve(Dict.make()->JSON.Encode.object) }