Skip to content

Commit

Permalink
feat: use dynamic payment body
Browse files Browse the repository at this point in the history
  • Loading branch information
Marno Janetzky committed Jan 9, 2025
1 parent 33c967e commit 9a20a4c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
22 changes: 6 additions & 16 deletions src/Payments/KlarnaPayment.res
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
open RecoilAtoms
open Utils
@react.component
let make = (~paymentType) => {
let loggerState = Recoil.useRecoilValueFromAtom(loggerAtom)
let {themeObj} = Recoil.useRecoilValueFromAtom(configAtom)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), KlarnaRedirect)
let isManualRetryEnabled = Recoil.useRecoilValueFromAtom(RecoilAtoms.isManualRetryEnabled)
let setComplete = Recoil.useSetRecoilState(fieldsComplete)
let paymentMethodListValue = Recoil.useRecoilValueFromAtom(PaymentUtils.paymentMethodListValue)

let (fullName, _) = Recoil.useLoggedRecoilState(userFullName, "fullName", loggerState)
let (email, _) = Recoil.useLoggedRecoilState(userEmailAddress, "email", loggerState)

let country = Recoil.useRecoilValueFromAtom(userCountry)
let (_, setRequiredFieldsBody) = React.useState(_ => Dict.make())

open Utils
let clientCountryCode =
Country.country
->Array.find(item => item.countryName == country)
->Option.getOr(Country.defaultTimeZone)
let (requiredFieldsBody, setRequiredFieldsBody) = React.useState(_ => Dict.make())

let complete = email.value != "" && fullName.value != "" && email.isValid->Option.getOr(false)
let empty = email.value == "" || fullName.value == ""
Expand All @@ -33,17 +27,13 @@ let make = (~paymentType) => {
let submitCallback = React.useCallback((ev: Window.event) => {
let json = ev.data->safeParse
let confirm = json->Utils.getDictFromJson->ConfirmType.itemToObjMapper
let (connectors, _) =
paymentMethodListValue->PaymentUtils.getConnectors(PayLater(Klarna(Redirect)))

if confirm.doSubmit {
if complete {
let bodyArr = PaymentBody.klarnaRedirectionBody(
~fullName=fullName.value,
~email=email.value,
~country=clientCountryCode.isoAlpha2,
~connectors,
)
let bodyArr =
PaymentBody.dynamicPaymentBody("pay_later", "klarna")->mergeAndFlattenToTuples(
requiredFieldsBody,
)

intent(
~bodyArr,
Expand Down
25 changes: 0 additions & 25 deletions src/Utilities/PaymentBody.res
Original file line number Diff line number Diff line change
Expand Up @@ -312,31 +312,6 @@ let becsBankDebitBody = (
),
])

let klarnaRedirectionBody = (~fullName, ~email, ~country, ~connectors) => [
("payment_method", "pay_later"->JSON.Encode.string),
("payment_method_type", "klarna"->JSON.Encode.string),
("payment_experience", "redirect_to_url"->JSON.Encode.string),
("connector", connectors->Utils.getArrofJsonString->JSON.Encode.array),
("name", fullName->JSON.Encode.string),
(
"payment_method_data",
[
(
"pay_later",
[
(
"klarna_redirect",
[
("billing_email", email->JSON.Encode.string),
("billing_country", country->JSON.Encode.string),
]->Utils.getJsonFromArrayOfJson,
),
]->Utils.getJsonFromArrayOfJson,
),
]->Utils.getJsonFromArrayOfJson,
),
]

let klarnaSDKbody = (~token, ~connectors) => [
("payment_method", "pay_later"->JSON.Encode.string),
("payment_method_type", "klarna"->JSON.Encode.string),
Expand Down

0 comments on commit 9a20a4c

Please sign in to comment.