|
2 | 2 |
|
3 | 3 | import { useState, useEffect, useCallback, useRef } from 'react'; |
4 | 4 | import { useRouter, useParams } from 'next/navigation'; |
| 5 | +import { authFetch } from '@/lib/auth/client'; |
5 | 6 |
|
6 | 7 | const PAYMENT_EXPIRY_MINUTES = 15; |
7 | 8 | const POLL_INTERVAL_MS = 5000; // Poll every 5 seconds |
@@ -117,18 +118,10 @@ export default function PaymentDetailPage() { |
117 | 118 | setPaymentStatus(data.status); |
118 | 119 |
|
119 | 120 | // Fetch full payment details |
120 | | - const token = localStorage.getItem('auth_token'); |
121 | | - if (token) { |
122 | | - const paymentResponse = await fetch(`/api/payments/${paymentId}`, { |
123 | | - headers: { |
124 | | - Authorization: `Bearer ${token}`, |
125 | | - }, |
126 | | - }); |
127 | | - if (paymentResponse.ok) { |
128 | | - const paymentData = await paymentResponse.json(); |
129 | | - if (paymentData.success && paymentData.payment) { |
130 | | - setPayment(paymentData.payment); |
131 | | - } |
| 121 | + const paymentResult = await authFetch(`/api/payments/${paymentId}`, {}); |
| 122 | + if (paymentResult && paymentResult.response.ok) { |
| 123 | + if (paymentResult.data.success && paymentResult.data.payment) { |
| 124 | + setPayment(paymentResult.data.payment); |
132 | 125 | } |
133 | 126 | } |
134 | 127 |
|
@@ -157,17 +150,12 @@ export default function PaymentDetailPage() { |
157 | 150 | // Poll for payment status |
158 | 151 | const pollPaymentStatus = useCallback(async () => { |
159 | 152 | try { |
160 | | - const token = localStorage.getItem('auth_token'); |
161 | | - if (!token) return; |
| 153 | + const result = await authFetch(`/api/payments/${paymentId}`, {}); |
| 154 | + if (!result) return; |
162 | 155 |
|
163 | | - const response = await fetch(`/api/payments/${paymentId}`, { |
164 | | - headers: { |
165 | | - Authorization: `Bearer ${token}`, |
166 | | - }, |
167 | | - }); |
| 156 | + const { response, data } = result; |
168 | 157 |
|
169 | 158 | if (response.ok) { |
170 | | - const data = await response.json(); |
171 | 159 | if (data.success && data.payment) { |
172 | 160 | setPayment(data.payment); |
173 | 161 | setPaymentStatus(data.payment.status); |
@@ -210,19 +198,10 @@ export default function PaymentDetailPage() { |
210 | 198 | useEffect(() => { |
211 | 199 | const fetchPayment = async () => { |
212 | 200 | try { |
213 | | - const token = localStorage.getItem('auth_token'); |
214 | | - if (!token) { |
215 | | - router.push('/login'); |
216 | | - return; |
217 | | - } |
| 201 | + const result = await authFetch(`/api/payments/${paymentId}`, {}, router); |
| 202 | + if (!result) return; |
218 | 203 |
|
219 | | - const response = await fetch(`/api/payments/${paymentId}`, { |
220 | | - headers: { |
221 | | - Authorization: `Bearer ${token}`, |
222 | | - }, |
223 | | - }); |
224 | | - |
225 | | - const data = await response.json(); |
| 204 | + const { response, data } = result; |
226 | 205 |
|
227 | 206 | if (!response.ok || !data.success) { |
228 | 207 | setError(data.error || 'Failed to load payment'); |
|
0 commit comments