Skip to content

Commit

Permalink
feat: HS-148: Added Surcharge for One Click Wallets (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushKapoorJuspay authored Jan 19, 2024
1 parent 4bf358e commit ee72b3f
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 81 deletions.
10 changes: 7 additions & 3 deletions src/Components/DynamicFields.res
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,14 @@ let make = (
!(PaymentMethodsRecord.dynamicFieldsToRenderOutsideBilling->Js.Array2.includes(field))
)

let isInfoElementPresent = dynamicFieldsToRenderInsideBilling->Js.Array2.includes(InfoElement)

let isOnlyInfoElementPresent =
dynamicFieldsToRenderInsideBilling->Js.Array2.length === 1 &&
dynamicFieldsToRenderInsideBilling->Belt.Array.get(0) === Some(InfoElement)
dynamicFieldsToRenderInsideBilling->Js.Array2.length === 1 && isInfoElementPresent

let isRenderDynamicFieldsInsideBilling =
dynamicFieldsToRenderInsideBilling->Js.Array2.length > 0 &&
(dynamicFieldsToRenderInsideBilling->Js.Array2.length > 1 || !isOnlyInfoElementPresent)
(dynamicFieldsToRenderInsideBilling->Js.Array2.length > 1 || !isOnlyInfoElementPresent)

{
fieldsArr->Js.Array2.length > 0
Expand Down Expand Up @@ -665,6 +666,9 @@ let make = (
}}
</>}
</RenderIf>
<RenderIf condition={!isInfoElementPresent}>
<Surcharge list paymentMethod paymentMethodType />
</RenderIf>
</>
: React.null
}
Expand Down
88 changes: 40 additions & 48 deletions src/Components/Surcharge.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
@react.component
let make = (~list, ~paymentMethod, ~paymentMethodType, ~cardBrand=CardUtils.NOTFOUND) => {
let make = (
~list,
~paymentMethod,
~paymentMethodType,
~cardBrand=CardUtils.NOTFOUND,
~isForWallets=false,
) => {
let getPaymentMethodTypes = paymentMethodType => {
PaymentMethodsRecord.getPaymentMethodTypeFromList(
~list,
Expand All @@ -10,66 +16,52 @@ let make = (~list, ~paymentMethod, ~paymentMethodType, ~cardBrand=CardUtils.NOTF

let paymentMethodTypes = paymentMethodType->getPaymentMethodTypes

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

let getMessage = (surchargeDetails: PaymentMethodsRecord.surchargeDetails) => {
let surchargeValue = surchargeDetails.displayTotalSurchargeAmount->Js.Float.toString

let getLocaleStrForSurcharge = (cardLocale, altPaymentLocale) => {
paymentMethod === "card" ? cardLocale(surchargeValue) : altPaymentLocale(surchargeValue)
}

Some(
getLocaleStrForSurcharge(
localeString.surchargeMsgAmountForCard,
localeString.surchargeMsgAmount,
),
)
}

let getCardNetwork = (paymentMethodType: PaymentMethodsRecord.paymentMethodTypes) => {
paymentMethodType.card_networks
->Js.Array2.filter(cardNetwork => cardNetwork.card_network === cardBrand)
->Belt.Array.get(0)
->Belt.Option.getWithDefault(PaymentMethodsRecord.defaultCardNetworks)
}

let getSurchargeMessage = () => {
switch paymentMethodTypes.surcharge_details {
| Some(surchargeDetails) => surchargeDetails->getMessage
| None =>
if paymentMethod === "card" {
let creditPaymentMethodTypes = getPaymentMethodTypes("credit")
if isForWallets {
SurchargeUtils.getOneClickWalletsMessage(~list)
} else {
switch paymentMethodTypes.surcharge_details {
| Some(surchargeDetails) => SurchargeUtils.getMessage(~paymentMethod, ~surchargeDetails)
| None =>
if paymentMethod === "card" {
let creditPaymentMethodTypes = getPaymentMethodTypes("credit")

let debitCardNetwork = paymentMethodTypes->getCardNetwork
let creditCardNetwork = creditPaymentMethodTypes->getCardNetwork
let debitCardNetwork = PaymentMethodsRecord.getCardNetwork(
~paymentMethodType=paymentMethodTypes,
~cardBrand,
)
let creditCardNetwork = PaymentMethodsRecord.getCardNetwork(
~paymentMethodType=creditPaymentMethodTypes,
~cardBrand,
)

switch (debitCardNetwork.surcharge_details, creditCardNetwork.surcharge_details) {
| (Some(debitSurchargeDetails), Some(creditSurchargeDetails)) =>
let creditCardSurcharge = creditSurchargeDetails.displayTotalSurchargeAmount
let debitCardSurcharge = debitSurchargeDetails.displayTotalSurchargeAmount
switch (debitCardNetwork.surcharge_details, creditCardNetwork.surcharge_details) {
| (Some(debitSurchargeDetails), Some(creditSurchargeDetails)) =>
let creditCardSurcharge = creditSurchargeDetails.displayTotalSurchargeAmount
let debitCardSurcharge = debitSurchargeDetails.displayTotalSurchargeAmount

if creditCardSurcharge >= debitCardSurcharge {
creditSurchargeDetails->getMessage
} else {
debitSurchargeDetails->getMessage
if creditCardSurcharge >= debitCardSurcharge {
SurchargeUtils.getMessage(~paymentMethod, ~surchargeDetails={creditSurchargeDetails})
} else {
SurchargeUtils.getMessage(~paymentMethod, ~surchargeDetails={debitSurchargeDetails})
}
| (None, Some(surchargeDetails))
| (Some(surchargeDetails), None) =>
SurchargeUtils.getMessage(~paymentMethod, ~surchargeDetails)
| (None, None) => None
}
| (None, Some(surchargeDetails))
| (Some(surchargeDetails), None) =>
surchargeDetails->getMessage
| (None, None) => None
} else {
None
}
} else {
None
}
}
}

switch getSurchargeMessage() {
| Some(surchargeMessage) =>
<div className="flex items-center text-xs mt-2">
<div className="flex items-baseline text-xs mt-2">
<Icon name="asterisk" size=8 className="text-red-600 mr-1" />
<span className="text-left"> {React.string(surchargeMessage)} </span>
<em className="text-left text-gray-400"> {surchargeMessage} </em>
</div>
| None => React.null
}
Expand Down
107 changes: 107 additions & 0 deletions src/Components/SurchargeUtils.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
type oneClickWallets = {
paymentMethodType: string,
displayName: string,
}
let oneClickWallets = [
{paymentMethodType: "apple_pay", displayName: "ApplePay"},
{paymentMethodType: "paypal", displayName: "Paypal"},
{paymentMethodType: "google_pay", displayName: "GooglePay"},
]

type walletSurchargeDetails = {
name: string,
surchargeDetails: PaymentMethodsRecord.surchargeDetails,
}

let getSurchargeDetailsForOneClickWallets = (~list) => {
let areOneClickWalletsRendered = Recoil.useRecoilValueFromAtom(
RecoilAtoms.areOneClickWalletsRendered,
)

oneClickWallets->Js.Array2.reduce((acc, wallet) => {
let isWalletBtnRendered = switch wallet.paymentMethodType {
| "apple_pay" => areOneClickWalletsRendered.isApplePay
| "paypal" => areOneClickWalletsRendered.isPaypal
| "google_pay" => areOneClickWalletsRendered.isGooglePay
| _ => false
}
if isWalletBtnRendered {
let paymentMethodType =
PaymentMethodsRecord.getPaymentMethodTypeFromList(
~list,
~paymentMethod="wallet",
~paymentMethodType=wallet.paymentMethodType,
)->Belt.Option.getWithDefault(PaymentMethodsRecord.defaultPaymentMethodType)
switch paymentMethodType.surcharge_details {
| Some(surchargDetails) =>
acc->Js.Array2.concat([
{
name: wallet.displayName,
surchargeDetails: surchargDetails,
},
])
| None => acc
}
} else {
acc
}
}, [])
}

let getMessage = (~surchargeDetails: PaymentMethodsRecord.surchargeDetails, ~paymentMethod) => {
let {localeString} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom)
let surchargeValue = surchargeDetails.displayTotalSurchargeAmount->Js.Float.toString

let getLocaleStrForSurcharge = (cardLocale, altPaymentLocale) => {
paymentMethod === "card" ? cardLocale(surchargeValue) : altPaymentLocale(surchargeValue)
}

Some(
getLocaleStrForSurcharge(
localeString.surchargeMsgAmountForCard,
localeString.surchargeMsgAmount,
),
)
}

let getOneClickWalletsMessage = (~list) => {
let {localeString} = Recoil.useRecoilValueFromAtom(RecoilAtoms.configAtom)

let oneClickWalletsArr = getSurchargeDetailsForOneClickWallets(~list)

if oneClickWalletsArr->Js.Array2.length !== 0 {
let msg = oneClickWalletsArr->Js.Array2.reducei((acc, wallet, index) => {
let amount = wallet.surchargeDetails.displayTotalSurchargeAmount->Js.Float.toString
let myMsg =
<>
<strong> {React.string(amount)} </strong>
{React.string(`${Utils.nbsp}${localeString.on} ${wallet.name}`)}
</>
let msgToConcat = if index === 0 {
myMsg
} else if index === oneClickWalletsArr->Belt.Array.length - 1 {
<>
{React.string(`${Utils.nbsp}${localeString.\"and"}${Utils.nbsp}`)}
{myMsg}
</>
} else {
<>
{React.string(`,${Utils.nbsp}`)}
{myMsg}
</>
}
<>
{acc}
{msgToConcat}
</>
}, React.null)
let finalElement =
<>
{React.string(`${localeString.surchargeMsgAmountForOneClickWallets}:${Utils.nbsp}`)}
{msg}
</>
Some(finalElement)
} else {
None
}
}
Loading

0 comments on commit ee72b3f

Please sign in to comment.