import { apiRequest } from './client' import type { PaymentView, ProvidersResponse } from '../types' export interface CreatePaymentInput { userId: string amountMinor: number merchantId: string payeeIban: string country?: string currency?: string description?: string idempotencyKey: string // preserved across retries of the SAME user action } export async function createPayment(input: CreatePaymentInput): Promise { return apiRequest('/payments', { headers: { 'Idempotency-Key': input.idempotencyKey }, body: { user_id: input.userId, amount_minor: input.amountMinor, merchant_id: input.merchantId, payee_iban: input.payeeIban, country: input.country, currency: input.currency, description: input.description ?? '', consent: { granted: true, purpose: 'payment initiation' }, }, }) } export async function getPayment(paymentId: string): Promise { // Authoritative status comes from the backend (which relies on signed webhooks). return apiRequest(`/payments/${encodeURIComponent(paymentId)}`) } export async function cancelPayment(paymentId: string): Promise { return apiRequest(`/payments/${encodeURIComponent(paymentId)}/cancel`, { method: 'POST', body: {} }) } /** Refund via the real Payment Core refund API (amount in minor units / halalas). * Omit amountMinor for a full refund of the remaining balance. */ export async function refundPayment(paymentId: string, amountMinor?: number, reason?: string): Promise { const body: Record = {} if (amountMinor !== undefined) body.amount_minor = amountMinor if (reason) body.reason = reason return apiRequest(`/payments/${encodeURIComponent(paymentId)}/refund`, { method: 'POST', body }) } export async function getProviders(): Promise { return apiRequest('/payments/providers') } /** DEV/mock only: ask the backend to simulate the provider advancing this payment * (server-side signed webhook). Rejected by the backend for real providers. */ export async function mockAdvance(paymentId: string, rawStatus: string): Promise { return apiRequest(`/payments/${encodeURIComponent(paymentId)}/mock-advance`, { body: { raw_status: rawStatus }, }) }