diff --git a/src/screens/HyperSwitch/SDKPayment/SDKPaymentTypes.res b/src/screens/HyperSwitch/SDKPayment/SDKPaymentTypes.res index 42146dfe9..e27c3f69a 100644 --- a/src/screens/HyperSwitch/SDKPayment/SDKPaymentTypes.res +++ b/src/screens/HyperSwitch/SDKPayment/SDKPaymentTypes.res @@ -1,7 +1,50 @@ -type paymentType = { +type address = { + line1: string, + line2: string, + line3: string, + city: string, + state: string, + zip: string, + country: string, + first_name: string, + last_name: string, +} + +type phone = { + number: string, + country_code: string, +} + +type shipping = { + address: address, + phone: phone, +} + +type billing = { + address: address, + phone: phone, +} +type orderDetails = { + product_name: string, + quantity: int, amount: int, - currency: string, +} +type metadata = {order_details: orderDetails} + +type paymentType = { + mutable amount: int, + mutable currency: string, profile_id: string, customer_id: string, description: string, + capture_method: string, + amount_to_capture: int, + email: string, + name: string, + phone: string, + phone_country_code: string, + authentication_type: string, + shipping: shipping, + billing: billing, + metadata: metadata, } diff --git a/src/screens/HyperSwitch/SDKPayment/SDKPaymentUtils.res b/src/screens/HyperSwitch/SDKPayment/SDKPaymentUtils.res index 1680b7392..ba5f72bff 100644 --- a/src/screens/HyperSwitch/SDKPayment/SDKPaymentUtils.res +++ b/src/screens/HyperSwitch/SDKPayment/SDKPaymentUtils.res @@ -5,18 +5,54 @@ let initialValueForForm: HSwitchSettingTypes.profileEntity => SDKPaymentTypes.pa profile_id: defaultBusinessProfile.profile_id, description: "Default value", customer_id: "hyperswitch_sdk_demo_id", - } -} - -let getTypedValueForPayment: Js.Json.t => SDKPaymentTypes.paymentType = values => { - open LogicUtils - let dictOfValues = values->getDictFromJsonObject - { - amount: dictOfValues->getInt("amount", 100), - currency: dictOfValues->getString("currency", "United States-USD"), - profile_id: dictOfValues->getString("profile_id", ""), - customer_id: dictOfValues->getString("customer_id", ""), - description: dictOfValues->getString("description", "Default value"), + email: "guest@example.com", + name: "John Doe", + phone: "999999999", + phone_country_code: "+65", + authentication_type: "no_three_ds", + shipping: { + address: { + line1: "1467", + line2: "Harrison Street", + line3: "Harrison Street", + city: "San Fransico", + state: "California", + zip: "94122", + country: "US", + first_name: "John", + last_name: "Doe", + }, + phone: { + number: "1234567890", + country_code: "+1", + }, + }, + billing: { + address: { + line1: "1467", + line2: "Harrison Street", + line3: "Harrison Street", + city: "San Fransico", + state: "California", + zip: "94122", + country: "US", + first_name: "John", + last_name: "Doe", + }, + phone: { + number: "1234567890", + country_code: "+1", + }, + }, + metadata: { + order_details: { + product_name: "Apple iphone 15", + quantity: 1, + amount: 100, + }, + }, + capture_method: "automatic", + amount_to_capture: 100, } } @@ -31,3 +67,74 @@ let getCurrencyValue = (countryCurrency: string) => { ->Belt.Option.getWithDefault("USD") ->Js.String2.trim } + +let getTypedValueForPayment: Js.Json.t => SDKPaymentTypes.paymentType = values => { + open LogicUtils + let dictOfValues = values->getDictFromJsonObject + let shippingAddress = + values->getDictFromJsonObject->getDictfromDict("shipping")->getDictfromDict("address") + let shippingPhone = + values->getDictFromJsonObject->getDictfromDict("shipping")->getDictfromDict("phone") + let billingAddress = + values->getDictFromJsonObject->getDictfromDict("billing")->getDictfromDict("address") + let billingPhone = + values->getDictFromJsonObject->getDictfromDict("shipping")->getDictfromDict("phone") + let metaData = + values->getDictFromJsonObject->getDictfromDict("metadata")->getDictfromDict("order_details") + let cents = dictOfValues->getInt("amount", 100)->convertAmountToCents + { + amount: dictOfValues->getInt("amount", 100), + currency: dictOfValues->getString("currency", "United States-USD"), + profile_id: dictOfValues->getString("profile_id", ""), + customer_id: dictOfValues->getString("customer_id", ""), + description: dictOfValues->getString("description", "Default value"), + email: dictOfValues->getString("email", ""), + name: dictOfValues->getString("name", ""), + phone: dictOfValues->getString("phone", ""), + phone_country_code: dictOfValues->getString("phone_country_code", ""), + authentication_type: dictOfValues->getString("authentication_type", ""), + shipping: { + address: { + line1: shippingAddress->getString("line1", ""), + line2: shippingAddress->getString("line2", ""), + line3: shippingAddress->getString("line3", ""), + city: shippingAddress->getString("city", ""), + state: shippingAddress->getString("state", ""), + zip: shippingAddress->getString("zip", ""), + country: shippingAddress->getString("country", ""), + first_name: shippingAddress->getString("first_name", ""), + last_name: shippingAddress->getString("last_name", ""), + }, + phone: { + number: shippingPhone->getString("number", ""), + country_code: shippingPhone->getString("country_code", ""), + }, + }, + billing: { + address: { + line1: billingAddress->getString("line1", ""), + line2: billingAddress->getString("line2", ""), + line3: billingAddress->getString("line3", ""), + city: billingAddress->getString("city", ""), + state: billingAddress->getString("state", ""), + zip: billingAddress->getString("zip", ""), + country: billingAddress->getString("country", ""), + first_name: billingAddress->getString("first_name", ""), + last_name: billingAddress->getString("last_name", ""), + }, + phone: { + number: billingPhone->getString("number", ""), + country_code: billingPhone->getString("country_code", ""), + }, + }, + metadata: { + order_details: { + product_name: metaData->getString("product_name", ""), + quantity: 1, + amount: cents, + }, + }, + capture_method: "automatic", + amount_to_capture: cents, + } +} diff --git a/src/screens/HyperSwitch/SDKPayment/TestPayment.res b/src/screens/HyperSwitch/SDKPayment/TestPayment.res index 0c5b7fd42..215638f97 100644 --- a/src/screens/HyperSwitch/SDKPayment/TestPayment.res +++ b/src/screens/HyperSwitch/SDKPayment/TestPayment.res @@ -33,22 +33,13 @@ let make = ( let filtersFromUrl = getDictFromUrlSearchParams(searchParams) let getClientSecret = async () => { + open SDKPaymentUtils try { let url = `${HSwitchGlobalVars.hyperSwitchApiPrefix}/payments` - let body = - Js.Dict.fromArray([ - ("currency", initialValues.currency->SDKPaymentUtils.getCurrencyValue->Js.Json.string), - ( - "amount", - initialValues.amount - ->SDKPaymentUtils.convertAmountToCents - ->Belt.Int.toFloat - ->Js.Json.number, - ), - ("profile_id", initialValues.profile_id->Js.Json.string), - ("customer_id", "hyperswitch_sdk_demo_id"->Js.Json.string), - ("description", initialValues.description->Js.Json.string), - ])->Js.Json.object_ + let paymentData = initialValues->toJson->Js.Json.stringify->safeParse->getTypedValueForPayment + paymentData.amount = paymentData.amount->convertAmountToCents + paymentData.currency = paymentData.currency->getCurrencyValue + let body = paymentData->toJson let response = await updateDetails(url, body, Post) let clientSecret = response->getDictFromJsonObject->getOptionString("client_secret") setPaymentId(_ => response->getDictFromJsonObject->getString("payment_id", ""))