Spaces:
Sleeping
Sleeping
| // src/lib/api/paymentApi.ts | |
| // Stripe Payment API โ ููุณุชุฎุฏู ุจุนุฏ ูุจูู ุงูุดูุฎ ููุฌูุณุฉ | |
| import { getApiBaseUrl } from './config'; | |
| const BASE_URL = getApiBaseUrl(); | |
| const TOKEN_KEY = 'authToken'; | |
| // โโโ Types โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| export interface InitiatePaymentRequest { | |
| sessionId: number; | |
| studentId: number; | |
| sheikhId: number; | |
| amount: number; | |
| } | |
| export interface InitiatePaymentResponse { | |
| publishableKey: string; | |
| clientSecret: string; | |
| paymentIntentId: string; | |
| } | |
| export interface ConfirmPaymentResponse { | |
| success: boolean; | |
| message: string; | |
| sessionId?: number; | |
| paymentIntentId?: string; | |
| } | |
| // โโโ Error Class โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| export class PaymentApiError extends Error { | |
| public readonly status: number; | |
| constructor(status: number, message: string) { | |
| super(message); | |
| this.status = status; | |
| this.name = 'PaymentApiError'; | |
| } | |
| } | |
| // โโโ Helper โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| function getAuthHeaders(): HeadersInit { | |
| const token = localStorage.getItem(TOKEN_KEY) ?? ''; | |
| return { | |
| 'Content-Type': 'application/json', | |
| ...(token ? { Authorization: `Bearer ${token}` } : {}), | |
| }; | |
| } | |
| // โโโ API Functions โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| /** | |
| * POST /api/payments/initiate/card | |
| * ูุจุฏุฃ ุนู ููุฉ ุงูุฏูุน ููุฑุฌุน clientSecret ู publishableKey ู ู Stripe | |
| */ | |
| export async function initiateCardPayment( | |
| req: InitiatePaymentRequest | |
| ): Promise<InitiatePaymentResponse> { | |
| const res = await fetch(`${BASE_URL}/api/payments/initiate/card`, { | |
| method: 'POST', | |
| headers: getAuthHeaders(), | |
| body: JSON.stringify(req), | |
| }); | |
| if (!res.ok) { | |
| const msg = await res.text().catch(() => res.statusText); | |
| throw new PaymentApiError(res.status, msg); | |
| } | |
| return res.json() as Promise<InitiatePaymentResponse>; | |
| } | |
| /** | |
| * POST /api/payments/confirm-payment/{paymentIntentId} | |
| * ูุคูุฏ ููู Backend ุฅู Stripe ูุฌุญ | |
| */ | |
| export async function confirmPaymentWithBackend( | |
| paymentIntentId: string | |
| ): Promise<ConfirmPaymentResponse> { | |
| const res = await fetch( | |
| `${BASE_URL}/api/payments/confirm-payment/${paymentIntentId}`, | |
| { | |
| method: 'POST', | |
| headers: getAuthHeaders(), | |
| } | |
| ); | |
| if (!res.ok) { | |
| const msg = await res.text().catch(() => res.statusText); | |
| throw new PaymentApiError(res.status, msg); | |
| } | |
| return res.json() as Promise<ConfirmPaymentResponse>; | |
| } |