Skip to content

Commit

Permalink
Merge branch 'master' of ssh://ssh.bitbucket.juspay.net/exc/orca-elem…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
arun.mishra committed Sep 5, 2023
2 parents 784e6cc + 46638b7 commit 2ac5118
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/Payments/QRCodeDisplay.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let make = () => {
let (return_url, setReturnUrl) = React.useState(_ => "")
let (clientSecret, setClientSecret) = React.useState(_ => "")
let (headers, setHeaders) = React.useState(_ => [])
let logger = Recoil.useRecoilValueFromAtom(RecoilAtoms.loggerAtom)

React.useEffect0(() => {
handlePostMessage([("iframeMountedCallback", true->Js.Json.boolean)])
Expand Down Expand Up @@ -49,7 +50,7 @@ let make = () => {
// }
open Promise
setHeaders(_ => headers->Js.Dict.entries)
PaymentHelpers.pollRetrievePaymentIntent(paymentIntentId, headers->Js.Dict.entries)
PaymentHelpers.pollRetrievePaymentIntent(paymentIntentId, headers->Js.Dict.entries, ~optLogger=Some(logger))
->then(res => {
Modal.close(setOpenModal)
postSubmitResponse(~jsonData=res, ~url=return_url)
Expand Down Expand Up @@ -77,7 +78,7 @@ let make = () => {

let closeModal = () => {
open Promise
PaymentHelpers.retrievePaymentIntent(clientSecret, headers)
PaymentHelpers.retrievePaymentIntent(clientSecret, headers, ~optLogger=Some(logger))
->then(json => {
let dict = json->Js.Json.decodeObject->Belt.Option.getWithDefault(Js.Dict.empty())
let status = dict->getString("status", "")
Expand Down
6 changes: 3 additions & 3 deletions src/Utilities/LoggerUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ let logApi = (
]
| NoResponse => [
("url", url->Js.Json.string),
("statusCode", "\"500\""->Js.Json.string),
("response", "{\"error\": \"No Response from Server\"}"->Js.Json.string),
("statusCode", "504"->Js.Json.string),
("response", `{"error": "No Response from Server"}`->Js.Json.string),
]
| Err => [
("url", url->Js.Json.string),
("statusCode", statusCode->Js.Json.string),
("response", "{\"error\": \"No Response from Server\"}"->Js.Json.string),
("response", `{"error": "No Response from Server"}`->Js.Json.string),
]
| Method => [("method", paymentMethod->Js.Json.string), ("result", result)]
}
Expand Down
191 changes: 160 additions & 31 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type payment = Card | BankTransfer | BankDebits | KlarnaRedirect | Gpay | Applep
let closePaymentLoaderIfAny = () =>
Utils.handlePostMessage([("fullscreen", false->Js.Json.boolean)])

let retrievePaymentIntent = (clientSecret, headers) => {
let retrievePaymentIntent = (clientSecret, headers, ~optLogger) => {
open Promise
let fetchApi = CommonHooks.useApiFetcher()
let paymentIntentID = Js.String2.split(clientSecret, "_secret_")[0]
Expand All @@ -22,6 +22,37 @@ let retrievePaymentIntent = (clientSecret, headers) => {
//let headers = [("Accept", "application/json"), ("api-key", publishableKey)]
fetchApi(uri, ~method_=Fetch.Get, ~headers=headers->addCustomPodHeader, ())
->then(res => {
let statusCode = res->Fetch.Response.status->string_of_int
if statusCode->Js.String2.charAt(0) !== "2" {
res
->Fetch.Response.json
->then(data => {
logApi(
~optLogger,
~url=uri,
~data,
~statusCode,
~type_="err",
~eventName=RETRIEVE_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
resolve()
})
->ignore
} else {
logApi(
~optLogger,
~url=uri,
~statusCode,
~type_="response",
~eventName=RETRIEVE_CALL,
~logType=INFO,
~logCategory=API,
(),
)
}
res->Fetch.Response.json
})
->then(res => resolve(res))
Expand All @@ -31,9 +62,9 @@ let retrievePaymentIntent = (clientSecret, headers) => {
})
}

let rec pollRetrievePaymentIntent = (clientSecret, headers) => {
let rec pollRetrievePaymentIntent = (clientSecret, headers, ~optLogger) => {
open Promise
retrievePaymentIntent(clientSecret, headers)
retrievePaymentIntent(clientSecret, headers, ~optLogger)
->then(json => {
let dict = json->Js.Json.decodeObject->Belt.Option.getWithDefault(Js.Dict.empty())
let status = dict->getString("status", "")
Expand All @@ -42,13 +73,13 @@ let rec pollRetrievePaymentIntent = (clientSecret, headers) => {
resolve(json)
} else {
delay(2000)->then(_val => {
pollRetrievePaymentIntent(clientSecret, headers)
pollRetrievePaymentIntent(clientSecret, headers, ~optLogger)
})
}
})
->catch(e => {
Js.log2("Unable to retrieve payment due to following error", e)
pollRetrievePaymentIntent(clientSecret, headers)
pollRetrievePaymentIntent(clientSecret, headers, ~optLogger)
})
}

Expand All @@ -73,6 +104,8 @@ let intentCall = (
~setIsManualRetryEnabled,
) => {
open Promise
let isConfirm = uri->Js.String.includes("/confirm")
let eventName: OrcaLogger.eventName = isConfirm ? CONFIRM_CALL : RETRIEVE_CALL
fetchApi(uri, ~method_=fetchMethod, ~headers=headers->addCustomPodHeader, ~bodyStr, ())
->then(res => {
let statusCode = res->Fetch.Response.status->string_of_int
Expand All @@ -84,7 +117,7 @@ let intentCall = (
res
->Fetch.Response.json
->then(data => {
if uri->Js.String.includes("/confirm") {
if isConfirm {
let paymentMethod = switch paymentType {
| Card => "CARD"
| _ =>
Expand All @@ -100,18 +133,18 @@ let intentCall = (
~paymentMethod,
)
}

logApi(
~optLogger,
~url=uri,
~data,
~statusCode,
~type_="response",
~eventName=API_CALL_FAILED,
~type_="err",
~eventName,
~logType=ERROR,
~logCategory=API,
(),
)

let dict = data->getDictFromJson
let errorObj = PaymentError.itemToObjMapper(dict)
closePaymentLoaderIfAny()
Expand All @@ -126,8 +159,8 @@ let intentCall = (
~optLogger,
~url=uri,
~statusCode,
~type_="err",
~eventName=API_CALL_FAILED_WITHOUT_STATUS_CODE,
~type_="no_response",
~eventName,
~logType=ERROR,
~logCategory=API,
(),
Expand All @@ -144,15 +177,7 @@ let intentCall = (
res
->Fetch.Response.json
->then(data => {
logApi(
~optLogger,
~url=uri,
~data,
~statusCode,
~type_="response",
~eventName=PAYMENT_RESPONSE,
(),
)
logApi(~optLogger, ~url=uri, ~statusCode, ~type_="response", ~eventName, ())
let intent = PaymentConfirmTypes.itemToObjMapper(data->getDictFromJson)

let url = urlSearch(confirmParam.return_url)
Expand Down Expand Up @@ -271,7 +296,7 @@ let intentCall = (
logApi(
~optLogger,
~url=uri,
~eventName=API_CALL_FAILED_WITHOUT_STATUS_CODE,
~eventName,
~type_="no_response",
~logType=ERROR,
~logCategory=API,
Expand Down Expand Up @@ -456,6 +481,7 @@ let useSessions = (
~publishableKey,
~wallets=[],
~isDelayedSessionToken=false,
~optLogger,
(),
) => {
let fetchApi = CommonHooks.useApiFetcher()
Expand All @@ -473,20 +499,61 @@ let useSessions = (
]
->Js.Dict.fromArray
->Js.Json.object_
let uri = `${endpoint}/payments/session_tokens`
fetchApi(
`${endpoint}/payments/session_tokens`,
uri,
~method_=Fetch.Post,
~bodyStr=body->Js.Json.stringify,
~headers=headers->addCustomPodHeader,
(),
)
->then(resp => {
let statusCode = resp->Fetch.Response.status->string_of_int
if statusCode->Js.String2.charAt(0) !== "2" {
resp
->Fetch.Response.json
->then(data => {
logApi(
~optLogger,
~url=uri,
~data,
~statusCode,
~type_="err",
~eventName=SESSIONS_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
resolve()
})
->ignore
} else {
logApi(
~optLogger,
~url=uri,
~statusCode,
~type_="response",
~eventName=SESSIONS_CALL,
~logType=INFO,
~logCategory=API,
(),
)
}
Fetch.Response.json(resp)
})
->then(json => {
json->resolve
})
->catch(_e => {
logApi(
~optLogger,
~url=uri,
~type_="no_response",
~eventName=SESSIONS_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
Js.Dict.empty()->Js.Json.object_->resolve
})
}
Expand All @@ -496,40 +563,102 @@ let usePaymentMethodList = (~clientSecret, ~publishableKey, ~logger) => {

open Promise
let headers = [("Content-Type", "application/json"), ("api-key", publishableKey)]
fetchApi(
`${endpoint}/account/payment_methods?client_secret=${clientSecret}`,
~method_=Fetch.Get,
~headers=headers->addCustomPodHeader,
(),
)
let uri = `${endpoint}/account/payment_methods?client_secret=${clientSecret}`
fetchApi(uri, ~method_=Fetch.Get, ~headers=headers->addCustomPodHeader, ())
->then(resp => {
let statusCode = resp->Fetch.Response.status->string_of_int
if statusCode->Js.String2.charAt(0) !== "2" {
ErrorUtils.manageErrorWarning(INTERNAL_API_DOWN, ~logger, ())
resp
->Fetch.Response.json
->then(data => {
logApi(
~optLogger=Some(logger),
~url=uri,
~data,
~statusCode,
~type_="err",
~eventName=PAYMENT_METHODS_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
resolve()
})
->ignore
} else {
logApi(
~optLogger=Some(logger),
~url=uri,
~statusCode,
~type_="response",
~eventName=PAYMENT_METHODS_CALL,
~logType=INFO,
~logCategory=API,
(),
)
}
Fetch.Response.json(resp)
})
->then(json => {
json->resolve
})
->catch(_e => {
logApi(
~optLogger=Some(logger),
~url=uri,
~type_="no_response",
~eventName=PAYMENT_METHODS_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
Js.Dict.empty()->Js.Json.object_->resolve
})
}

let useCustomerDetails = (~clientSecret, ~publishableKey) => {
let useCustomerDetails = (~clientSecret, ~publishableKey, ~optLogger) => {
open Promise
let fetchApi = CommonHooks.useApiFetcher()
let headers = [("Content-Type", "application/json"), ("api-key", publishableKey)]
let uri = `${ApiEndpoint.endpoint}/customers/payment_methods?client_secret=${clientSecret}`

fetchApi(uri, ~method_=Fetch.Get, ~headers=headers->ApiEndpoint.addCustomPodHeader, ())
->then(res => {
let statusCode = res->Fetch.Response.status->string_of_int
if statusCode->Js.String2.charAt(0) !== "2" {
res
->Fetch.Response.json
->then(data => {
logApi(
~optLogger,
~url=uri,
~data,
~statusCode,
~type_="err",
~eventName=CUSTOMER_PAYMENT_METHODS_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
resolve()
})
->ignore
} else {
logApi(
~optLogger,
~url=uri,
~statusCode,
~type_="response",
~eventName=CUSTOMER_PAYMENT_METHODS_CALL,
~logType=INFO,
~logCategory=API,
(),
)
}
res->Fetch.Response.json
})
->then(res => resolve(res))
->catch(e => {
Js.log2("Unable to retrieve customer details because of ", e)
Js.Dict.empty()->Js.Json.object_->resolve
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let make = (
let sessionsPromise = PaymentHelpers.useSessions(
~clientSecret=clientSecretId,
~publishableKey,
~optLogger=Some(logger),
(),
)
let paymentMethodListPromise = PaymentHelpers.usePaymentMethodList(
Expand Down Expand Up @@ -125,6 +126,7 @@ let make = (
let customerDetailsPromise = PaymentHelpers.useCustomerDetails(
~clientSecret=clientSecretId,
~publishableKey,
~optLogger=Some(logger),
)
open Promise
customerDetailsPromise
Expand Down
Loading

0 comments on commit 2ac5118

Please sign in to comment.