Skip to content

Commit

Permalink
refactor: added condition on create call
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanskar2001 committed Sep 19, 2024
1 parent 9bd3e54 commit 6ba916d
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions Hyperswitch-React-Demo-App/src/Payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,40 @@ function Payment() {
const isCypressTestMode = queryParams.get("isCypressTestMode");
const publishableKeyQueryParam = queryParams.get("publishableKey");
const clientSecretQueryParam = queryParams.get("clientSecret");
const url = SELF_SERVER_URL === "" ? ENDPOINT : SELF_SERVER_URL;

useEffect(() => {
const fetchData = async () => {
try {
const url = SELF_SERVER_URL === "" ? ENDPOINT : SELF_SERVER_URL;
const getPaymentData = async () => {
try {
const [configResponse, urlsResponse] = await Promise.all([
fetch(`${url}/config`),
fetch(`${url}/urls`),
]);

const [configResponse, urlsResponse, paymentIntentResponse] =
await Promise.all([
fetch(`${url}/config`),
fetch(`${url}/urls`),
fetch(`${url}/create-payment-intent`),
]);
const paymentIntentResponse = isCypressTestMode
? { clientSecret: clientSecretQueryParam }
: await (await fetch(`${url}/create-payment-intent`)).json();

if (!configResponse.ok || !urlsResponse.ok) {
throw new Error("Network response was not ok");
}

if (
!configResponse.ok ||
!urlsResponse.ok ||
!paymentIntentResponse.ok
) {
throw new Error("Network response was not ok");
}
const paymentDataArray = await Promise.all([
configResponse.json(),
urlsResponse.json(),
]);

const [configData, urlsData, paymentIntentData] = await Promise.all([
configResponse.json(),
urlsResponse.json(),
paymentIntentResponse.json(),
]);
return [...paymentDataArray, paymentIntentResponse];
} catch (error) {
console.error("Error fetching data:", error);
}
};

useEffect(() => {
const fetchData = async () => {
try {
const [configData, urlsData, paymentIntentData] = await getPaymentData(
url
);

const { publishableKey } = configData;
const { serverUrl, clientUrl } = urlsData;
Expand Down

0 comments on commit 6ba916d

Please sign in to comment.