From e1517cfe9883a79c90210ce018e9b7802408507d Mon Sep 17 00:00:00 2001 From: Alireza Date: Wed, 29 Nov 2023 03:10:35 +0330 Subject: [PATCH] feat: finished payment callback --- frontend/src/pages/my-account/useMyAccount.js | 54 +++++++++---------- .../usePaymentCallbackPage.js | 13 +++-- .../src/providers/APIProvider/APIProvider.jsx | 9 ++-- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/frontend/src/pages/my-account/useMyAccount.js b/frontend/src/pages/my-account/useMyAccount.js index de4df7c..6352892 100644 --- a/frontend/src/pages/my-account/useMyAccount.js +++ b/frontend/src/pages/my-account/useMyAccount.js @@ -1,7 +1,7 @@ -import { useCallback, useEffect, useState } from 'react'; -import { useNavigate } from 'react-router-dom'; -import { useAPI } from '../../providers/APIProvider/APIProvider.jsx'; -import { useConfig } from '../../providers/config-provider/ConfigProvider.jsx'; +import {useCallback, useEffect, useState} from 'react'; +import {useNavigate} from 'react-router-dom'; +import {useAPI} from '../../providers/APIProvider/APIProvider.jsx'; +import {useConfig} from '../../providers/config-provider/ConfigProvider.jsx'; export default function useMyAccount() { @@ -46,35 +46,32 @@ export default function useMyAccount() { }) }, [removeFromUserCart]) - const handleBuyCart = useCallback(() => { - // TODO: this endpoint has some issues - postPaymentData({ call_back: 'https://aaiss.ir/callback' }); - }, [postPaymentData]); + const handleBuyCart = useCallback(() => { + postPaymentData({call_back: 'https://aaiss.ir/callback'}); + }, [postPaymentData]); - useEffect(() => { - if (paymentData) { - console.log({ paymentData }); - const { message } = paymentData; - // TODO: THIS SHOULD BE TESTED ON LIVE TEST!!!!! - window.location.replace(message); - } - }, [paymentData]); - - useEffect(() => { - if (paymentData == null) return; + useEffect(() => { + if (!paymentData) + return if (paymentData.status !== 200 || paymentData.data.status !== 200) { - const toastTemp = {} - toastTemp.message = "Unknown Error! Please Try Again Later" - toastTemp.alertType = "error" setOpenToast(true) - setToastData(toastTemp) + setToastData({ + message: "There was an Error Regarding Payment! Please Try Again Later", + alertType: "error" + }) return } - console.log(paymentData.data.message) - navigate(paymentData.data.message) - }, [paymentData]) + setOpenToast(true) + setToastData({ + message: "Success! Redirecting You to the Desired Website...", + alertType: "success" + }) + setTimeout(() => { + window.location.href = paymentData.data.data.payment_url + }, 2000) + }, [paymentData]); useEffect(() => { if (removeFromCartResponse == null) @@ -96,14 +93,11 @@ export default function useMyAccount() { break; } - setRemoveFromCartResponse(null) setToastData(toastDataTemp) setOpenToast(true) - setWorkshopsData(null) - setPresentationsData(null) getUserWorkshops() getUserPresentations() - }, [removeFromCartResponse]) + }, [removeFromCartResponse, getUserPresentations, getUserWorkshops, setOpenToast, setToastData]) useEffect(() => { if (!accessToken) diff --git a/frontend/src/pages/payment-callback/usePaymentCallbackPage.js b/frontend/src/pages/payment-callback/usePaymentCallbackPage.js index 31ef255..7c45283 100644 --- a/frontend/src/pages/payment-callback/usePaymentCallbackPage.js +++ b/frontend/src/pages/payment-callback/usePaymentCallbackPage.js @@ -14,28 +14,35 @@ export default function usePaymentCallbackPage() { if (routeParams == null) return; const clientrefid = routeParams['clientrefid']; + if (!clientrefid) + return postVerifyPayment({ clientrefid, }); - }, [routeParams]); + }, [postVerifyPayment, routeParams]); useEffect(() => { if (verifyPaymentData == null) return; + const paymentResultTemp = {}; if (verifyPaymentData.status !== 200 || verifyPaymentData.data.status !== 200) { setPaymentStatus(false); + paymentResultTemp['Message'] = "Payment Failed!" } else { + paymentResultTemp['Message'] = "Success!" setPaymentStatus(true); } - setPaymentStatus(false); - const paymentResultTemp = {}; const keyDict = { message: 'Message', refid: 'Reference ID', card_number: 'Credit Card Number', + data: "Reference ID" }; + const removedKeys = ['status', 'message'] Object.keys(verifyPaymentData.data).forEach((key) => { + if (removedKeys.indexOf(key) > -1) + return if (key in keyDict) paymentResultTemp[keyDict[key]] = verifyPaymentData.data[key]; else paymentResultTemp[key] = verifyPaymentData.data[key]; }); diff --git a/frontend/src/providers/APIProvider/APIProvider.jsx b/frontend/src/providers/APIProvider/APIProvider.jsx index 43e8a1d..e36b4ad 100644 --- a/frontend/src/providers/APIProvider/APIProvider.jsx +++ b/frontend/src/providers/APIProvider/APIProvider.jsx @@ -213,7 +213,7 @@ export function APIProvider({ children }) { const postVerifyPayment = useCallback( async (data) => { await service - .get(`${URL.baseURL}${URL.services.default}${URL.endpoints.payment.verify}`, data) + .post(`${URL.baseURL}${URL.services.default}${URL.endpoints.payment.verify}`, data) .then((response) => { setVerifyPaymentData(response); }) @@ -233,8 +233,11 @@ export function APIProvider({ children }) { }, }) .then((response) => { - setPaymentData(response.data); - }); + setPaymentData(response); + }) + .catch(error => { + setPaymentData(error.response) + }); }, [currentYear, service, getAccessTokenHeader], );