54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
export type PaymentResult = {
|
|
success: boolean;
|
|
holdId?: string;
|
|
error?: string;
|
|
};
|
|
|
|
export interface PaymentProvider {
|
|
createHold(amount: number, currency: string): Promise<PaymentResult>;
|
|
release(holdId: string): Promise<{ success: boolean; error?: string }>;
|
|
refund(holdId: string): Promise<{ success: boolean; error?: string }>;
|
|
}
|
|
|
|
export class MockPaymentProvider implements PaymentProvider {
|
|
private holds: Map<string, { amount: number; currency: string; released: boolean }> = new Map();
|
|
|
|
async createHold(amount: number, currency: string): Promise<PaymentResult> {
|
|
const holdId = `hold_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
this.holds.set(holdId, { amount, currency, released: false });
|
|
|
|
return {
|
|
success: true,
|
|
holdId,
|
|
};
|
|
}
|
|
|
|
async release(holdId: string): Promise<{ success: boolean; error?: string }> {
|
|
const hold = this.holds.get(holdId);
|
|
|
|
if (!hold) {
|
|
return { success: false, error: 'Hold not found' };
|
|
}
|
|
|
|
if (hold.released) {
|
|
return { success: false, error: 'Hold already released' };
|
|
}
|
|
|
|
hold.released = true;
|
|
return { success: true };
|
|
}
|
|
|
|
async refund(holdId: string): Promise<{ success: boolean; error?: string }> {
|
|
const hold = this.holds.get(holdId);
|
|
|
|
if (!hold) {
|
|
return { success: false, error: 'Hold not found' };
|
|
}
|
|
|
|
this.holds.delete(holdId);
|
|
return { success: true };
|
|
}
|
|
}
|
|
|
|
export const paymentProvider = new MockPaymentProvider();
|