Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Google pay log issue fix #762

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Components/SavedMethods.res
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ let make = (
GooglePayHelpers.handleGooglePayClicked(
~sessionObj=optToken,
~componentName,
~paymentMethodListValue,
~iframeId,
~readOnly,
)
Expand Down
1 change: 0 additions & 1 deletion src/Payments/GPay.res
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ let make = (
GooglePayHelpers.handleGooglePayClicked(
~sessionObj,
~componentName,
~paymentMethodListValue,
~iframeId,
~readOnly=options.readOnly,
)
Expand Down
7 changes: 0 additions & 7 deletions src/Utilities/GooglePayHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ let handleGooglePayClicked = (
~componentName,
~iframeId,
~readOnly,
~paymentMethodListValue: PaymentMethodsRecord.paymentMethodList,
) => {
let paymentDataRequest = GooglePayType.getPaymentDataFromSession(~sessionObj, ~componentName)
messageParentWindow([
Expand All @@ -168,18 +167,13 @@ let handleGooglePayClicked = (
messageParentWindow([
("GpayClicked", true->JSON.Encode.bool),
("GpayPaymentDataRequest", paymentDataRequest->Identity.anyTypeToJson),
(
"IsTaxCalculationEnabled",
paymentMethodListValue.is_tax_calculation_enabled->JSON.Encode.bool,
),
])
}
}

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)
Expand All @@ -192,7 +186,6 @@ let useSubmitCallback = (~isWallet, ~sessionObj, ~componentName) => {
handleGooglePayClicked(
~sessionObj,
~componentName,
~paymentMethodListValue,
~iframeId,
~readOnly=options.readOnly,
)
Expand Down
226 changes: 126 additions & 100 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,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")
Expand Down
Loading