From ea72384f91e5dd0840d5ab3ed02b6772ff6c3270 Mon Sep 17 00:00:00 2001 From: Sagnik Mitra Date: Wed, 30 Oct 2024 18:52:38 +0530 Subject: [PATCH] fix: google pay log issue fix --- src/Components/SavedMethods.res | 1 - src/Payments/GPay.res | 1 - src/Utilities/GooglePayHelpers.res | 7 - src/orca-loader/Elements.res | 226 ++++++++++++++++------------- 4 files changed, 126 insertions(+), 109 deletions(-) diff --git a/src/Components/SavedMethods.res b/src/Components/SavedMethods.res index 2516e9ca..ae2dbdf1 100644 --- a/src/Components/SavedMethods.res +++ b/src/Components/SavedMethods.res @@ -141,7 +141,6 @@ let make = ( GooglePayHelpers.handleGooglePayClicked( ~sessionObj=optToken, ~componentName, - ~paymentMethodListValue, ~iframeId, ~readOnly, ) diff --git a/src/Payments/GPay.res b/src/Payments/GPay.res index 26ba4e17..d9e0c69d 100644 --- a/src/Payments/GPay.res +++ b/src/Payments/GPay.res @@ -127,7 +127,6 @@ let make = ( GooglePayHelpers.handleGooglePayClicked( ~sessionObj, ~componentName, - ~paymentMethodListValue, ~iframeId, ~readOnly=options.readOnly, ) diff --git a/src/Utilities/GooglePayHelpers.res b/src/Utilities/GooglePayHelpers.res index a8007b69..1f1118e5 100644 --- a/src/Utilities/GooglePayHelpers.res +++ b/src/Utilities/GooglePayHelpers.res @@ -156,7 +156,6 @@ let handleGooglePayClicked = ( ~componentName, ~iframeId, ~readOnly, - ~paymentMethodListValue: PaymentMethodsRecord.paymentMethodList, ) => { let paymentDataRequest = GooglePayType.getPaymentDataFromSession(~sessionObj, ~componentName) messageParentWindow([ @@ -168,10 +167,6 @@ let handleGooglePayClicked = ( messageParentWindow([ ("GpayClicked", true->JSON.Encode.bool), ("GpayPaymentDataRequest", paymentDataRequest->Identity.anyTypeToJson), - ( - "IsTaxCalculationEnabled", - paymentMethodListValue.is_tax_calculation_enabled->JSON.Encode.bool, - ), ]) } } @@ -179,7 +174,6 @@ let handleGooglePayClicked = ( let useSubmitCallback = (~isWallet, ~sessionObj, ~componentName) => { let areRequiredFieldsValid = Recoil.useRecoilValueFromAtom(RecoilAtoms.areRequiredFieldsValid) let areRequiredFieldsEmpty = Recoil.useRecoilValueFromAtom(RecoilAtoms.areRequiredFieldsEmpty) - let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue) let options = Recoil.useRecoilValueFromAtom(RecoilAtoms.optionAtom) let {localeString} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom) let {iframeId} = Recoil.useRecoilValueFromAtom(RecoilAtoms.keys) @@ -192,7 +186,6 @@ let useSubmitCallback = (~isWallet, ~sessionObj, ~componentName) => { handleGooglePayClicked( ~sessionObj, ~componentName, - ~paymentMethodListValue, ~iframeId, ~readOnly=options.readOnly, ) diff --git a/src/orca-loader/Elements.res b/src/orca-loader/Elements.res index a44ecdd3..f7a1911b 100644 --- a/src/orca-loader/Elements.res +++ b/src/orca-loader/Elements.res @@ -89,6 +89,7 @@ let make = ( let clientSecretReMatch = Re.test(`.+_secret_[A-Za-z0-9]+`->Re.fromString, clientSecret) let preMountLoaderIframeDiv = mountPreMountLoaderIframe() + let isTaxCalculationEnabled = ref(false) let unMountPreMountLoaderIframe = () => { switch preMountLoaderIframeDiv->Nullable.toOption { @@ -133,6 +134,8 @@ let make = ( let dict = json->getDictFromJson let isPaymentMethodsData = dict->getString("data", "") === "payment_methods" if isPaymentMethodsData { + isTaxCalculationEnabled.contents = + dict->getDictFromDict("response")->getBool("is_tax_calculation_enabled", false) addSmartEventListener("message", onPlaidCallback(mountedIframeRef), "onPlaidCallback") let json = dict->getJsonFromDict("response", JSON.Encode.null) @@ -917,110 +920,133 @@ let make = ( try { let transactionInfo = gpayobj.transaction_info->getDictFromJson - let handleGooglePayMessages = (event: Types.event) => { - let evJson = event.data->anyTypeToJson - let isTaxCalculationEnabled = - evJson - ->getOptionalJsonFromJson("IsTaxCalculationEnabled") - ->Option.flatMap(JSON.Decode.bool) - ->Option.getOr(false) - - let onPaymentDataChanged = intermediatePaymentData => { - let shippingAddress = - intermediatePaymentData - ->getDictFromJson - ->getDictFromDict("shippingAddress") - ->billingContactItemToObjMapper - let newShippingAddress = + let onPaymentDataChanged = intermediatePaymentData => { + let shippingAddress = + intermediatePaymentData + ->getDictFromJson + ->getDictFromDict("shippingAddress") + ->billingContactItemToObjMapper + let newShippingAddress = + [ + ("state", shippingAddress.administrativeArea->JSON.Encode.string), + ("country", shippingAddress.countryCode->JSON.Encode.string), + ("zip", shippingAddress.postalCode->JSON.Encode.string), + ]->getJsonFromArrayOfJson + + let paymentMethodType = "google_pay"->JSON.Encode.string + + let currentPaymentRequest = [ + ( + "newTransactionInfo", [ - ("state", shippingAddress.administrativeArea->JSON.Encode.string), - ("country", shippingAddress.countryCode->JSON.Encode.string), - ("zip", shippingAddress.postalCode->JSON.Encode.string), - ]->getJsonFromArrayOfJson - - let paymentMethodType = "google_pay"->JSON.Encode.string - - if isTaxCalculationEnabled { - TaxCalculation.calculateTax( - ~shippingAddress=[ - ("address", newShippingAddress), - ]->getJsonFromArrayOfJson, - ~logger, - ~publishableKey, - ~clientSecret, - ~paymentMethodType, - )->then(resp => { - switch resp->TaxCalculation.taxResponseToObjMapper { - | Some(taxCalculationResponse) => { - let updatePaymentRequest = [ - ( - "newTransactionInfo", - [ - ( - "countryCode", - shippingAddress.countryCode->JSON.Encode.string, - ), - ( - "currencyCode", - transactionInfo - ->getString("currency_code", "") - ->JSON.Encode.string, - ), - ("totalPriceStatus", "FINAL"->JSON.Encode.string), - ( - "totalPrice", - taxCalculationResponse.net_amount - ->minorUnitToString - ->JSON.Encode.string, - ), - ]->getJsonFromArrayOfJson, - ), - ]->getJsonFromArrayOfJson - updatePaymentRequest->resolve - } - | None => JSON.Encode.null->resolve + ( + "countryCode", + transactionInfo + ->getString("country_code", "") + ->JSON.Encode.string, + ), + ( + "currencyCode", + transactionInfo + ->getString("currency_code", "") + ->JSON.Encode.string, + ), + ("totalPriceStatus", "FINAL"->JSON.Encode.string), + ( + "totalPrice", + transactionInfo + ->getString("total_price", "") + ->JSON.Encode.string, + ), + ]->getJsonFromArrayOfJson, + ), + ]->getJsonFromArrayOfJson + + if isTaxCalculationEnabled.contents { + TaxCalculation.calculateTax( + ~shippingAddress=[ + ("address", newShippingAddress), + ]->getJsonFromArrayOfJson, + ~logger, + ~publishableKey, + ~clientSecret, + ~paymentMethodType, + )->then(resp => { + switch resp->TaxCalculation.taxResponseToObjMapper { + | Some(taxCalculationResponse) => { + let updatePaymentRequest = [ + ( + "newTransactionInfo", + [ + ( + "countryCode", + shippingAddress.countryCode->JSON.Encode.string, + ), + ( + "currencyCode", + transactionInfo + ->getString("currency_code", "") + ->JSON.Encode.string, + ), + ("totalPriceStatus", "FINAL"->JSON.Encode.string), + ( + "totalPrice", + taxCalculationResponse.net_amount + ->minorUnitToString + ->JSON.Encode.string, + ), + ]->getJsonFromArrayOfJson, + ), + ]->getJsonFromArrayOfJson + updatePaymentRequest->resolve } - }) - } else { - JSON.Encode.null->resolve - } - } - let gpayClientRequest = if componentType->getIsExpressCheckoutComponent { - { - "environment": publishableKey->String.startsWith("pk_prd_") - ? "PRODUCTION" - : "TEST", - "paymentDataCallbacks": { - "onPaymentDataChanged": onPaymentDataChanged, - }, - }->Identity.anyTypeToJson + | None => currentPaymentRequest->resolve + } + }) } else { - { - "environment": publishableKey->String.startsWith("pk_prd_") - ? "PRODUCTION" - : "TEST", - }->Identity.anyTypeToJson + currentPaymentRequest->resolve } - let gPayClient = GooglePayType.google(gpayClientRequest) - - gPayClient.isReadyToPay(payRequest) - ->then(res => { - let dict = res->getDictFromJson - let isReadyToPay = getBool(dict, "result", false) - let msg = [("isReadyToPay", isReadyToPay->JSON.Encode.bool)]->Dict.fromArray - mountedIframeRef->Window.iframePostMessage(msg) - resolve() - }) - ->catch(err => { - logger.setLogInfo( - ~value=err->Identity.anyTypeToJson->JSON.stringify, - ~eventName=GOOGLE_PAY_FLOW, - ~paymentMethod="GOOGLE_PAY", - ~logType=DEBUG, - ) - resolve() - }) - ->ignore + } + let gpayClientRequest = if componentType->getIsExpressCheckoutComponent { + { + "environment": publishableKey->String.startsWith("pk_prd_") + ? "PRODUCTION" + : "TEST", + "paymentDataCallbacks": { + "onPaymentDataChanged": onPaymentDataChanged, + }, + }->Identity.anyTypeToJson + } else { + { + "environment": publishableKey->String.startsWith("pk_prd_") + ? "PRODUCTION" + : "TEST", + }->Identity.anyTypeToJson + } + let gPayClient = GooglePayType.google(gpayClientRequest) + + gPayClient.isReadyToPay(payRequest) + ->then(res => { + let dict = res->getDictFromJson + let isReadyToPay = getBool(dict, "result", false) + let msg = [("isReadyToPay", isReadyToPay->JSON.Encode.bool)]->Dict.fromArray + mountedIframeRef->Window.iframePostMessage(msg) + resolve() + }) + ->catch(err => { + logger.setLogInfo( + ~value=err->Identity.anyTypeToJson->JSON.stringify, + ~eventName=GOOGLE_PAY_FLOW, + ~paymentMethod="GOOGLE_PAY", + ~logType=DEBUG, + ) + resolve() + }) + ->ignore + + let handleGooglePayMessages = (event: Types.event) => { + let evJson = event.data->anyTypeToJson + let gpayClicked = evJson ->getOptionalJsonFromJson("GpayClicked")