From 37230d3455674f6fead4b5b79f61eac8f309fcd3 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Mon, 24 Jun 2024 15:32:11 +0530 Subject: [PATCH] feat: headless functions added for payment management --- Hyperswitch-React-Demo-App/src/something.js | 2 +- src/Components/SavedMethodItem.res | 7 +-- src/RenderPaymentMethods.res | 1 + src/orca-loader/Hyper.res | 1 + src/orca-loader/PaymentSession.res | 15 ++++- src/orca-loader/PaymentSessionMethods.res | 69 ++++++++++++++++++++- src/orca-loader/Types.res | 23 ++++++- 7 files changed, 107 insertions(+), 11 deletions(-) diff --git a/Hyperswitch-React-Demo-App/src/something.js b/Hyperswitch-React-Demo-App/src/something.js index 48a40c320..5770efbb5 100644 --- a/Hyperswitch-React-Demo-App/src/something.js +++ b/Hyperswitch-React-Demo-App/src/something.js @@ -8,7 +8,7 @@ export const loadSDK = (clientSecret, pubkey) => { const appearance = { theme: "charcoal", }; - const ephimeralKey = "epk_199235de39aa4ca08c596a98635e6bbb"; + const ephimeralKey = "epk_8dd34bfa79b1483b9f9a56ba9ad23009"; const widgets = hyper.widgets({ appearance, clientSecret, ephimeralKey }); const unifiedCheckoutOptions = { layout: "tabs", diff --git a/src/Components/SavedMethodItem.res b/src/Components/SavedMethodItem.res index 7b25c1703..47e217be7 100644 --- a/src/Components/SavedMethodItem.res +++ b/src/Components/SavedMethodItem.res @@ -17,7 +17,6 @@ let make = (~brandIcon, ~paymentItem: PaymentType.customerMethods) => { } let handleDelete = e => { - Console.log2("methodID", paymentItem.paymentMethodId) open Promise PaymentHelpers.deletePaymentMethod( ~ephimeralKey=config.ephimeralKey, @@ -25,12 +24,10 @@ let make = (~brandIcon, ~paymentItem: PaymentType.customerMethods) => { ~logger, ~switchToCustomPod, ) - ->then(res => { - Console.log2("deleted paymentId", res) + ->then(_ => { resolve() }) - ->catch(err => { - Console.log2("deleting error ", err) + ->catch(_ => { resolve() }) ->ignore diff --git a/src/RenderPaymentMethods.res b/src/RenderPaymentMethods.res index 4bf4da3f6..ca3fd3e01 100644 --- a/src/RenderPaymentMethods.res +++ b/src/RenderPaymentMethods.res @@ -149,6 +149,7 @@ let make = ( }> + | PaymentRequestButtonsElement | NONE => React.null }} diff --git a/src/orca-loader/Hyper.res b/src/orca-loader/Hyper.res index d98719653..d5bac00ad 100644 --- a/src/orca-loader/Hyper.res +++ b/src/orca-loader/Hyper.res @@ -572,6 +572,7 @@ let make = (publishableKey, options: option, analyticsInfo: option) => { +let make = ( + options, + ~clientSecret, + ~publishableKey, + ~logger: option, + ~ephimeralKey, +) => { let logger = logger->Option.getOr(OrcaLogger.defaultLoggerConfig) let switchToCustomPod = GlobalVars.isInteg && @@ -20,6 +26,13 @@ let make = (options, ~clientSecret, ~publishableKey, ~logger: option + PaymentSessionMethods.getPaymentManagementMethods( + ~ephimeralKey, + ~logger, + ~switchToCustomPod, + ~endpoint, + ), } defaultInitPaymentSession diff --git a/src/orca-loader/PaymentSessionMethods.res b/src/orca-loader/PaymentSessionMethods.res index bd6c2d983..02510967a 100644 --- a/src/orca-loader/PaymentSessionMethods.res +++ b/src/orca-loader/PaymentSessionMethods.res @@ -1,3 +1,7 @@ +open Promise +open Types +open Utils + let getCustomerSavedPaymentMethods = ( ~clientSecret, ~publishableKey, @@ -5,9 +9,6 @@ let getCustomerSavedPaymentMethods = ( ~logger, ~switchToCustomPod, ) => { - open Promise - open Types - open Utils PaymentHelpers.fetchCustomerPaymentMethodList( ~clientSecret, ~publishableKey, @@ -196,3 +197,65 @@ let getCustomerSavedPaymentMethods = ( updatedCustomerDetails->resolve }) } + +let getPaymentManagementMethods = (~ephimeralKey, ~logger, ~switchToCustomPod, ~endpoint) => { + let getSavedPaymentManagementMethodsList = _ => { + PaymentHelpers.fetchSavedPaymentMethodList( + ~ephimeralKey, + ~optLogger=Some(logger), + ~switchToCustomPod=false, + ~endpoint, + ) + ->then(response => { + response->resolve + }) + ->catch(err => { + let exceptionMessage = err->formatException->JSON.stringify + let updatedCustomerDetails = + [ + ( + "error", + [ + ("type", "server_error"->JSON.Encode.string), + ("message", exceptionMessage->JSON.Encode.string), + ]->getJsonFromArrayOfJson, + ), + ]->getJsonFromArrayOfJson + updatedCustomerDetails->resolve + }) + } + + let deleteSavedPaymentMethod = paymentMethodId => { + PaymentHelpers.deletePaymentMethod( + ~ephimeralKey, + ~paymentMethodId={paymentMethodId->JSON.Decode.string->Option.getOr("")}, + ~logger, + ~switchToCustomPod, + ) + ->then(response => { + response->resolve + }) + ->catch(err => { + let exceptionMessage = err->formatException->JSON.stringify + let updatedCustomerDetails = + [ + ( + "error", + [ + ("type", "server_error"->JSON.Encode.string), + ("message", exceptionMessage->JSON.Encode.string), + ]->getJsonFromArrayOfJson, + ), + ]->getJsonFromArrayOfJson + + updatedCustomerDetails->resolve + }) + } + + { + getSavedPaymentManagementMethodsList, + deleteSavedPaymentMethod, + } + ->Identity.anyTypeToJson + ->resolve +} diff --git a/src/orca-loader/Types.res b/src/orca-loader/Types.res index 8d396c99a..91b1ad5a4 100644 --- a/src/orca-loader/Types.res +++ b/src/orca-loader/Types.res @@ -46,7 +46,15 @@ type getCustomerSavedPaymentMethods = { confirmWithLastUsedPaymentMethod: JSON.t => Promise.t, } -type initPaymentSession = {getCustomerSavedPaymentMethods: unit => Promise.t} +type getPaymentManagementMethods = { + getSavedPaymentManagementMethodsList: unit => Promise.t, + deleteSavedPaymentMethod: JSON.t => Promise.t, +} + +type initPaymentSession = { + getCustomerSavedPaymentMethods: unit => Promise.t, + getPaymentManagementMethods: unit => Promise.t, +} type confirmParams = {return_url: string} @@ -124,6 +132,14 @@ let getCustomerDefaultSavedPaymentMethodData = () => { JSON.Encode.null } +let getSavedPaymentManagementMethodsList = () => { + JSON.Encode.null +} + +let deleteSavedPaymentMethod = () => { + JSON.Encode.null +} + let getCustomerLastUsedPaymentMethodData = () => { JSON.Encode.null } @@ -145,8 +161,13 @@ let defaultGetCustomerSavedPaymentMethods = () => { Promise.resolve(JSON.Encode.null) } +let defaultGetPaymentManagementMethods = () => { + Promise.resolve(JSON.Encode.null) +} + let defaultInitPaymentSession: initPaymentSession = { getCustomerSavedPaymentMethods: defaultGetCustomerSavedPaymentMethods, + getPaymentManagementMethods: defaultGetPaymentManagementMethods, } let defaultHyperInstance = {