Files
fotospiel-app/resources/js/lib/giftVouchers.ts

64 lines
1.6 KiB
TypeScript

export type GiftVoucherTier = {
key: string;
label: string;
amount: number;
currency: string;
paddle_price_id?: string | null;
can_checkout: boolean;
};
export type GiftVoucherCheckoutRequest = {
tier_key: string;
purchaser_email: string;
recipient_email?: string;
recipient_name?: string;
message?: string;
success_url?: string;
return_url?: string;
};
export type GiftVoucherCheckoutResponse = {
checkout_url: string | null;
expires_at: string | null;
id: string | null;
};
export async function fetchGiftVoucherTiers(): Promise<GiftVoucherTier[]> {
const response = await fetch('/api/v1/marketing/gift-vouchers/tiers', {
headers: {
Accept: 'application/json',
},
});
if (!response.ok) {
throw new Error('Failed to load gift voucher tiers');
}
const payload = await response.json();
return (payload?.data ?? []) as GiftVoucherTier[];
}
export async function createGiftVoucherCheckout(data: GiftVoucherCheckoutRequest): Promise<GiftVoucherCheckoutResponse> {
const response = await fetch('/api/v1/marketing/gift-vouchers/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(data),
});
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
const message =
(payload?.errors && typeof payload.errors === 'object' && Object.values(payload.errors)[0]?.[0]) ||
payload?.message ||
'Unable to start checkout';
throw new Error(message);
}
return payload as GiftVoucherCheckoutResponse;
}