Skip to content

Commit 63e8c8e

Browse files
committed
refactor: addressed comments
1 parent bc60a59 commit 63e8c8e

File tree

6 files changed

+108
-68
lines changed

6 files changed

+108
-68
lines changed

src/Utilities/APIHelpers/APIUtils.res

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type apiCallV1 =
1313
| RetrieveStatus
1414
| ConfirmPayout
1515

16-
type apiCallV2 = FetchSessionsV2
16+
type apiCallV2 = FetchSessionsV2 | FetchIntent
1717

1818
type apiCall =
1919
| V1(apiCallV1)
@@ -26,6 +26,7 @@ type apiParams = {
2626
paymentMethodId: option<string>,
2727
forceSync: option<string>,
2828
pollId: option<string>,
29+
paymentIdV2: option<string>,
2930
}
3031

3132
let generateApiUrl = (apiCallType: apiCall, ~params: apiParams) => {
@@ -36,13 +37,15 @@ let generateApiUrl = (apiCallType: apiCall, ~params: apiParams) => {
3637
paymentMethodId,
3738
forceSync,
3839
pollId,
40+
paymentIdV2,
3941
} = params
4042

4143
let clientSecretVal = clientSecret->Option.getOr("")
4244
let publishableKeyVal = publishableKey->Option.getOr("")
4345
let paymentIntentID = Utils.getPaymentId(clientSecretVal)
4446
let paymentMethodIdVal = paymentMethodId->Option.getOr("")
4547
let pollIdVal = pollId->Option.getOr("")
48+
let paymentIdVal = paymentIdV2->Option.getOr("")
4649

4750
let baseUrl =
4851
customBackendBaseUrl->Option.getOr(
@@ -115,6 +118,7 @@ let generateApiUrl = (apiCallType: apiCall, ~params: apiParams) => {
115118
| V2(inner) =>
116119
switch inner {
117120
| FetchSessionsV2 => `v2/payments/${paymentIntentID}/create-external-sdk-tokens`
121+
| FetchIntent => `v2/payments/${paymentIdVal}/get-intent`
118122
}
119123
}
120124

src/Utilities/PaymentHelpers.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let retrievePaymentIntent = async (
3636
paymentMethodId: None,
3737
forceSync: isForceSync ? Some("true") : None,
3838
pollId: None,
39+
paymentIdV2: None,
3940
},
4041
)
4142

@@ -71,6 +72,7 @@ let threeDsAuth = async (~clientSecret, ~logger, ~threeDsMethodComp, ~headers) =
7172
paymentMethodId: None,
7273
forceSync: None,
7374
pollId: None,
75+
paymentIdV2: None,
7476
},
7577
)
7678
let broswerInfo = BrowserSpec.broswerInfo
@@ -172,6 +174,7 @@ let retrieveStatus = async (~publishableKey, ~customPodUri, pollID, logger) => {
172174
paymentMethodId: None,
173175
forceSync: None,
174176
pollId: Some(pollID),
177+
paymentIdV2: None,
175178
},
176179
)
177180

@@ -1368,6 +1371,7 @@ let fetchSessions = async (
13681371
paymentMethodId: None,
13691372
forceSync: None,
13701373
pollId: None,
1374+
paymentIdV2: None,
13711375
},
13721376
)
13731377

@@ -1407,6 +1411,7 @@ let confirmPayout = async (
14071411
paymentMethodId: None,
14081412
forceSync: None,
14091413
pollId: None,
1414+
paymentIdV2: None,
14101415
},
14111416
)
14121417

@@ -1449,6 +1454,7 @@ let createPaymentMethod = async (
14491454
paymentMethodId: None,
14501455
forceSync: None,
14511456
pollId: None,
1457+
paymentIdV2: None,
14521458
},
14531459
)
14541460

@@ -1490,6 +1496,7 @@ let fetchPaymentMethodList = async (
14901496
paymentMethodId: None,
14911497
forceSync: None,
14921498
pollId: None,
1499+
paymentIdV2: None,
14931500
},
14941501
)
14951502

@@ -1526,6 +1533,7 @@ let fetchCustomerPaymentMethodList = async (
15261533
paymentMethodId: None,
15271534
forceSync: None,
15281535
pollId: None,
1536+
paymentIdV2: None,
15291537
},
15301538
)
15311539

@@ -1632,6 +1640,7 @@ let callAuthLink = async (
16321640
paymentMethodId: None,
16331641
forceSync: None,
16341642
pollId: None,
1643+
paymentIdV2: None,
16351644
},
16361645
)
16371646

@@ -1695,6 +1704,7 @@ let callAuthExchange = async (
16951704
paymentMethodId: None,
16961705
forceSync: None,
16971706
pollId: None,
1707+
paymentIdV2: None,
16981708
},
16991709
)
17001710

