-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend/frontend): change payment flow (#70)
* feat: removed verify API and changed callback page flow * fix(backend): change payment flow * fix: rollback unwanted changes * fix(backend): redirect after payment verification --------- Co-authored-by: Alireza <[email protected]>
- Loading branch information
1 parent
79a51d5
commit 4d036c1
Showing
3 changed files
with
59 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 51 additions & 56 deletions
107
frontend/src/pages/payment-callback/usePaymentCallbackPage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,55 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useAPI } from '../../providers/APIProvider/APIProvider'; | ||
import { useConfig } from '../../providers/config-provider/ConfigProvider'; | ||
import {useEffect, useState} from 'react'; | ||
import {useConfig} from '../../providers/config-provider/ConfigProvider'; | ||
|
||
export default function usePaymentCallbackPage() { | ||
const { routeParams } = useConfig(); | ||
|
||
const { verifyPaymentData, postVerifyPayment } = useAPI(); | ||
|
||
const [paymentStatus, setPaymentStatus] = useState(false); | ||
const [paymentResultsData, setPaymentResultsData] = useState(); | ||
const [isLoading, setIsLoading] = useState(true) | ||
|
||
useEffect(() => { | ||
if (routeParams == null) return; | ||
|
||
const clientrefid = routeParams['clientrefid']; | ||
if (!clientrefid) | ||
return | ||
postVerifyPayment({ | ||
clientrefid, | ||
}); | ||
}, [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); | ||
} | ||
|
||
const keyDict = { | ||
message: 'Message', | ||
refid: 'Reference ID', | ||
card_number: 'Credit Card Number', | ||
data: "Reference ID" | ||
const {routeParams} = useConfig(); | ||
|
||
const [paymentStatus, setPaymentStatus] = useState(false); | ||
const [paymentResultsData, setPaymentResultsData] = useState(); | ||
const [isLoading, setIsLoading] = useState(true) | ||
|
||
useEffect(() => { | ||
if (routeParams == null) return; | ||
|
||
const refID = routeParams['client_ref_id']; | ||
const status = routeParams['payment_status'] | ||
if (!refID && !status) | ||
return | ||
|
||
const keysDict = { | ||
status: "Status", | ||
redID: "Reference ID" | ||
} | ||
const paymentResultTemp = {} | ||
|
||
if (status) { | ||
const statusValues = { | ||
succeeded: true, | ||
failed: false, | ||
} | ||
|
||
const statusDict = { | ||
true: "Success!", | ||
false: "Failure!" | ||
} | ||
|
||
const statusBool = statusValues[status] ?? false | ||
setPaymentStatus(statusBool) | ||
paymentResultTemp[keysDict.status] = statusDict[statusBool] | ||
} | ||
|
||
if (refID) { | ||
paymentResultTemp[keysDict.redID] = refID | ||
} | ||
|
||
setPaymentResultsData(paymentResultTemp); | ||
setIsLoading(false) | ||
}, [routeParams]); | ||
|
||
return { | ||
routeParams, | ||
paymentStatus, | ||
isLoading, | ||
paymentResultsData, | ||
}; | ||
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]; | ||
}); | ||
setPaymentResultsData(paymentResultTemp); | ||
setIsLoading(false) | ||
}, [verifyPaymentData]); | ||
|
||
return { | ||
routeParams, | ||
paymentStatus, | ||
isLoading, | ||
paymentResultsData, | ||
}; | ||
} |