Skip to content

Commit

Permalink
Merge pull request #39 from AAISS/fix/small-defects
Browse files Browse the repository at this point in the history
feat: finished payment callback
  • Loading branch information
AlirezaYousefpourM authored Nov 28, 2023
2 parents 347ae1b + e1517cf commit 9aa8df5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
54 changes: 24 additions & 30 deletions frontend/src/pages/my-account/useMyAccount.js
Original file line number Diff line number Diff line change
@@ -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() {

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/pages/payment-callback/usePaymentCallbackPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/providers/APIProvider/APIProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -233,8 +233,11 @@ export function APIProvider({ children }) {
},
})
.then((response) => {
setPaymentData(response.data);
});
setPaymentData(response);
})
.catch(error => {
setPaymentData(error.response)
});
},
[currentYear, service, getAccessTokenHeader],
);
Expand Down

0 comments on commit 9aa8df5

Please sign in to comment.