Skip to content

Commit

Permalink
fix: resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ArushKapoorJuspay committed May 16, 2024
1 parent 3aa20e1 commit 08edabf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 74 deletions.
36 changes: 11 additions & 25 deletions src/Payments/ApplePay.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,7 @@ let make = (~sessionObj: option<JSON.t>) => {

let isGuestCustomer = UtilityHooks.useIsGuestCustomer()

React.useEffect0(() => {
AddressPaymentInput.importStates("./../States.json")
->then(res => {
setStatesJson(_ => res.states)
resolve()
})
->ignore

None
})
PaymentUtils.useStatesJson(setStatesJson)

let processPayment = (bodyArr, ~isThirdPartyFlow=false, ()) => {
let requestBody = PaymentUtils.appendedCustomerAcceptance(
Expand Down Expand Up @@ -252,7 +243,7 @@ let make = (~sessionObj: option<JSON.t>) => {
processPayment(bodyDict, ~isThirdPartyFlow=true, ())
} else {
let message = [("applePayButtonClicked", true->JSON.Encode.bool)]
Utils.handlePostMessage(message)
handlePostMessage(message)
}
} else {
let bodyDict = PaymentBody.applePayRedirectBody(~connectors)
Expand Down Expand Up @@ -281,23 +272,19 @@ let make = (~sessionObj: option<JSON.t>) => {
}

try {
let dict = json->Utils.getDictFromJson
let dict = json->getDictFromJson
if dict->Dict.get("applePayProcessPayment")->Option.isSome {
let token =
dict->Dict.get("applePayProcessPayment")->Option.getOr(Dict.make()->JSON.Encode.object)

let billingContact =
dict
->Dict.get("applePayBillingContact")
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
->getDictfromDict("applePayBillingContact")
->ApplePayTypes.billingContactItemToObjMapper

let shippingContact =
dict
->Dict.get("applePayShippingContact")
->Option.flatMap(JSON.Decode.object)
->Option.getOr(Dict.make())
->getDictfromDict("applePayShippingContact")
->ApplePayTypes.shippingContactItemToObjMapper

let requiredFieldsBody = DynamicFieldsUtils.getApplePayRequiredFields(
Expand All @@ -311,11 +298,10 @@ let make = (~sessionObj: option<JSON.t>) => {

let applePayBody =
bodyDict
->Dict.fromArray
->JSON.Encode.object
->Utils.flattenObject(true)
->Utils.mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->Utils.getArrayOfTupleFromDict
->getJsonFromArrayOfJson
->flattenObject(true)
->mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->getArrayOfTupleFromDict

processPayment(applePayBody, ())
} else if dict->Dict.get("showApplePayButton")->Option.isSome {
Expand All @@ -324,13 +310,13 @@ let make = (~sessionObj: option<JSON.t>) => {
syncPayment()
}
} catch {
| _ => Utils.logInfo(Console.log("Error in parsing Apple Pay Data"))
| _ => logInfo(Console.log("Error in parsing Apple Pay Data"))
}
}
Window.addEventListener("message", handleApplePayMessages)
Some(
() => {
Utils.handlePostMessage([("applePaySessionAbort", true->JSON.Encode.bool)])
handlePostMessage([("applePaySessionAbort", true->JSON.Encode.bool)])
Window.removeEventListener("message", handleApplePayMessages)
},
)
Expand Down
25 changes: 8 additions & 17 deletions src/Payments/GPay.res
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,7 @@ let make = (~sessionObj: option<SessionsType.token>, ~thirdPartySessionObj: opti

let (stateJson, setStatesJson) = React.useState(_ => JSON.Encode.null)

React.useEffect0(() => {
AddressPaymentInput.importStates("./../States.json")
->then(res => {
setStatesJson(_ => res.states)
resolve()
})
->ignore

None
})
PaymentUtils.useStatesJson(setStatesJson)

React.useEffect(() => {
let handle = (ev: Window.event) => {
Expand All @@ -96,7 +87,7 @@ let make = (~sessionObj: option<SessionsType.token>, ~thirdPartySessionObj: opti
} catch {
| _ => Dict.make()->JSON.Encode.object
}
let dict = json->Utils.getDictFromJson
let dict = json->getDictFromJson
if dict->Dict.get("gpayResponse")->Option.isSome {
let metadata = dict->getJsonObjectFromDict("gpayResponse")
let obj = metadata->getDictFromJson->itemToObjMapper
Expand All @@ -109,21 +100,21 @@ let make = (~sessionObj: option<SessionsType.token>, ~thirdPartySessionObj: opti
let billingContact =
obj.paymentMethodData.info
->getDictFromJson
->Utils.getJsonObjectFromDict("billingAddress")
->getJsonObjectFromDict("billingAddress")
->getDictFromJson
->billingContactItemToObjMapper

let shippingContact =
metadata
->getDictFromJson
->Utils.getJsonObjectFromDict("shippingAddress")
->getJsonObjectFromDict("shippingAddress")
->getDictFromJson
->billingContactItemToObjMapper

let email =
metadata
->getDictFromJson
->Utils.getString("email", "")
->getString("email", "")

let requiredFieldsBody = DynamicFieldsUtils.getGooglePayRequiredFields(
~billingContact,
Expand All @@ -143,7 +134,7 @@ let make = (~sessionObj: option<SessionsType.token>, ~thirdPartySessionObj: opti
processPayment(body, ())
}
if dict->Dict.get("gpayError")->Option.isSome {
Utils.handlePostMessage([("fullscreen", false->JSON.Encode.bool)])
handlePostMessage([("fullscreen", false->JSON.Encode.bool)])
}
}
Window.addEventListener("message", handle)
Expand Down Expand Up @@ -248,13 +239,13 @@ let make = (~sessionObj: option<SessionsType.token>, ~thirdPartySessionObj: opti
} catch {
| _ => Dict.make()->JSON.Encode.object
}
let dict = json->Utils.getDictFromJson
let dict = json->getDictFromJson
try {
if dict->Dict.get("googlePaySyncPayment")->Option.isSome {
syncPayment()
}
} catch {
| _ => Utils.logInfo(Console.log("Error in syncing GooglePay Payment"))
| _ => logInfo(Console.log("Error in syncing GooglePay Payment"))
}
}
Window.addEventListener("message", handleGooglePayMessages)
Expand Down
10 changes: 6 additions & 4 deletions src/Payments/PaymentMethodsRecord.res
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,12 @@ let getOptionsFromPaymentMethodFieldType = (dict, key, ~isAddressCountry=true) =
isAddressCountry ? AddressCountry(countryArr) : ShippingAddressCountry(countryArr)
}
| _ => {
let countryArr =
Country.country
->Array.filter(item => options->Array.includes(item.isoAlpha2))
->Array.map(item => item.countryName)
let countryArr = Country.country->Array.reduce([], (acc, country) => {
if options->Array.includes(country.isoAlpha2) {
acc->Array.push(country.countryName)
}
acc
})
isAddressCountry ? AddressCountry(countryArr) : ShippingAddressCountry(countryArr)
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/Payments/PaypalSDK.res
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =
~paymentMethodType="paypal",
)

React.useEffect0(() => {
AddressPaymentInput.importStates("./../States.json")
->then(res => {
setStatesJson(_ => res.states)
resolve()
})
->ignore

None
})
PaymentUtils.useStatesJson(setStatesJson)

let loadPaypalSdk = () => {
loggerState.setLogInfo(
Expand Down Expand Up @@ -122,8 +113,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) =

let paypalBody =
body
->Dict.fromArray
->JSON.Encode.object
->Utils.getJsonFromArrayOfJson
->Utils.flattenObject(true)
->Utils.mergeTwoFlattenedJsonDicts(requiredFieldsBody)
->Utils.getArrayOfTupleFromDict
Expand Down
20 changes: 10 additions & 10 deletions src/Types/GooglePayType.res
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ let jsonToPaymentRequestDataType: (paymentDataRequest, Dict.t<JSON.t>) => paymen

let billingContactItemToObjMapper = dict => {
{
address1: dict->Utils.getString("address1", ""),
address2: dict->Utils.getString("address2", ""),
address3: dict->Utils.getString("address3", ""),
administrativeArea: dict->Utils.getString("administrativeArea", ""),
countryCode: dict->Utils.getString("countryCode", ""),
locality: dict->Utils.getString("locality", ""),
name: dict->Utils.getString("name", ""),
phoneNumber: dict->Utils.getString("phoneNumber", ""),
postalCode: dict->Utils.getString("postalCode", ""),
sortingCode: dict->Utils.getString("sortingCode", ""),
address1: dict->getString("address1", ""),
address2: dict->getString("address2", ""),
address3: dict->getString("address3", ""),
administrativeArea: dict->getString("administrativeArea", ""),
countryCode: dict->getString("countryCode", ""),
locality: dict->getString("locality", ""),
name: dict->getString("name", ""),
phoneNumber: dict->getString("phoneNumber", ""),
postalCode: dict->getString("postalCode", ""),
sortingCode: dict->getString("sortingCode", ""),
}
}
14 changes: 14 additions & 0 deletions src/Utilities/PaymentUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,17 @@ let useGetPaymentMethodList = (~paymentOptions, ~paymentType) => {
}
}, (methodslist, paymentMethodOrder, optionAtomValue.wallets.payPal, paymentType))
}

let useStatesJson = setStatesJson => {
open Promise
React.useEffect0(() => {
AddressPaymentInput.importStates("./../States.json")
->then(res => {
setStatesJson(_ => res.states)
resolve()
})
->ignore

None
})
}
8 changes: 2 additions & 6 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -1261,18 +1261,14 @@ let getStateNameFromStateCodeAndCountry = (list: JSON.t, stateCode: string, coun
let val = options->Array.find(item =>
item
->getDictFromJson
->Dict.get("code")
->Option.flatMap(JSON.Decode.string)
->Option.getOr("") === stateCode
->getString("code", "") === stateCode
)

switch val {
| Some(stateObj) =>
stateObj
->getDictFromJson
->Dict.get("name")
->Option.flatMap(JSON.Decode.string)
->Option.getOr(stateCode)
->getString("name", stateCode)
| None => stateCode
}
}

0 comments on commit 08edabf

Please sign in to comment.