Skip to content

Commit

Permalink
fix: added Support for future APIs and added error handling
Browse files Browse the repository at this point in the history
fix #207
  • Loading branch information
ArushKapoorJuspay committed Mar 11, 2024
1 parent 0eaac2a commit 81121ec
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 154 deletions.
35 changes: 29 additions & 6 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ let rec intentCall = (
~sdkHandleOneClickConfirmPayment,
~counter,
~isPaymentSession=false,
~paymentSessionRedirect="if_redirect",
(),
) => {
open Promise
Expand Down Expand Up @@ -199,7 +200,11 @@ let rec intentCall = (
if handleUserError {
handleOpenUrl(url.href)
} else {
resolve(. data)
let failedSubmitResponse = getFailedSubmitResponse(
~errorType=errorObj.error.type_,
~message=errorObj.error.message,
)
resolve(. failedSubmitResponse)
}
},
)->then(resolve)
Expand Down Expand Up @@ -227,7 +232,11 @@ let rec intentCall = (
if handleUserError {
handleOpenUrl(url.href)
} else {
resolve(. exceptionMessage)
let failedSubmitResponse = getFailedSubmitResponse(
~errorType="server_error",
~message="Something went wrong",
)
resolve(. failedSubmitResponse)
}
} else {
let paymentIntentID =
Expand Down Expand Up @@ -293,8 +302,10 @@ let rec intentCall = (
| (Paypal, false) =>
if !isPaymentSession {
postSubmitResponse(~jsonData=data, ~url=url.href)
} else {
} else if paymentSessionRedirect === "always" {
handleOpenUrl(url.href)
} else {
resolve(. data)
}
| _ => handleOpenUrl(url.href)
}
Expand Down Expand Up @@ -410,7 +421,11 @@ let rec intentCall = (
)
handleOpenUrl(url.href)
} else {
resolve(. data)
let failedSubmitResponse = getFailedSubmitResponse(
~errorType="confirm_payment_failed",
~message="Payment failed. Try again!",
)
resolve(. failedSubmitResponse)
}
}
} else if intent.status == "processing" {
Expand Down Expand Up @@ -464,7 +479,11 @@ let rec intentCall = (
~message="Payment failed. Try again!",
)
} else {
resolve(. data)
let failedSubmitResponse = getFailedSubmitResponse(
~errorType="confirm_payment_failed",
~message="Payment failed. Try again!",
)
resolve(. failedSubmitResponse)
}
},
)->then(resolve)
Expand Down Expand Up @@ -495,7 +514,11 @@ let rec intentCall = (
if handleUserError {
handleOpenUrl(url.href)
} else {
resolve(. exceptionMessage)
let failedSubmitResponse = getFailedSubmitResponse(
~errorType="server_error",
~message="Something went wrong",
)
resolve(. failedSubmitResponse)
}
} else {
let paymentIntentID =
Expand Down
13 changes: 13 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ let postSubmitResponse = (~jsonData, ~url) => {
])
}

let getFailedSubmitResponse = (~errorType, ~message) => {
[
(
"error",
[("type", errorType->Js.Json.string), ("message", message->Js.Json.string)]
->Js.Dict.fromArray
->Js.Json.object_,
),
]
->Js.Dict.fromArray
->Js.Json.object_
}