@@ -1767,6 +1777,7 @@ let fetchSavedPaymentMethodList = async (
17671777
paymentMethodId: None,
17681778
forceSync: None,
17691779
pollId: None,
1780+
paymentIdV2: None,
17701781
},
17711782
)
17721783

@@ -1797,6 +1808,7 @@ let deletePaymentMethod = async (~ephemeralKey, ~paymentMethodId, ~logger, ~cust
17971808
paymentMethodId: Some(paymentMethodId),
17981809
forceSync: None,
17991810
pollId: None,
1811+
paymentIdV2: None,
18001812
},
18011813
)
18021814

@@ -1834,6 +1846,7 @@ let calculateTax = async (
18341846
paymentMethodId: None,
18351847
forceSync: None,
18361848
pollId: None,
1849+
paymentIdV2: None,
18371850
},
18381851
)
18391852
let onSuccess = data => data

src/Utilities/PaymentHelpersV2.res

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -586,43 +586,43 @@ let fetchSessions = (
586586
})
587587
}
588588

589-
let fetchIntent = (
589+
let fetchIntent = async (
590590
~clientSecret,
591591
~publishableKey,
592592
~paymentId,
593-
~logger as _,
593+
~logger,
594594
~customPodUri,
595595
~endpoint,
596596
~profileId,
597597
) => {
598-
open Promise
599-
let baseHeaders = [
600-
("Content-Type", "application/json"),
601-
("x-profile-id", profileId),
602-
("Authorization", `publishable-key=${publishableKey},client-secret=${clientSecret}`),
603-
]
598+
let uri = APIUtils.generateApiUrl(
599+
V2(FetchIntent),
600+
~params={
601+
clientSecret: Some(clientSecret),
602+
customBackendBaseUrl: Some(endpoint),
603+
publishableKey: Some(publishableKey),
604+
paymentMethodId: None,
605+
forceSync: None,
606+
pollId: None,
607+
paymentIdV2: Some(paymentId),
608+
},
609+
)
604610

605-
let headers = switch customPodUri {
606-
| value if value != "" => [...baseHeaders, ("x-feature", value)]
607-
| _ => baseHeaders
608-
}
611+
let onSuccess = data => data
609612

610-
let uri = `${endpoint}/v2/payments/${paymentId}/get-intent`
611-
fetchApi(uri, ~method=#GET, ~headers=headers->ApiEndpoint.addCustomPodHeader(~customPodUri))
612-
->then(resp => {
613-
if !(resp->Fetch.Response.ok) {
614-
resp
615-
->Fetch.Response.json
616-
->then(_ => {
617-
JSON.Encode.null->resolve
618-
})
619-
} else {
620-
Fetch.Response.json(resp)
621-
}
622-
})
623-
->catch(err => {
624-
let exceptionMessage = err->formatException
625-
Console.error2("Error ", exceptionMessage)
626-
JSON.Encode.null->resolve
627-
})
613+
let onFailure = _ => JSON.Encode.null
614+
615+
// Todo: Add logger
616+
await fetchApiWithLogging(
617+
uri,
618+
~eventName=CUSTOMER_PAYMENT_METHODS_CALL,
619+
~logger,
620+
~method=#GET,
621+
~customPodUri=Some(customPodUri),
622+
~publishableKey=Some(publishableKey),
623+
~clientSecret=Some(clientSecret),
624+
~profileId=Some(profileId),
625+
~onSuccess,
626+
~onFailure,
627+
)
628628
}

src/Utilities/UnifiedHelpersV2.res

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ let itemToIntentObjMapper = dict => {
156156
}
157157

158158
let createIntentDetails = (dict, key) => {
159-
let intentDict =
160-
dict
161-
->Dict.get(key)
162-
->Option.flatMap(JSON.Decode.object)
163-
->Option.getOr(Dict.make())
159+
let intentDict = dict->Utils.getDictFromDict(key)
164160
let response = intentDict->itemToIntentObjMapper
165161

166162
LoadedIntent(response)

src/Utilities/Utils.res

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,30 @@ let getHeaders = (
848848
~customPodUri=None,
849849
~headers=Dict.make(),
850850
~publishableKey=None,
851+
~clientSecret=None,
852+
~profileId=None,
851853
): Fetch.Headers.t => {
854+
let publishableKeyVal = publishableKey->Option.map(key => key)->Option.getOr("invalid_key")
855+
let profileIdVal = profileId->Option.getOr("invalid_key")
856+
let clientSecretVal = clientSecret->Option.getOr("invalid_key")
857+
852858
let defaultHeaders = [
853859
("Content-Type", "application/json"),
854-
("api-key", publishableKey->Option.map(key => key)->Option.getOr("invalid_key")),
855860
("X-Client-Version", Window.version),
856861
("X-Payment-Confirm-Source", "sdk"),
857862
("X-Browser-Name", HyperLogger.arrayOfNameAndVersion->Array.get(0)->Option.getOr("Others")),
858863
("X-Browser-Version", HyperLogger.arrayOfNameAndVersion->Array.get(1)->Option.getOr("0")),
859864
("X-Client-Platform", "web"),
860865
]
861866

867+
let authorizationHeaders = switch GlobalVars.sdkVersion {
868+
| V2 => [
869+
("x-profile-id", profileIdVal),
870+
("Authorization", `publishable-key=${publishableKeyVal},client-secret=${clientSecretVal}`),
871+
]
872+
| V1 => [("api-key", publishableKey->Option.map(key => key)->Option.getOr("invalid_key"))]
873+
}
874+
862875
let authHeader = switch (token, uri) {
863876
| (Some(tok), Some(_)) => [("Authorization", tok)]
864877
| _ => []
@@ -871,6 +884,7 @@ let getHeaders = (
871884

872885
let finalHeaders = [
873886
...defaultHeaders,
887+
...authorizationHeaders,
874888
...authHeader,
875889
...customPodHeader,
876890
...Dict.toArray(headers),
@@ -949,16 +963,20 @@ let fetchApiWithLogging = async (
949963
~publishableKey=None,
950964
~isPaymentSession=false,
951965
~onCatchCallback=None,
966+
~clientSecret=None,
967+
~profileId=None,
952968
) => {
953969
open LoggerUtils
954970

955971
// * Log request initiation
956-
LogAPIResponse.logApiResponse(
957-
~logger,
958-
~uri,
959-
~eventName=apiEventInitMapper(eventName),
960-
~status=Request,
961-
)
972+
if GlobalVars.sdkVersion != V2 {
973+
LogAPIResponse.logApiResponse(
974+
~logger,
975+
~uri,
976+
~eventName=apiEventInitMapper(eventName),
977+
~status=Request,
978+
)
979+
}
962980

963981
try {
964982
let body = switch method {
@@ -976,6 +994,8 @@ let fetchApiWithLogging = async (
976994
~uri,
977995
~customPodUri,
978996
~publishableKey,
997+
~clientSecret,
998+
~profileId,
979999
),
9801000
},
9811001
)
@@ -984,26 +1004,30 @@ let fetchApiWithLogging = async (
9841004

9851005
if resp->Fetch.Response.ok {
9861006
let data = await Fetch.Response.json(resp)
987-
LogAPIResponse.logApiResponse(
988-
~logger,
989-
~uri,
990-
~eventName=Some(eventName),
991-
~status=Success,
992-
~statusCode,
993-
~isPaymentSession,
994-
)
1007+
if GlobalVars.sdkVersion != V2 {
1008+
LogAPIResponse.logApiResponse(
1009+
~logger,
1010+
~uri,
1011+
~eventName=Some(eventName),
1012+
~status=Success,
1013+
~statusCode,
1014+
~isPaymentSession,
1015+
)
1016+
}
9951017
onSuccess(data)
9961018
} else {
9971019
let data = await resp->Fetch.Response.json
998-
LogAPIResponse.logApiResponse(
999-
~logger,
1000-
~uri,
1001-
~eventName=Some(eventName),
1002-
~status=Error,
1003-
~statusCode,
1004-
~data,
1005-
~isPaymentSession,
1006-
)
1020+
if GlobalVars.sdkVersion != V2 {
1021+
LogAPIResponse.logApiResponse(
1022+
~logger,
1023+
~uri,
1024+
~eventName=Some(eventName),
1025+
~status=Error,
1026+
~statusCode,
1027+
~data,
1028+
~isPaymentSession,
1029+
)
1030+
}
10071031
onFailure(data)
10081032
}
10091033
} catch {
@@ -1017,14 +1041,16 @@ let fetchApiWithLogging = async (
10171041
"error": exceptionMessage,
10181042
},
10191043
)
1020-
LogAPIResponse.logApiResponse(
1021-
~logger,
1022-
~uri,
1023-
~eventName=Some(eventName),
1024-
~status=Exception,
1025-
~data=exceptionMessage,
1026-
~isPaymentSession,
1027-
)
1044+
if GlobalVars.sdkVersion != V2 {
1045+
LogAPIResponse.logApiResponse(
1046+
~logger,
1047+
~uri,
1048+
~eventName=Some(eventName),
1049+
~status=Exception,
1050+
~data=exceptionMessage,
1051+
~isPaymentSession,
1052+
)
1053+
}
10281054
switch onCatchCallback {
10291055
| Some(fun) => fun(exceptionMessage)
10301056
| None => onFailure(exceptionMessage)

src/hyper-loader/Hyper.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ let make = (keys, options: option<JSON.t>, analyticsInfo: option<JSON.t>) => {
340340
paymentMethodId: None,
341341
forceSync: None,
342342
pollId: None,
343+
paymentIdV2: None,
343344
},
344345
)
345346

0 commit comments

Comments
 (0)