Skip to content

Commit

Permalink
refactor: moved part of LoggerUtils to shared code base
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragKV-Juspay committed Oct 4, 2024
1 parent e0194e4 commit baaef9d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
45 changes: 45 additions & 0 deletions shared-code/Utils/LogUtils.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
type apiLogType = Request | Response | NoResponse | Err | Method

let getApiLogValues = (
apiLogType: apiLogType,
url,
statusCode,
data,
~paymentMethod=?,
~result=?,
) => {
switch apiLogType {
| Request => ([("url", url->JSON.Encode.string)], [])
| Response => (
[("url", url->JSON.Encode.string), ("statusCode", statusCode->JSON.Encode.string)],
[("response", data)],
)
| NoResponse => (
[
("url", url->JSON.Encode.string),
("statusCode", "504"->JSON.Encode.string),
("response", data),
],
[("response", data)],
)
| Err => (
[
("url", url->JSON.Encode.string),
("statusCode", statusCode->JSON.Encode.string),
("response", data),
],
[("response", data)],
)
| Method => {
let methodValue = switch paymentMethod {
| Some(method) => method->JSON.Encode.string
| None => ""->JSON.Encode.string
}
let resultValue = switch result {
| Some(res) => res
| None => Dict.make()->JSON.Encode.object
}
([("method", methodValue)], [("result", resultValue)])
}
}
}
28 changes: 3 additions & 25 deletions src/Utilities/LoggerUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let logApi = (
~eventName,
~statusCode="",
~data: JSON.t=Dict.make()->JSON.Encode.object,
~apiLogType: OrcaLogger.apiLogType,
~apiLogType: LogUtils.apiLogType,
~url="",
~paymentMethod="",
~result: JSON.t=Dict.make()->JSON.Encode.object,
Expand All @@ -11,30 +11,8 @@ let logApi = (
~logCategory: OrcaLogger.logCategory=API,
~isPaymentSession: bool=false,
) => {
let (value, internalMetadata) = switch apiLogType {
| Request => ([("url", url->JSON.Encode.string)], [])
| Response => (
[("url", url->JSON.Encode.string), ("statusCode", statusCode->JSON.Encode.string)],
[("response", data)],
)
| NoResponse => (
[
("url", url->JSON.Encode.string),
("statusCode", "504"->JSON.Encode.string),
("response", data),
],
[("response", data)],
)
| Err => (
[
("url", url->JSON.Encode.string),
("statusCode", statusCode->JSON.Encode.string),
("response", data),
],
[("response", data)],
)
| Method => ([("method", paymentMethod->JSON.Encode.string)], [("result", result)])
}
let (value, internalMetadata) = LogUtils.getApiLogValues(apiLogType, url, statusCode, data)

switch optLogger {
| Some(logger) =>
logger.setLogApi(
Expand Down
55 changes: 16 additions & 39 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,7 @@ let usePaymentSync = (optLogger: option<OrcaLogger.loggerMake>, paymentType: pay
switch keys.clientSecret {
| Some(clientSecret) =>
let paymentIntentID = clientSecret->getPaymentId
let headers = getHeader(
~apiKey=confirmParam.publishableKey,
)
let headers = getHeader(~apiKey=confirmParam.publishableKey)
let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey=confirmParam.publishableKey)
let uri = `${endpoint}/payments/${paymentIntentID}?force_sync=true&client_secret=${clientSecret}`

Expand Down Expand Up @@ -981,9 +979,9 @@ let usePaymentIntent = (optLogger, paymentType) => {
let paymentIntentID = clientSecret->getPaymentId
let headers = getHeader(
~apiKey=confirmParam.publishableKey,
~clientSource=paymentTypeFromUrl->CardThemeType.getPaymentModeToStrMapper
~clientSource=paymentTypeFromUrl->CardThemeType.getPaymentModeToStrMapper,
)

let returnUrlArr = [("return_url", confirmParam.return_url->JSON.Encode.string)]
let manual_retry = manualRetry ? [("retry_action", "manual_retry"->JSON.Encode.string)] : []
let body =
Expand Down Expand Up @@ -1153,9 +1151,9 @@ let useCompleteAuthorize = (optLogger: option<OrcaLogger.loggerMake>, paymentTyp
switch keys.clientSecret {
| Some(clientSecret) =>
let paymentIntentID = clientSecret->getPaymentId
let headers = getHeader(
let headers = getHeader(
~apiKey=confirmParam.publishableKey,
~clientSource=paymentTypeFromUrl->CardThemeType.getPaymentModeToStrMapper
~clientSource=paymentTypeFromUrl->CardThemeType.getPaymentModeToStrMapper,
)
let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey=confirmParam.publishableKey)
let uri = `${endpoint}/payments/${paymentIntentID}/complete_authorize`
Expand Down Expand Up @@ -1212,10 +1210,7 @@ let fetchSessions = (
) => {
open Promise
let uri = `${endpoint}/payments/session_tokens`
let headers = getHeader(
~apiKey=publishableKey,
~merchantHostname=merchantHostname,
)
let headers = getHeader(~apiKey=publishableKey, ~merchantHostname)
let paymentIntentID = clientSecret->getPaymentId
let body =
[
Expand Down Expand Up @@ -1290,9 +1285,7 @@ let fetchSessions = (

let confirmPayout = (~clientSecret, ~publishableKey, ~logger, ~customPodUri, ~uri, ~body) => {
open Promise
let headers = getHeader(
~apiKey=publishableKey,
)
let headers = getHeader(~apiKey=publishableKey)
logApi(
~optLogger=Some(logger),
~url=uri,
Expand Down Expand Up @@ -1367,9 +1360,7 @@ let createPaymentMethod = (
~body,
) => {
open Promise
let headers = getHeader(
~apiKey=publishableKey,
)
let headers = getHeader(~apiKey=publishableKey)
let uri = `${endpoint}/payment_methods`
logApi(
~optLogger=Some(logger),
Expand Down Expand Up @@ -1444,9 +1435,7 @@ let fetchPaymentMethodList = (
~endpoint,
) => {
open Promise
let headers = getHeader(
~apiKey=publishableKey,
)
let headers = getHeader(~apiKey=publishableKey)
let uri = `${endpoint}/account/payment_methods?client_secret=${clientSecret}`
logApi(
~optLogger=Some(logger),
Expand Down Expand Up @@ -1512,9 +1501,7 @@ let fetchCustomerPaymentMethodList = (
~isPaymentSession=false,
) => {
open Promise
let headers = getHeader(
~apiKey=publishableKey,
)
let headers = getHeader(~apiKey=publishableKey)
let uri = `${endpoint}/customers/payment_methods?client_secret=${clientSecret}`
logApi(
~optLogger,
Expand Down Expand Up @@ -1606,9 +1593,7 @@ let paymentIntentForPaymentSession = (
~isConfirmCall=true,
)
let uri = `${endpoint}/payments/${paymentIntentID}/confirm`
let headers = getHeader(
~apiKey=confirmParam.publishableKey,
)
let headers = getHeader(~apiKey=confirmParam.publishableKey)
let broswerInfo = BrowserSpec.broswerInfo()
let returnUrlArr = [("return_url", confirmParam.return_url->JSON.Encode.string)]

Expand Down Expand Up @@ -1653,9 +1638,7 @@ let callAuthLink = (
open Promise
let endpoint = ApiEndpoint.getApiEndPoint()
let uri = `${endpoint}/payment_methods/auth/link`
let headers = getHeader(
~apiKey=publishableKey,
)->Dict.fromArray
let headers = getHeader(~apiKey=publishableKey)->Dict.fromArray
logApi(
~optLogger,
~url=uri,
Expand Down Expand Up @@ -1763,9 +1746,7 @@ let callAuthExchange = (
("payment_method_type", paymentMethodType->JSON.Encode.string),
("public_token", publicToken->JSON.Encode.string),
]
let headers = getHeader(
~apiKey=publishableKey,
)->Dict.fromArray
let headers = getHeader(~apiKey=publishableKey)->Dict.fromArray
logApi(
~optLogger,
~url=uri,
Expand Down Expand Up @@ -1861,9 +1842,7 @@ let fetchSavedPaymentMethodList = (
~isPaymentSession=false,
) => {
open Promise
let headers = getHeader(
~apiKey=ephemeralKey,
)
let headers = getHeader(~apiKey=ephemeralKey)
let uri = `${endpoint}/customers/payment_methods`
logApi(
~optLogger,
Expand Down Expand Up @@ -1927,9 +1906,7 @@ let fetchSavedPaymentMethodList = (
let deletePaymentMethod = (~ephemeralKey, ~paymentMethodId, ~logger, ~customPodUri) => {
open Promise
let endpoint = ApiEndpoint.getApiEndPoint()
let headers = getHeader(
~apiKey=ephemeralKey,
)
let headers = getHeader(~apiKey=ephemeralKey)
let uri = `${endpoint}/payment_methods/${paymentMethodId}`
logApi(
~optLogger=Some(logger),
Expand Down Expand Up @@ -1997,7 +1974,7 @@ let calculateTax = (
) => {
open Promise
let endpoint = ApiEndpoint.getApiEndPoint()
let headers = getHeader(~apiKey=apiKey,)
let headers = getHeader(~apiKey)
let uri = `${endpoint}/payments/${paymentId}/calculate_tax`
let body = [
("client_secret", clientSecret),
Expand Down
8 changes: 4 additions & 4 deletions src/orca-log-catcher/OrcaLogger.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type apiLogType = Request | Response | NoResponse | Method | Err
// type apiLogType = Request | Response | NoResponse | Method | Err
type logType = DEBUG | INFO | ERROR | WARNING | SILENT
type logCategory = API | USER_ERROR | USER_EVENT | MERCHANT_EVENT

Expand Down Expand Up @@ -240,7 +240,7 @@ type loggerMake = {
~logType: logType=?,
~logCategory: logCategory=?,
~paymentMethod: string=?,
~apiLogType: apiLogType=?,
~apiLogType: LogUtils.apiLogType=?,
~isPaymentSession: bool=?,
) => unit,
setLogInitiated: unit => unit,
Expand Down Expand Up @@ -641,7 +641,7 @@ let make = (
}
}

let calculateLatencyHook = (~eventName, ~apiLogType=Method) => {
let calculateLatencyHook = (~eventName, ~apiLogType=LogUtils.Method) => {
let currentTimestamp = Date.now()
let latency = switch eventName {
| PAYMENT_ATTEMPT => {
Expand Down Expand Up @@ -798,7 +798,7 @@ let make = (
~logType=INFO,
~logCategory=API,
~paymentMethod="",
~apiLogType=Request,
~apiLogType=LogUtils.Request,
~isPaymentSession=false,
) => {
let eventNameStr = eventName->eventNameToStrMapper
Expand Down

0 comments on commit baaef9d

Please sign in to comment.