diff --git a/src/components/Billing/AddCardMethodModal.jsx b/src/components/Billing/AddCardMethodModal.jsx index 74f5055..bb173e2 100644 --- a/src/components/Billing/AddCardMethodModal.jsx +++ b/src/components/Billing/AddCardMethodModal.jsx @@ -1,22 +1,35 @@ import { GlobalContext } from "@/globalContext"; import MkdSDK from "@/utils/MkdSDK"; import { Dialog, Transition } from "@headlessui/react"; -import { CardCvcElement, CardExpiryElement, CardNumberElement, useElements, useStripe } from "@stripe/react-stripe-js"; +import { + CardCvcElement, + CardExpiryElement, + CardNumberElement, + useElements, + useStripe, +} from "@stripe/react-stripe-js"; import React, { Fragment, useState } from "react"; import { useContext } from "react"; import { LoadingButton } from "../frontend"; -export default function AddCardMethodModal({ modalOpen, closeModal, onSuccess }) { +export default function AddCardMethodModal({ + modalOpen, + closeModal, + onSuccess, +}) { const [loading, setLoading] = useState(false); const stripe = useStripe(); const elements = useElements(); - const { dispatch: globalDispatch, state: globalState } = useContext(GlobalContext); + const { dispatch: globalDispatch, state: globalState } = + useContext(GlobalContext); const sdk = new MkdSDK(); const [ctrl] = useState(new AbortController()); + const [error, setError] = useState(""); const addNewCard = async (e) => { setLoading(true); e.preventDefault(); + setError(""); // create stripe token try { const cardNum = elements.getElement("cardNumber"); @@ -25,35 +38,123 @@ export default function AddCardMethodModal({ modalOpen, closeModal, onSuccess }) globalDispatch({ type: "SHOW_ERROR", payload: { - message: r.error?.message ? r.error?.message : r?.trace?.raw?.message, + message: r.error?.message + ? r.error?.message + : r?.trace?.raw?.message, }, }); + setError( + r.error?.message ? r.error?.message : r?.trace?.raw?.message + ); } else { - await sdk.createCustomerStripeCard({ sourceToken: r ? r.token.id : result.token.id }, ctrl.signal); + await sdk.createCustomerStripeCard( + { sourceToken: r ? r.token.id : result.token.id }, + ctrl.signal + ); closeModal(); onSuccess(); } - } - ); + }); } catch (error) { if (error.name == "AbortError") { setLoading(false); return; } - console.log(error) + console.log(error); globalDispatch({ type: "SHOW_ERROR", payload: { message: error?.message ? error?.message : "Declined", }, }); + setError(error?.message ? error?.message : "Declined"); } setLoading(false); }; return ( -
- {/* Add Modal UI here to allow card to be created */} -
+ + + +
+ +
+
+ + + + Add New Card + +
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ {error &&
{error}
} +
+ + + Add Card + +
+
+
+
+
+
+
+
); }