diff --git a/shared-code/Utils/LogUtils.res b/shared-code/Utils/LogUtils.res new file mode 100644 index 000000000..80872da8f --- /dev/null +++ b/shared-code/Utils/LogUtils.res @@ -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)]) + } + } +} diff --git a/src/Utilities/LoggerUtils.res b/src/Utilities/LoggerUtils.res index fd9b4570c..98c6ff23f 100644 --- a/src/Utilities/LoggerUtils.res +++ b/src/Utilities/LoggerUtils.res @@ -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, @@ -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( diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index 262cd786f..5dd928480 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -896,9 +896,7 @@ let usePaymentSync = (optLogger: option, 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}` @@ -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 = @@ -1153,9 +1151,9 @@ let useCompleteAuthorize = (optLogger: option, 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` @@ -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 = [ @@ -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, @@ -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), @@ -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), @@ -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, @@ -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)] @@ -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, @@ -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, @@ -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, @@ -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), @@ -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), diff --git a/src/orca-log-catcher/OrcaLogger.res b/src/orca-log-catcher/OrcaLogger.res index bdcc4e4cb..3af9c82f6 100644 --- a/src/orca-log-catcher/OrcaLogger.res +++ b/src/orca-log-catcher/OrcaLogger.res @@ -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 @@ -240,7 +240,7 @@ type loggerMake = { ~logType: logType=?, ~logCategory: logCategory=?, ~paymentMethod: string=?, - ~apiLogType: apiLogType=?, + ~apiLogType: LogUtils.apiLogType=?, ~isPaymentSession: bool=?, ) => unit, setLogInitiated: unit => unit, @@ -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 => { @@ -798,7 +798,7 @@ let make = ( ~logType=INFO, ~logCategory=API, ~paymentMethod="", - ~apiLogType=Request, + ~apiLogType=LogUtils.Request, ~isPaymentSession=false, ) => { let eventNameStr = eventName->eventNameToStrMapper