amanpay / web /src /api /payments.ts
MHamdan's picture
CI deploy 84761d0
de6cac5 verified
Raw
History Blame Contribute Delete
2.37 kB
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<PaymentView> {
return apiRequest<PaymentView>('/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<PaymentView> {
// Authoritative status comes from the backend (which relies on signed webhooks).
return apiRequest<PaymentView>(`/payments/${encodeURIComponent(paymentId)}`)
}
export async function cancelPayment(paymentId: string): Promise<PaymentView> {
return apiRequest<PaymentView>(`/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<PaymentView> {
const body: Record<string, unknown> = {}
if (amountMinor !== undefined) body.amount_minor = amountMinor
if (reason) body.reason = reason
return apiRequest<PaymentView>(`/payments/${encodeURIComponent(paymentId)}/refund`, { method: 'POST', body })
}
export async function getProviders(): Promise<ProvidersResponse> {
return apiRequest<ProvidersResponse>('/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<unknown> {
return apiRequest(`/payments/${encodeURIComponent(paymentId)}/mock-advance`, {
body: { raw_status: rawStatus },
})
}