Skip to content

Commit

Permalink
feat: tax calculation in google pay
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSagnik007 committed Oct 25, 2024
1 parent 6056997 commit 6364dc7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Components/SavedMethods.res
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ let make = (
GooglePayHelpers.handleGooglePayClicked(
~sessionObj=optToken,
~componentName,
~paymentMethodListValue,
~iframeId,
~readOnly,
)
Expand Down
1 change: 1 addition & 0 deletions src/Payments/GPay.res
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ let make = (
GooglePayHelpers.handleGooglePayClicked(
~sessionObj,
~componentName,
~paymentMethodListValue,
~iframeId,
~readOnly=options.readOnly,
)
Expand Down
2 changes: 2 additions & 0 deletions src/Types/GooglePayType.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type paymentDataRequest = {
mutable shippingAddressRequired: bool,
mutable emailRequired: bool,
mutable shippingAddressParameters: JSON.t,
mutable callbackIntents: array<JSON.t>,
}
@val @scope("Object") external assign2: (JSON.t, JSON.t) => paymentDataRequest = "assign"
type element = {
Expand Down Expand Up @@ -180,6 +181,7 @@ let getPaymentDataFromSession = (~sessionObj, ~componentName) => {
paymentDataRequest.shippingAddressRequired = gpayobj.shippingAddressRequired
paymentDataRequest.shippingAddressParameters =
gpayobj.shippingAddressParameters->transformKeys(CamelCase)
paymentDataRequest.callbackIntents = ["SHIPPING_ADDRESS"->JSON.Encode.string]
}

paymentDataRequest
Expand Down
21 changes: 19 additions & 2 deletions src/Utilities/GooglePayHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ let useHandleGooglePayResponse = (
}, (paymentMethodTypes, stateJson, isManualRetryEnabled, requiredFieldsBody, isWallet))
}

let handleGooglePayClicked = (~sessionObj, ~componentName, ~iframeId, ~readOnly) => {
let handleGooglePayClicked = (
~sessionObj,
~componentName,
~iframeId,
~readOnly,
~paymentMethodListValue: PaymentMethodsRecord.paymentMethodList,
) => {
let paymentDataRequest = GooglePayType.getPaymentDataFromSession(~sessionObj, ~componentName)
messageParentWindow([
("fullscreen", true->JSON.Encode.bool),
Expand All @@ -162,13 +168,18 @@ let handleGooglePayClicked = (~sessionObj, ~componentName, ~iframeId, ~readOnly)
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 @@ -178,7 +189,13 @@ let useSubmitCallback = (~isWallet, ~sessionObj, ~componentName) => {
let json = ev.data->safeParse
let confirm = json->getDictFromJson->ConfirmType.itemToObjMapper
if confirm.doSubmit && areRequiredFieldsValid && !areRequiredFieldsEmpty {
handleGooglePayClicked(~sessionObj, ~componentName, ~iframeId, ~readOnly=options.readOnly)
handleGooglePayClicked(
~sessionObj,
~componentName,
~paymentMethodListValue,
~iframeId,
~readOnly=options.readOnly,
)
} else if areRequiredFieldsEmpty {
postFailedSubmitResponse(
~errortype="validation_error",
Expand Down
123 changes: 97 additions & 26 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -876,35 +876,106 @@ let make = (
)

try {
let gPayClient = GooglePayType.google(
{
"environment": publishableKey->String.startsWith("pk_prd_")
? "PRODUCTION"
: "TEST",
}->Identity.anyTypeToJson,
)

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

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 gpayClicked =
evJson
->getOptionalJsonFromJson("GpayClicked")
Expand Down

0 comments on commit 6364dc7

Please sign in to comment.