Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added confirm and get handler for last used payment #428

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 158 additions & 122 deletions src/orca-loader/PaymentSessionMethods.res
Original file line number Diff line number Diff line change
@@ -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.
}
}

ArushKapoorJuspay marked this conversation as resolved.
Show resolved Hide resolved
let getCustomerSavedPaymentMethods = (
~clientSecret,
~publishableKey,
Expand All @@ -17,140 +31,162 @@ 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 default saved payment method data for this customer",
) =>
[
(
"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 getCustomerLastUsedPaymentMethodData = () => {
let customerPaymentMethodsCopy = customerDetailsArray->Array.copy
customerPaymentMethodsCopy->Array.sort(sortFunctions)

switch customerPaymentMethodsCopy->Array.get(0) {
| Some(customerLastPaymentUsed) => customerLastPaymentUsed
| None => paymentNotExist(~message="No recent payments found for this customer.")
}
}

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 confirmWithLastUsedPaymentMethod = 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 for this customer.")->resolve
}
}

{
getCustomerDefaultSavedPaymentMethodData,
getCustomerLastUsedPaymentMethodData,
confirmWithCustomerDefaultPaymentMethod,
confirmWithLastUsedPaymentMethod,
}
->Identity.anyTypeToJson
->resolve
})
->catch(err => {
let exceptionMessage = err->formatException->JSON.stringify
Expand Down
10 changes: 10 additions & 0 deletions src/orca-loader/Types.res
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ type element = {

type getCustomerSavedPaymentMethods = {
getCustomerDefaultSavedPaymentMethodData: unit => JSON.t,
getCustomerLastUsedPaymentMethodData: unit => JSON.t,
confirmWithCustomerDefaultPaymentMethod: JSON.t => Promise.t<JSON.t>,
confirmWithLastUsedPaymentMethod: JSON.t => Promise.t<JSON.t>,
}

type initPaymentSession = {getCustomerSavedPaymentMethods: unit => Promise.t<JSON.t>}
Expand Down Expand Up @@ -122,10 +124,18 @@ let getCustomerDefaultSavedPaymentMethodData = () => {
JSON.Encode.null
}

let getCustomerLastUsedPaymentMethodData = () => {
JSON.Encode.null
}

let confirmWithCustomerDefaultPaymentMethod = _confirmParams => {
Promise.resolve(Dict.make()->JSON.Encode.object)
}

let confirmWithLastUsedPaymentMethod = _confirmParams => {
Promise.resolve(Dict.make()->JSON.Encode.object)
}

let defaultGetCustomerSavedPaymentMethods = () => {
// TODO: After rescript migration to v11, add this without TAG using enums
// Promise.resolve({
Expand Down
Loading