From 8df24df92e9fa488ad81fb27709cbffe12a6bef6 Mon Sep 17 00:00:00 2001
From: Vrishab Srivatsa <136090360+vsrivatsa-juspay@users.noreply.github.com>
Date: Mon, 19 Feb 2024 14:23:15 +0530
Subject: [PATCH] fix: Handling error cases for payment methods list (#164)
Co-authored-by: Praful Koppalkar <126236898+prafulkoppalkar@users.noreply.github.com>
Co-authored-by: Akash Kamble <127744130+akash-kamble-juspay@users.noreply.github.com>
---
src/LoaderController.res | 14 ++++++++++----
src/PaymentElement.res | 5 +++--
src/Types/PaymentType.res | 4 ++--
src/Utilities/PaymentHelpers.res | 4 ++--
4 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/src/LoaderController.res b/src/LoaderController.res
index abee9d6a1..c33ec17e8 100644
--- a/src/LoaderController.res
+++ b/src/LoaderController.res
@@ -127,7 +127,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => {
logger.setLogInitiated()
switch paymentlist {
| Loaded(_)
- | LoadError(_) => ()
+ | LoadError => ()
| _ =>
setList(._ =>
showCardFormByDefault && Utils.checkPriorityList(paymentMethodOrder) ? SemiLoaded : Loading
@@ -340,10 +340,16 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => {
if dict->getDictIsSome("paymentMethodList") {
let list = dict->getJsonObjectFromDict("paymentMethodList")
list == Js.Dict.empty()->Js.Json.object_
- ? setList(._ => LoadError(Js.Dict.empty()->Js.Json.object_))
+ ? setList(._ => LoadError)
: switch list->Utils.getDictFromJson->Js.Dict.get("error") {
- | Some(err) => setList(._ => LoadError(err))
- | None => setList(._ => Loaded(list))
+ | Some(_) => setList(._ => LoadError)
+ | None =>
+ let isNonEmptyPaymentMethodList =
+ list
+ ->Utils.getDictFromJson
+ ->Utils.getArray("payment_methods")
+ ->Js.Array2.length > 0
+ setList(._ => isNonEmptyPaymentMethodList ? Loaded(list) : LoadError)
}
}
if dict->getDictIsSome("customerPaymentMethods") {
diff --git a/src/PaymentElement.res b/src/PaymentElement.res
index 1e8240830..3580221d0 100644
--- a/src/PaymentElement.res
+++ b/src/PaymentElement.res
@@ -90,6 +90,7 @@ let make = (
)
}
: ()
+ | LoadError
| SemiLoaded =>
setPaymentOptions(_ =>
showCardFormByDefault && Utils.checkPriorityList(paymentMethodOrder) ? ["card"] : []
@@ -142,7 +143,7 @@ let make = (
? ""
: switch methodslist {
| SemiLoaded
- | LoadError(_) =>
+ | LoadError =>
showCardFormByDefault && Utils.checkPriorityList(paymentMethodOrder) ? "card" : ""
| Loaded(_) =>
paymentOptions->Js.Array2.includes(selectedOption) && showCardFormByDefault
@@ -319,7 +320,7 @@ let make = (
{switch methodslist {
- | LoadError(_) => React.null
+ | LoadError => React.null
| _ =>
Js.Array2.length == 0 && walletOptions->Js.Array2.length == 0}>
diff --git a/src/Types/PaymentType.res b/src/Types/PaymentType.res
index a1fbe2f87..77f21ee21 100644
--- a/src/Types/PaymentType.res
+++ b/src/Types/PaymentType.res
@@ -298,7 +298,7 @@ let getAddress = (dict, str, logger) => {
line2: getWarningString(json, "line2", "", ~logger),
city: getWarningString(json, "city", "", ~logger),
state: getWarningString(json, "state", "", ~logger),
- country: country,
+ country,
postal_code: getWarningString(json, "postal_code", "", ~logger),
}
})
@@ -893,7 +893,7 @@ let itemToObjMapper = (dict, logger) => {
}
}
-type loadType = Loading | Loaded(Js.Json.t) | SemiLoaded | LoadError(Js.Json.t)
+type loadType = Loading | Loaded(Js.Json.t) | SemiLoaded | LoadError
let getIsAllStoredCardsHaveName = (savedCards: array) => {
savedCards
diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res
index 7f6e2ab1a..defda0ee2 100644
--- a/src/Utilities/PaymentHelpers.res
+++ b/src/Utilities/PaymentHelpers.res
@@ -436,7 +436,7 @@ let usePaymentSync = (optLogger: option, paymentType: pay
| Loaded(_) => paymentSync()
| SemiLoaded
| Loading
- | LoadError(_) => ()
+ | LoadError => ()
}
| None =>
postFailedSubmitResponse(
@@ -629,7 +629,7 @@ let usePaymentIntent = (optLogger: option, paymentType: p
}
| SemiLoaded => intentWithoutMandate()
| Loading
- | LoadError(_) => ()
+ | LoadError => ()
}
| None =>
postFailedSubmitResponse(