Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added dynamic fields support for sepa bank transfer #775

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 17 additions & 38 deletions src/Payments/SepaBankTransfer.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@ open Utils
@react.component
let make = (~paymentType) => {
let {iframeId} = Recoil.useRecoilValueFromAtom(keys)
let {fields} = Recoil.useRecoilValueFromAtom(optionAtom)
let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom)
let {config, themeObj, localeString} = Recoil.useRecoilValueFromAtom(configAtom)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), BankTransfer)
let {themeObj} = Recoil.useRecoilValueFromAtom(configAtom)
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(isManualRetryEnabled)
let (country, setCountry) = React.useState(_ => "France")
let setComplete = Recoil.useSetRecoilState(fieldsComplete)
let (email, _) = Recoil.useLoggedRecoilState(userEmailAddress, "email", loggerState)
let (fullName, _) = Recoil.useLoggedRecoilState(userFullName, "fullName", loggerState)
let showAddressDetails = PaymentType.getShowAddressDetails(
~billingDetails=fields.billingDetails,
~logger=loggerState,
)
let countryNames = Utils.getCountryNames(Country.country)
let setComplete = Recoil.useSetRecoilState(fieldsComplete)
let clientCountryCode =
Country.country
->Array.find(item => item.countryName == country)
->Option.getOr(Country.defaultTimeZone)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), BankTransfer)

let (requiredFieldsBody, setRequiredFieldsBody) = React.useState(_ => Dict.make())
let complete = email.value != "" && fullName.value != "" && email.isValid->Option.getOr(false)
let empty = email.value == "" || fullName.value == ""
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)

UtilityHooks.useHandlePostMessages(~complete, ~empty, ~paymentType="bank_transfer")

Expand All @@ -38,14 +28,15 @@ let make = (~paymentType) => {
let confirm = json->getDictFromJson->ConfirmType.itemToObjMapper
if confirm.doSubmit {
if complete {
let (connectors, _) = paymentMethodListValue->PaymentUtils.getConnectors(BankTransfer(Sepa))
let bodyArr =
PaymentBody.dynamicPaymentBody("bank_transfer", "sepa")
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict

intent(
~bodyArr=PaymentBody.sepaBankTransferBody(
~email=email.value,
~name=fullName.value,
~country=clientCountryCode.isoAlpha2,
~connectors,
),
~bodyArr,
~confirmParam=confirm.confirmParams,
~handleUserError=false,
~iframeId,
Expand All @@ -55,25 +46,13 @@ let make = (~paymentType) => {
postFailedSubmitResponse(~errortype="validation_error", ~message="Please enter all fields")
}
}
}, (email, fullName, country, isManualRetryEnabled))
}, (email, fullName, isManualRetryEnabled))
useSubmitPaymentData(submitCallback)

let updatedOptionsArrayForCountry =
countryNames->DropdownField.updateArrayOfStringToOptionsTypeArray

<div className="flex flex-col animate-slowShow" style={gridGap: themeObj.spacingTab}>
<EmailPaymentInput paymentType />
<FullNamePaymentInput paymentType={paymentType} />
<RenderIf condition={showAddressDetails.country == Auto}>
<DropdownField
appearance=config.appearance
fieldName=localeString.countryLabel
value=country
setValue=setCountry
disabled=false
options=updatedOptionsArrayForCountry
/>
</RenderIf>
<DynamicFields
paymentType paymentMethod="bank_transfer" paymentMethodType="sepa" setRequiredFieldsBody
/>
<Surcharge paymentMethod="bank_transfer" paymentMethodType="sepa" />
<InfoElement />
</div>
Expand Down
60 changes: 21 additions & 39 deletions src/Utilities/PaymentBody.res
Original file line number Diff line number Diff line change
Expand Up @@ -698,38 +698,6 @@ let bacsBankTransferBody = (~email, ~name, ~connectors) => {
]
}

let sepaBankTransferBody = (~email, ~name, ~country, ~connectors) => {
let (firstName, lastName) = name->Utils.getFirstAndLastNameFromFullName

[
("payment_method", "bank_transfer"->JSON.Encode.string),
("connector", connectors->Utils.getArrofJsonString->JSON.Encode.array),
("payment_method_type", "sepa"->JSON.Encode.string),
(
"payment_method_data",
[
(
"billing",
[
("email", email->JSON.Encode.string),
(
"address",
[
("first_name", firstName),
("last_name", lastName),
("country", country->JSON.Encode.string),
]->Utils.getJsonFromArrayOfJson,
),
]->Utils.getJsonFromArrayOfJson,
),
(
"bank_transfer",
[("sepa_bank_transfer", Dict.make()->JSON.Encode.object)]->Utils.getJsonFromArrayOfJson,
),
]->Utils.getJsonFromArrayOfJson,
),
]
}
let blikBody = (~blikCode) => [
("payment_method", "bank_redirect"->JSON.Encode.string),
("payment_method_type", "blik"->JSON.Encode.string),
Expand Down Expand Up @@ -977,16 +945,30 @@ let appendRedirectPaymentMethods = [
]

let appendBankeDebitMethods = ["sepa"]
let appendBankTransferMethods = ["sepa"]

let appendPaymentMethodExperience = (paymentMethodType, isQrPaymentMethod) =>
let getPaymentMethodSuffix = (~paymentMethodType, ~paymentMethod, ~isQrPaymentMethod) => {
if isQrPaymentMethod {
paymentMethodType ++ "_qr"
Some("qr")
} else if appendRedirectPaymentMethods->Array.includes(paymentMethodType) {
paymentMethodType ++ "_redirect"
} else if appendBankeDebitMethods->Array.includes(paymentMethodType) {
paymentMethodType ++ "_bank_debit"
Some("redirect")
} else if (
appendBankeDebitMethods->Array.includes(paymentMethodType) && paymentMethod == "bank_debit"
) {
Some("bank_debit")
} else if (
appendBankTransferMethods->Array.includes(paymentMethodType) && paymentMethod == "bank_transfer"
) {
Some("bank_transfer")
} else {
paymentMethodType
None
}
}

let appendPaymentMethodExperience = (~paymentMethod, ~paymentMethodType, ~isQrPaymentMethod) =>
switch getPaymentMethodSuffix(~paymentMethodType, ~paymentMethod, ~isQrPaymentMethod) {
| Some(suffix) => `${paymentMethodType}_${suffix}`
| None => paymentMethodType
}

let paymentExperiencePaymentMethods = ["affirm"]
Expand All @@ -1010,7 +992,7 @@ let dynamicPaymentBody = (paymentMethod, paymentMethodType, ~isQrPaymentMethod=f
paymentMethod,
[
(
paymentMethodType->appendPaymentMethodExperience(isQrPaymentMethod),
appendPaymentMethodExperience(~paymentMethod, ~paymentMethodType, ~isQrPaymentMethod),
Dict.make()->JSON.Encode.object,
),
]->Utils.getJsonFromArrayOfJson,
Expand Down
Loading