change payment success file and cards endpoints

This commit is contained in:
Hemant Vishwakarma
2026-04-23 18:33:33 +05:30
parent b3bb9f08cd
commit cd7c6ffbaf
2 changed files with 20 additions and 19 deletions

View File

@@ -43,7 +43,7 @@ export const cardsApi = createApi({
}), }),
payForCard: builder.mutation({ payForCard: builder.mutation({
query: (id) => ({ query: (id) => ({
url: `/website/passes/${id}/pay`, url: `/website/passes/${id}/pay`,
method: "POST", method: "POST",
body: {}, body: {},
@@ -51,10 +51,10 @@ export const cardsApi = createApi({
}), }),
confirmCardPayment: builder.mutation({ confirmCardPayment: builder.mutation({
query: (id) => ({ query: ({ bookingId, paymentIntentId }: { bookingId: string; paymentIntentId: string }) => ({
url: `/website/passes/${id}/confirm-payment`, url: `/website/passes/${bookingId}/confirm-payment`,
method: "POST", method: "POST",
// body: id, body: { paymentIntentId }, // ✅ send paymentIntentId to backend
}), }),
}), }),

View File

@@ -15,7 +15,6 @@ interface PaymentSuccessPageProps {
user?: { email: string; name: string } | null; user?: { email: string; name: string } | null;
} }
// Helper to get cookie value
const getCookie = (name: string): string | null => { const getCookie = (name: string): string | null => {
const value = `; ${document.cookie}`; const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`); const parts = value.split(`; ${name}=`);
@@ -39,34 +38,36 @@ export function PaymentSuccessPage({
useEffect(() => { useEffect(() => {
const confirm = async () => { const confirm = async () => {
// Try all possible sources // 1⃣ Get bookingId from storage / cookie
let bookingId = getCookie('pendingBookingId'); let bookingId = getCookie('pendingBookingId');
if (!bookingId) bookingId = localStorage.getItem('pendingBookingId'); if (!bookingId) bookingId = localStorage.getItem('pendingBookingId');
if (!bookingId) bookingId = sessionStorage.getItem('pendingBookingId'); if (!bookingId) bookingId = sessionStorage.getItem('pendingBookingId');
if (!bookingId) bookingId = searchParams.get('bookingId'); if (!bookingId) bookingId = searchParams.get('bookingId');
console.log('Retrieved bookingId from sources:', { // 2⃣ Get payment_intent from URL (Stripe redirect)
cookie: getCookie('pendingBookingId'), const paymentIntentId = searchParams.get('payment_intent');
localStorage: localStorage.getItem('pendingBookingId'),
sessionStorage: sessionStorage.getItem('pendingBookingId'), console.log('🔍 Retrieved data:', { bookingId, paymentIntentId });
queryParam: searchParams.get('bookingId'),
final: bookingId,
});
if (!bookingId) { if (!bookingId) {
setStatus('error'); setStatus('error');
setErrorMsg( setErrorMsg('Booking ID not found. Please contact support.');
'Booking ID not found. Please contact support with your order details. ' + return;
'If you just completed payment, your order may still be processing.' }
);
if (!paymentIntentId) {
setStatus('error');
setErrorMsg('Payment intent missing. Please contact support.');
return; return;
} }
try { try {
await confirmPayment(bookingId).unwrap(); // 3⃣ Call confirm endpoint with paymentIntentId
await confirmPayment({ bookingId, paymentIntentId }).unwrap();
setStatus('success'); setStatus('success');
toast.success('Payment confirmed! Your order is complete.'); toast.success('Payment confirmed! Your order is complete.');
// Clean up all storage
// 4⃣ Clean up stored bookingId
document.cookie = 'pendingBookingId=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; document.cookie = 'pendingBookingId=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
localStorage.removeItem('pendingBookingId'); localStorage.removeItem('pendingBookingId');
sessionStorage.removeItem('pendingBookingId'); sessionStorage.removeItem('pendingBookingId');