Skip to content

Commit

Permalink
fix: HS-148: Added Surcharge support for rate (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushKapoorJuspay authored Nov 21, 2023
1 parent 6dcf0c0 commit 21d862c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
11 changes: 9 additions & 2 deletions public/icons/orca.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions src/Components/Surcharge.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ let make = (~list, ~paymentMethod, ~paymentMethodType, ~cardBrand=CardUtils.NOTF

let {localeString} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom)

let getSurchargeValue = (surchargeDetails: PaymentMethodsRecord.surchargeDetails) => {
switch surchargeDetails.surcharge.value {
| VAL(val) => val
| PERCENTAGE(percent) => percent.percentage
}->Js.Float.toString
}

let getMessage = (surchargeDetails: PaymentMethodsRecord.surchargeDetails) => {
let surchargeType = surchargeDetails.surcharge.surchargeType
let surchargeValue = surchargeDetails.surcharge.value->Js.Float.toString
let surchargeValue = surchargeDetails->getSurchargeValue

let getLocaleStrForSurcharge = (cardLocale, altPaymentLocale) => {
paymentMethod === "card" ? cardLocale(surchargeValue) : altPaymentLocale(surchargeValue)
Expand All @@ -28,7 +35,7 @@ let make = (~list, ~paymentMethod, ~paymentMethodType, ~cardBrand=CardUtils.NOTF
localeString.surchargeMsgAmount,
),
)
| PERCENTAGE =>
| RATE =>
Some(
getLocaleStrForSurcharge(
localeString.surchargeMsgPercentageForCard,
Expand Down Expand Up @@ -58,7 +65,10 @@ let make = (~list, ~paymentMethod, ~paymentMethodType, ~cardBrand=CardUtils.NOTF

switch (debitCardNetwork.surcharge_details, creditCardNetwork.surcharge_details) {
| (Some(debitSurchargeDetails), Some(creditSurchargeDetails)) =>
if creditSurchargeDetails.surcharge.value >= debitSurchargeDetails.surcharge.value {
let creditCardSurcharge = creditSurchargeDetails->getSurchargeValue
let debitCardSurcharge = debitSurchargeDetails->getSurchargeValue

if creditCardSurcharge >= debitCardSurcharge {
creditSurchargeDetails->getMessage
} else {
debitSurchargeDetails->getMessage
Expand All @@ -78,7 +88,7 @@ let make = (~list, ~paymentMethod, ~paymentMethodType, ~cardBrand=CardUtils.NOTF
| Some(surchargeMessage) =>
<div className="flex items-center text-xs mt-2">
<Icon name="asterisk" size=8 className="text-red-600 mr-1" />
<span> {React.string(surchargeMessage)} </span>
<span className="text-left"> {React.string(surchargeMessage)} </span>
</div>
| None => React.null
}
Expand Down
39 changes: 28 additions & 11 deletions src/Payments/PaymentMethodsRecord.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ type bankNames = {
eligible_connectors: array<string>,
}

type surchargeType = FIXED | PERCENTAGE | NONE
type surchargeType = FIXED | RATE | NONE
type surchargeValue =
| VAL(float)
| PERCENTAGE({percentage: float})

type surcharge = {
surchargeType: surchargeType,
value: float,
value: surchargeValue,
}

type surchargeDetails = {surcharge: surcharge}
Expand Down Expand Up @@ -663,7 +666,7 @@ let getPaymentExperience = (dict, str) => {
let getSurchargeTypeFromStr = str => {
switch str->Js.String2.toLowerCase {
| "fixed" => FIXED
| "percentage" => PERCENTAGE
| "rate" => RATE
| _ => NONE
}
}
Expand All @@ -678,19 +681,33 @@ let getSurchargeDetails = dict => {
->Belt.Option.getWithDefault(Js.Dict.empty())

let surchargeType = surchargDetails->Utils.getString("type", "none")->getSurchargeTypeFromStr
let surchargeVal =
surchargDetails
->Js.Dict.get("value")
->Belt.Option.getWithDefault(Js.Json.null)
->Js.Json.decodeNumber
->Belt.Option.getWithDefault(0.0)
let getSurchargeVal = switch surchargeType {
| RATE =>
PERCENTAGE({
percentage: surchargDetails
->Js.Dict.get("value")
->Belt.Option.flatMap(Js.Json.decodeObject)
->Belt.Option.flatMap(x => x->Js.Dict.get("percentage"))
->Belt.Option.flatMap(Js.Json.decodeNumber)
->Belt.Option.getWithDefault(0.0),
})
| FIXED
| _ =>
VAL(
surchargDetails
->Js.Dict.get("value")
->Belt.Option.flatMap(Js.Json.decodeNumber)
->Belt.Option.getWithDefault(0.0),
)
}

switch surchargeType {
| FIXED
| PERCENTAGE =>
| RATE =>
Some({
surcharge: {
surchargeType: surchargeType,
value: surchargeVal,
value: getSurchargeVal,
},
})
| _ => None
Expand Down

0 comments on commit 21d862c

Please sign in to comment.