Skip to content

Commit

Permalink
fix: dynamic tax calculation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagnik Mitra authored and Sagnik Mitra committed Sep 16, 2024
1 parent 477effc commit 4136ffe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/Utilities/ApplePayHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,31 @@ let startApplePaySession = (
~paymentMethodType,
)
->then(response => {
let taxCalculationResponse = response->getDictFromJson->taxResponseToObjMapper
let taxCalculationResponse = response->taxResponseToObjMapper
let (netAmount, ordertaxAmount, shippingCost) = (
taxCalculationResponse.net_amount,
taxCalculationResponse.order_tax_amount,
taxCalculationResponse.shipping_cost,
)
let newTotal: lineItem = {
label: "Net Amount",
amount: netAmount->Int.toString,
amount: netAmount->Float.toString,
\"type": "final",
}
let newLineItems: array<lineItem> = [
{
label: "Bag Subtotal",
amount: (netAmount - ordertaxAmount - shippingCost)->Int.toString,
amount: (netAmount -. ordertaxAmount -. shippingCost)->Float.toString,
\"type": "final",
},
{
label: "Order Tax Amount",
amount: ordertaxAmount->Int.toString,
amount: ordertaxAmount->Float.toString,
\"type": "final",
},
{
label: "Shipping Cost",
amount: shippingCost->Int.toString,
amount: shippingCost->Float.toString,
\"type": "final",
},
]
Expand Down
28 changes: 19 additions & 9 deletions src/Utilities/TaxCalculation.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ open Utils

type calculateTaxResponse = {
payment_id: string,
net_amount: int,
order_tax_amount: int,
shipping_cost: int,
net_amount: float,
order_tax_amount: float,
shipping_cost: float,
}

let taxResponseToObjMapper = dict => {
payment_id: dict->getString("payment_id", ""),
net_amount: dict->getInt("net_amount", 0),
order_tax_amount: dict->getInt("order_tax_amount", 0),
shipping_cost: dict->getInt("shipping_cost", 0),
let taxResponseToObjMapper = resp => {
let responseDict = resp->getDictFromJson
let displayAmountDict = responseDict->getDictFromDict("display_amount")
{
payment_id: responseDict->getString("payment_id", ""),
net_amount: displayAmountDict->getFloat("net_amount", 0.0),
order_tax_amount: displayAmountDict->getFloat("order_tax_amount", 0.0),
shipping_cost: displayAmountDict->getFloat("shipping_cost", 0.0),
}
}

let calculateTax = (~shippingAddress, ~logger, ~clientSecret, ~publishableKey, ~paymentMethodType) => {
let calculateTax = (
~shippingAddress,
~logger,
~clientSecret,
~publishableKey,
~paymentMethodType,
) => {
PaymentHelpers.calculateTax(
~clientSecret=clientSecret->JSON.Encode.string,
~apiKey=publishableKey,
Expand Down

0 comments on commit 4136ffe

Please sign in to comment.