let toCamelCase = str => {
if str->Js.String2.includes(":") {
str
Expand Down
150 changes: 10 additions & 140 deletions src/orca-loader/PaymentSession.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
open Types

let make = (options, ~clientSecret, ~publishableKey, ~logger: option<OrcaLogger.loggerMake>) => {
open Promise
let logger = logger->Belt.Option.getWithDefault(OrcaLogger.defaultLoggerConfig)
let switchToCustomPod =
GlobalVars.isInteg &&
Expand All @@ -12,145 +11,16 @@ let make = (options, ~clientSecret, ~publishableKey, ~logger: option<OrcaLogger.
->Belt.Option.getWithDefault(false)
let endpoint = ApiEndpoint.getApiEndPoint(~publishableKey, ())

let defaultPaymentMethodList = ref(Js.Json.null)

let customerDetailsPromise = PaymentHelpers.useCustomerDetails(
~clientSecret,
~publishableKey,
~endpoint,
~switchToCustomPod,
~optLogger=Some(logger),
)

let getDefaultPaymentMethod = () => {
customerDetailsPromise
->then(customerDetails => {
let customerPaymentMethods =
customerDetails
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("customer_payment_methods"))
->Belt.Option.flatMap(Js.Json.decodeArray)
->Belt.Option.getWithDefault([])
->Js.Array2.filter(customerPaymentMethod => {
customerPaymentMethod
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("default_payment_method_set"))
->Belt.Option.flatMap(Js.Json.decodeBoolean)
->Belt.Option.getWithDefault(false)
})
let isGuestCustomer =
customerDetails
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("is_guest_customer"))
->Belt.Option.getWithDefault(Js.Json.null)
let updatedCustomerDetails =
[
("customer_payment_methods", customerPaymentMethods->Js.Json.array),
("is_guest_customer", isGuestCustomer),
]
->Js.Dict.fromArray
->Js.Json.object_
defaultPaymentMethodList := updatedCustomerDetails
updatedCustomerDetails->resolve
})
->catch(_err => {
let dict =
[("customer_payment_methods", []->Js.Json.array)]->Js.Dict.fromArray->Js.Json.object_
let msg = [("customerPaymentMethods", dict)]->Js.Dict.fromArray
resolve(msg->Js.Json.object_)
})
}

let confirmWithDefault = payload => {
let customerPaymentMethod =
defaultPaymentMethodList.contents
->Utils.getDictFromJson
->Js.Dict.get("customer_payment_methods")
->Belt.Option.flatMap(Js.Json.decodeArray)
->Belt.Option.getWithDefault([])
->Belt.Array.get(0)
->Belt.Option.flatMap(Js.Json.decodeObject)
->Belt.Option.getWithDefault(Js.Dict.empty())
let paymentToken = customerPaymentMethod->Utils.getJsonFromDict("payment_token", Js.Json.null)
let paymentMethod = customerPaymentMethod->Utils.getJsonFromDict("payment_method", Js.Json.null)
let paymentMethodType =
customerPaymentMethod->Utils.getJsonFromDict("payment_method_type", Js.Json.null)

let confirmParams =
payload
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("confirmParams"))
->Belt.Option.getWithDefault(Js.Json.null)

let returnUrl =
confirmParams
->Js.Json.decodeObject
->Belt.Option.flatMap(x => x->Js.Dict.get("return_url"))
->Belt.Option.flatMap(Js.Json.decodeString)
->Belt.Option.getWithDefault("")

let confirmParam: ConfirmType.confirmParams = {
return_url: returnUrl,
publishableKey,
}

let paymentIntentID = Js.String2.split(clientSecret, "_secret_")[0]
let endpoint = ApiEndpoint.getApiEndPoint(
~publishableKey=confirmParam.publishableKey,
~isConfirmCall=true,
(),
)
let uri = `${endpoint}/payments/${paymentIntentID}/confirm`
let headers = [("Content-Type", "application/json"), ("api-key", confirmParam.publishableKey)]

let paymentType: PaymentHelpers.payment = switch paymentMethodType
->Js.Json.decodeString
->Belt.Option.getWithDefault("") {
| "apple_pay" => Applepay
| "google_pay" => Gpay
| "debit"
| "credit"
| "" =>
Card
| _ => Other
}

let broswerInfo = BrowserSpec.broswerInfo()

let body = [
("client_secret", clientSecret->Js.Json.string),
("payment_method", paymentMethod),
("payment_token", paymentToken),
("payment_method_type", paymentMethodType),
]

let bodyStr =
body->Js.Array2.concat(broswerInfo)->Js.Dict.fromArray->Js.Json.object_->Js.Json.stringify

PaymentHelpers.intentCall(
~fetchApi=Utils.fetchApi,
~uri,
~headers,
~bodyStr,
~confirmParam: ConfirmType.confirmParams,
~clientSecret,
~optLogger=Some(logger),
~handleUserError=false,
~paymentType,
~iframeId="",
~fetchMethod=Fetch.Post,
~setIsManualRetryEnabled={(. _) => ()},
~switchToCustomPod=false,
~sdkHandleOneClickConfirmPayment=false,
~counter=0,
~isPaymentSession=true,
(),
)
let defaultInitPaymentSession = {
getCustomerSavedPaymentMethods: _ =>
PaymentSessionMethods.getCustomerSavedPaymentMethods(
~clientSecret,
~publishableKey,
~endpoint,
~logger,
~switchToCustomPod,
),
}

let returnObject = {
getDefaultPaymentMethod,
confirmWithDefault,
}
returnObject
defaultInitPaymentSession
}
Loading

0 comments on commit 81121ec

Please sign in to comment.