Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
let (optionsPayment, setOptionsPayment) = Recoil.useRecoilState(optionAtom)
let setPaymentManagementList = Recoil.useSetRecoilState(paymentManagementList)
let setPaymentMethodsListV2 = Recoil.useSetRecoilState(paymentMethodsListV2)
let setIntentList = Recoil.useSetRecoilState(intentList)
let setSessionId = Recoil.useSetRecoilState(sessionId)
let setBlockConfirm = Recoil.useSetRecoilState(isConfirmBlocked)
let setCustomPodUri = Recoil.useSetRecoilState(customPodUri)
Expand Down Expand Up @@ -443,6 +444,11 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime
}
}
}
if dict->getDictIsSome("getIntent") {
let getIntentDict = dict->getJsonObjectFromDict("getIntent")->getDictFromJson
let intentDetails = getIntentDict->UnifiedHelpersV2.createIntentDetails("response")
setIntentList(_ => intentDetails)
}
if dict->getDictIsSome("paymentsListV2") {
let paymentsListV2 = dict->getJsonObjectFromDict("paymentsListV2")
let listDict = paymentsListV2->getDictFromJson
Expand Down
12 changes: 12 additions & 0 deletions src/Payments/PreMountLoader.res
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,25 @@ let getMessageHandlerV2Elements = (
~endpoint,
)

let getIntentPromise = PaymentHelpersV2.fetchIntent(
~clientSecret,
~paymentId,
~profileId,
~publishableKey,
~logger,
~customPodUri,
~endpoint,
)

ev => {
open Utils
let dict = ev.data->safeParse->getDictFromJson
if dict->isKeyPresentInDict("sendPaymentMethodsListV2Response") {
paymentMethodsListPromise->sendPromiseData("payment_methods_list_v2")
} else if dict->isKeyPresentInDict("sendSessionTokensResponse") {
sessionTokensPromise->sendPromiseData("session_tokens")
} else if dict->isKeyPresentInDict("sendGetIntentResponse") {
getIntentPromise->sendPromiseData("get_intent_v2")
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Types/UnifiedPaymentsTypesV2.res
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ type paymentListLookupNew = {
walletsList: array<string>,
otherPaymentList: array<string>,
}

type intentCall = {paymentType: PaymentMethodsRecord.payment_type}

type intentLoadState = LoadingIntent | LoadedIntent(intentCall) | Error(JSON.t)
130 changes: 73 additions & 57 deletions src/Utilities/APIHelpers/APIUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,36 @@ type apiCallV1 =
| RetrieveStatus
| ConfirmPayout

type apiCallV2 = FetchSessionsV2
type apiCallV2 = FetchSessionsV2 | FetchIntent

type apiCall =
| V1(apiCallV1)
| V2(apiCallV2)

type apiParams = {
clientSecret: option<string>,
type commonApiParams = {
publishableKey: option<string>,
customBackendBaseUrl: option<string>,
}

type apiParamsV2 = {...commonApiParams, paymentIdV2: option<string>}

type apiParamsV1 = {
...commonApiParams,
clientSecret: option<string>,
paymentMethodId: option<string>,
forceSync: option<string>,
pollId: option<string>,
payoutId: option<string>,
}

let generateApiUrl = (apiCallType: apiCall, ~params: apiParams) => {
module CommonUtils = {
let buildQueryParams = params =>
switch params {
| list{} => ""
| _ =>
params
->List.map(((key, value)) => `${key}=${value}`)
->List.reduce("", (acc, param) => acc === "" ? `?${param}` : `${acc}&${param}`)
}
}

let generateApiUrlV1 = (~params: apiParamsV1, ~apiCallType: apiCallV1) => {
let {
clientSecret,
publishableKey,
Expand All @@ -52,17 +65,8 @@ let generateApiUrl = (apiCallType: apiCall, ~params: apiParams) => {
ApiEndpoint.getApiEndPoint(~publishableKey=publishableKeyVal),
)

let buildQueryParams = params =>
switch params {
| list{} => ""
| _ =>
params
->List.map(((key, value)) => `${key}=${value}`)
->List.reduce("", (acc, param) => acc === "" ? `?${param}` : `${acc}&${param}`)
}

let isRetrieveIntent = switch apiCallType {
| V1(RetrievePaymentIntent) => true
| RetrievePaymentIntent => true
| _ => false
}

Expand All @@ -78,48 +82,60 @@ let generateApiUrl = (apiCallType: apiCall, ~params: apiParams) => {
}->List.filterMap(x => x)

let queryParams = switch apiCallType {
| V1(inner) =>
switch inner {
| FetchPaymentMethodList
| FetchCustomerPaymentMethodList
| RetrievePaymentIntent => defaultParams
| FetchSessions
| FetchThreeDsAuth
| FetchSavedPaymentMethodList
| DeletePaymentMethod
| CalculateTax
| CreatePaymentMethod
| CallAuthLink
| CallAuthExchange
| RetrieveStatus
| ConfirmPayout =>
list{}
}
| V2(_) => list{}
| FetchPaymentMethodList
| FetchCustomerPaymentMethodList
| RetrievePaymentIntent => defaultParams
| FetchSessions
| FetchThreeDsAuth
| FetchSavedPaymentMethodList
| DeletePaymentMethod
| CalculateTax
| CreatePaymentMethod
| CallAuthLink
| CallAuthExchange
| RetrieveStatus
| ConfirmPayout =>
list{}
}

let path = switch apiCallType {
| V1(inner) =>
switch inner {
| FetchPaymentMethodList => "account/payment_methods"
| FetchSessions => "payments/session_tokens"
| FetchThreeDsAuth => `payments/${paymentIntentID}/3ds/authentication`
| FetchCustomerPaymentMethodList
| FetchSavedPaymentMethodList => "customers/payment_methods"
| DeletePaymentMethod => `payment_methods/${paymentMethodIdVal}`
| CalculateTax => `payments/${paymentIntentID}/calculate_tax`
| CreatePaymentMethod => "payment_methods"
| RetrievePaymentIntent => `payments/${paymentIntentID}`
| CallAuthLink => "payment_methods/auth/link"
| CallAuthExchange => "payment_methods/auth/exchange"
| RetrieveStatus => `poll/status/${pollIdVal}`
| ConfirmPayout => `payouts/${payoutIdVal}/confirm`
}
| V2(inner) =>
switch inner {
| FetchSessionsV2 => `v2/payments/${paymentIntentID}/create-external-sdk-tokens`
}
| FetchPaymentMethodList => "account/payment_methods"
| FetchSessions => "payments/session_tokens"
| FetchThreeDsAuth => `payments/${paymentIntentID}/3ds/authentication`
| FetchCustomerPaymentMethodList
| FetchSavedPaymentMethodList => "customers/payment_methods"
| DeletePaymentMethod => `payment_methods/${paymentMethodIdVal}`
| CalculateTax => `payments/${paymentIntentID}/calculate_tax`
| CreatePaymentMethod => "payment_methods"
| RetrievePaymentIntent => `payments/${paymentIntentID}`
| CallAuthLink => "payment_methods/auth/link"
| CallAuthExchange => "payment_methods/auth/exchange"
| RetrieveStatus => `poll/status/${pollIdVal}`
| ConfirmPayout => `payouts/${payoutIdVal}/confirm`
}

`${baseUrl}/${path}${CommonUtils.buildQueryParams(queryParams)}`
}

let generateApiUrlV2 = (~params: apiParamsV2, ~apiCallType: apiCallV2) => {
let {publishableKey, customBackendBaseUrl, paymentIdV2} = params

let publishableKeyVal = publishableKey->Option.getOr("")
let paymentIdVal = paymentIdV2->Option.getOr("")

let baseUrl =
customBackendBaseUrl->Option.getOr(
ApiEndpoint.getApiEndPoint(~publishableKey=publishableKeyVal),
)

let queryParams = switch apiCallType {
| _ => list{}
}

let path = switch apiCallType {
| FetchSessionsV2 => `v2/payments/${paymentIdVal}/create-external-sdk-tokens`
| FetchIntent => `v2/payments/${paymentIdVal}/get-intent`
}

`${baseUrl}/${path}${buildQueryParams(queryParams)}`
`${baseUrl}/${path}${CommonUtils.buildQueryParams(queryParams)}`
}
52 changes: 26 additions & 26 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ let retrievePaymentIntent = async (
~customPodUri,
~isForceSync=false,
) => {
let uri = APIUtils.generateApiUrl(
V1(RetrievePaymentIntent),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=RetrievePaymentIntent,
~params={
clientSecret: Some(clientSecret),
publishableKey: Some(publishableKey),
Expand Down Expand Up @@ -63,8 +63,8 @@ let retrievePaymentIntent = async (
}

let threeDsAuth = async (~clientSecret, ~logger, ~threeDsMethodComp, ~headers) => {
let url = APIUtils.generateApiUrl(
V1(FetchThreeDsAuth),
let url = APIUtils.generateApiUrlV1(
~apiCallType=FetchThreeDsAuth,
~params={
clientSecret: Some(clientSecret),
publishableKey: None,
Expand Down Expand Up @@ -165,8 +165,8 @@ let rec pollRetrievePaymentIntent = (
}

let retrieveStatus = async (~publishableKey, ~customPodUri, pollID, logger) => {
let uri = APIUtils.generateApiUrl(
V1(RetrieveStatus),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=RetrieveStatus,
~params={
clientSecret: None,
publishableKey: Some(publishableKey),
Expand Down Expand Up @@ -1362,8 +1362,8 @@ let fetchSessions = async (
("wallets", wallets->JSON.Encode.array),
("delayed_session_token", isDelayedSessionToken->JSON.Encode.bool),
]->getJsonFromArrayOfJson
let uri = APIUtils.generateApiUrl(
V1(FetchSessions),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=FetchSessions,
~params={
customBackendBaseUrl: Some(endpoint),
clientSecret: None,
Expand Down Expand Up @@ -1403,8 +1403,8 @@ let confirmPayout = async (
~body,
~payoutId,
) => {
let uri = APIUtils.generateApiUrl(
V1(ConfirmPayout),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=ConfirmPayout,
~params={
clientSecret: Some(clientSecret),
customBackendBaseUrl: Some(endpoint),
Expand Down Expand Up @@ -1446,8 +1446,8 @@ let createPaymentMethod = async (
~endpoint,
~body,
) => {
let uri = APIUtils.generateApiUrl(
V1(CreatePaymentMethod),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=CreatePaymentMethod,
~params={
clientSecret: Some(clientSecret),
customBackendBaseUrl: Some(endpoint),
Expand Down Expand Up @@ -1488,8 +1488,8 @@ let fetchPaymentMethodList = async (
~customPodUri,
~endpoint,
) => {
let uri = APIUtils.generateApiUrl(
V1(FetchPaymentMethodList),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=FetchPaymentMethodList,
~params={
clientSecret: Some(clientSecret),
customBackendBaseUrl: Some(endpoint),
Expand Down Expand Up @@ -1525,8 +1525,8 @@ let fetchCustomerPaymentMethodList = async (
~endpoint,
~isPaymentSession=false,
) => {
let uri = APIUtils.generateApiUrl(
V1(FetchCustomerPaymentMethodList),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=FetchCustomerPaymentMethodList,
~params={
clientSecret: Some(clientSecret),
customBackendBaseUrl: Some(endpoint),
Expand Down Expand Up @@ -1632,8 +1632,8 @@ let callAuthLink = async (
~iframeId,
~logger,
) => {
let uri = APIUtils.generateApiUrl(
V1(CallAuthLink),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=CallAuthLink,
~params={
clientSecret: None,
publishableKey: Some(publishableKey),
Expand Down Expand Up @@ -1696,8 +1696,8 @@ let callAuthExchange = async (
) => {
open Promise
open PaymentType
let uri = APIUtils.generateApiUrl(
V1(CallAuthExchange),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=CallAuthExchange,
~params={
clientSecret: None,
publishableKey: Some(publishableKey),
Expand Down Expand Up @@ -1769,8 +1769,8 @@ let fetchSavedPaymentMethodList = async (
~customPodUri,
~isPaymentSession=false,
) => {
let uri = APIUtils.generateApiUrl(
V1(FetchSavedPaymentMethodList),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=FetchSavedPaymentMethodList,
~params={
customBackendBaseUrl: Some(endpoint),
clientSecret: None,
Expand Down Expand Up @@ -1800,8 +1800,8 @@ let fetchSavedPaymentMethodList = async (
}

let deletePaymentMethod = async (~ephemeralKey, ~paymentMethodId, ~logger, ~customPodUri) => {
let uri = APIUtils.generateApiUrl(
V1(DeletePaymentMethod),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=DeletePaymentMethod,
~params={
customBackendBaseUrl: None,
clientSecret: None,
Expand Down Expand Up @@ -1838,8 +1838,8 @@ let calculateTax = async (
~customPodUri,
~sessionId,
) => {
let uri = APIUtils.generateApiUrl(
V1(CalculateTax),
let uri = APIUtils.generateApiUrlV1(
~apiCallType=CalculateTax,
~params={
customBackendBaseUrl: None,
clientSecret: Some(clientSecret),
Expand Down
Loading
Loading