Skip to content

Commit

Permalink
fix: Invalid shipping address and taxjar api failing case handle in a…
Browse files Browse the repository at this point in the history
…pple pay (#628)

Co-authored-by: Sagnik Mitra <[email protected]>
  • Loading branch information
ImSagnik007 and Sagnik Mitra authored Sep 20, 2024
1 parent 4c16c6c commit d4f3c41
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
70 changes: 37 additions & 33 deletions src/Utilities/ApplePayHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -127,40 +127,44 @@ let startApplePaySession = (
~clientSecret,
~paymentMethodType,
)
->then(response => {
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->Float.toString,
\"type": "final",
}
let newLineItems: array<lineItem> = [
{
label: "Bag Subtotal",
amount: (netAmount -. ordertaxAmount -. shippingCost)->Float.toString,
\"type": "final",
},
{
label: "Order Tax Amount",
amount: ordertaxAmount->Float.toString,
\"type": "final",
},
{
label: "Shipping Cost",
amount: shippingCost->Float.toString,
\"type": "final",
},
]
let updatedOrderDetails: updatedOrderDetails = {
newTotal,
newLineItems,
->thenResolve(response => {
switch response->taxResponseToObjMapper {
| Some(taxCalculationResponse) => {
let (netAmount, ordertaxAmount, shippingCost) = (
taxCalculationResponse.net_amount,
taxCalculationResponse.order_tax_amount,
taxCalculationResponse.shipping_cost,
)
let newTotal: lineItem = {
label: "Net Amount",
amount: netAmount->minorUnitToString,
\"type": "final",
}
let newLineItems: array<lineItem> = [
{
label: "Bag Subtotal",
amount: (netAmount - ordertaxAmount - shippingCost)->minorUnitToString,
\"type": "final",
},
{
label: "Order Tax Amount",
amount: ordertaxAmount->minorUnitToString,
\"type": "final",
},
{
label: "Shipping Cost",
amount: shippingCost->minorUnitToString,
\"type": "final",
},
]
let updatedOrderDetails: updatedOrderDetails = {
newTotal,
newLineItems,
}
ssn.completeShippingContactSelection(updatedOrderDetails)
}
| None => ssn.abort()
}
ssn.completeShippingContactSelection(updatedOrderDetails)->resolve
})
->ignore
}
Expand Down
22 changes: 11 additions & 11 deletions src/Utilities/TaxCalculation.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ open Utils

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

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),
}
resp
->JSON.Decode.object
->Option.map(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 calculateTax = (
Expand Down
2 changes: 2 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1377,3 +1377,5 @@ let getFirstAndLastNameFromFullName = fullName => {
}

let checkIsTestCardWildcard = val => ["1111222233334444"]->Array.includes(val)

let minorUnitToString = val => (val->Int.toFloat /. 100.)->Float.toString

0 comments on commit d4f3c41

Please sign in to comment.