Skip to content

Commit

Permalink
fix: google pay log issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSagnik007 committed Oct 30, 2024
1 parent 76adf80 commit e30069b
Showing 1 changed file with 100 additions and 101 deletions.
201 changes: 100 additions & 101 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -917,110 +920,106 @@ 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 =
[
("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
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

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 => JSON.Encode.null->resolve
}
})
} else {
{
"environment": publishableKey->String.startsWith("pk_prd_")
? "PRODUCTION"
: "TEST",
}->Identity.anyTypeToJson
JSON.Encode.null->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")
Expand Down

0 comments on commit e30069b

Please sign in to comment.