Skip to content

Commit

Permalink
feat: pass email and phone number to confirm call (#681)
Browse files Browse the repository at this point in the history
Co-authored-by: Pritish Budhiraja <[email protected]>
  • Loading branch information
ImSagnik007 and PritishBudhiraja authored Sep 26, 2024
1 parent 0ac07a2 commit 412ba68
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
10 changes: 9 additions & 1 deletion src/Payments/PaypalSDKHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ let loadPaypalSDK = (
->Array.get(0)
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
let details = purchaseUnit->paypalShippingDetails
let payerDetails =
val
->Utils.getDictFromJson
->Dict.get("payer")
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
->PaymentType.itemToPayerDetailsObjectMapper

let details = purchaseUnit->paypalShippingDetails(payerDetails)
let requiredFieldsBody = DynamicFieldsUtils.getPaypalRequiredFields(
~details,
~paymentMethodTypes,
Expand Down
18 changes: 18 additions & 0 deletions src/Types/PaymentType.res
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ type options = {
hideCardNicknameField: bool,
customMessageForCardTerms: string,
}

type payerDetails = {
email: option<string>,
phone: option<string>,
}

let defaultCardDetails = {
scheme: None,
last4Digits: "",
Expand Down Expand Up @@ -313,6 +319,7 @@ let defaultOptions = {
hideCardNicknameField: false,
customMessageForCardTerms: "",
}

let getLayout = (str, logger) => {
switch str {
| "tabs" => Tabs
Expand Down Expand Up @@ -1065,3 +1072,14 @@ type loadType = Loading | Loaded(JSON.t) | SemiLoaded | LoadError(JSON.t)
let getIsStoredPaymentMethodHasName = (savedMethod: customerMethods) => {
savedMethod.card.cardHolderName->Option.getOr("")->String.length > 0
}

let itemToPayerDetailsObjectMapper = dict => {
email: dict->Dict.get("email_address")->Option.flatMap(JSON.Decode.string),
phone: dict
->Dict.get("phone")
->Option.flatMap(JSON.Decode.object)
->Option.flatMap(Dict.get(_, "phone_number"))
->Option.flatMap(JSON.Decode.object)
->Option.flatMap(Dict.get(_, "national_number"))
->Option.flatMap(JSON.Decode.string),
}
12 changes: 4 additions & 8 deletions src/Types/PaypalSDKTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ let getShippingDetails = shippingAddressOverrideObj => {
}
}

let paypalShippingDetails = purchaseUnit => {
let paypalShippingDetails = (purchaseUnit, payerDetails: PaymentType.payerDetails) => {
let shippingAddress = purchaseUnit->Utils.getDictFromDict("shipping")
let payee = purchaseUnit->Utils.getDictFromDict("payee")

let address = shippingAddress->Utils.getDictFromDict("address")
let name = shippingAddress->Utils.getDictFromDict("name")
Expand All @@ -139,10 +138,7 @@ let paypalShippingDetails = purchaseUnit => {
let countryCode = address->Utils.getOptionString("country_code")
let postalCode = address->Utils.getOptionString("postal_code")
let state = address->Utils.getOptionString("admin_area_1")
let email = payee->Utils.getString("email_address", "")

let payeePhone = payee->Utils.getOptionString("phone")
let shippingAddressPhone = address->Utils.getOptionString("phone")
let email = payerDetails.email->Option.getOr("")

{
email,
Expand All @@ -154,9 +150,9 @@ let paypalShippingDetails = purchaseUnit => {
countryCode,
postalCode,
state,
phone: shippingAddressPhone,
phone: payerDetails.phone,
},
phone: payeePhone,
phone: payerDetails.phone,
}
}

Expand Down
31 changes: 13 additions & 18 deletions src/Utilities/DynamicFieldsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1059,32 +1059,27 @@ let getPaypalRequiredFields = (

let fieldVal = switch item.field_type {
| ShippingName => {
let name = details.shippingAddress.recipientName->Option.getOr("")
name->getNameFromString(requiredFieldsArr)
let name = details.shippingAddress.recipientName
name->Option.map(getNameFromString(_, requiredFieldsArr))
}
| ShippingAddressLine1 => details.shippingAddress.line1->Option.getOr("")
| ShippingAddressLine2 => details.shippingAddress.line2->Option.getOr("")
| ShippingAddressCity => details.shippingAddress.city->Option.getOr("")
| ShippingAddressLine1 => details.shippingAddress.line1
| ShippingAddressLine2 => details.shippingAddress.line2
| ShippingAddressCity => details.shippingAddress.city
| ShippingAddressState => {
let administrativeArea = details.shippingAddress.state->Option.getOr("")
let countryCode = details.shippingAddress.countryCode->Option.getOr("")
Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode)
}
| ShippingAddressCountry(_) => details.shippingAddress.countryCode->Option.getOr("")
| ShippingAddressPincode => details.shippingAddress.postalCode->Option.getOr("")
| Email => details.email
| PhoneNumber =>
switch (details.phone->Option.getOr(""), details.shippingAddress.phone->Option.getOr("")) {
| (phone, "") => phone
| ("", phone) => phone
| _ => ""
Utils.getStateNameFromStateCodeAndCountry(statesList, administrativeArea, countryCode)->Some
}
| _ => ""
| ShippingAddressCountry(_) => details.shippingAddress.countryCode
| ShippingAddressPincode => details.shippingAddress.postalCode
| Email => details.email->Some
| PhoneNumber => details.phone
| _ => None
}

if fieldVal !== "" {
fieldVal->Option.mapOr((), fieldVal =>
acc->Dict.set(item.required_field, fieldVal->JSON.Encode.string)
}
)

acc
})
Expand Down
22 changes: 11 additions & 11 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1286,21 +1286,21 @@ let getStateNameFromStateCodeAndCountry = (list: JSON.t, stateCode: string, coun
list
->getDictFromJson
->getOptionalArrayFromDict(country)
->Option.getOr([])

let val = options->Array.find(item =>
item
->getDictFromJson
->getString("code", "") === stateCode
options
->Option.flatMap(
Array.find(_, item =>
item
->getDictFromJson
->getString("code", "") === stateCode
),
)

switch val {
| Some(stateObj) =>
->Option.flatMap(stateObj =>
stateObj
->getDictFromJson
->getString("name", stateCode)
| None => stateCode
}
->getOptionString("name")
)
->Option.getOr(stateCode)
}

let removeHyphen = str => str->String.replaceRegExp(%re("/-/g"), "")
Expand Down

0 comments on commit 412ba68

Please sign in to comment.