43 lines
1.9 KiB
JavaScript
43 lines
1.9 KiB
JavaScript
// Контракт платёжного провайдера автоподтверждения Kaspi (ApiPay-подобный: счёт на номер телефона):
|
||
// createInvoice({ orderId, phone, amount }) -> { invoiceId }
|
||
// getInvoice(invoiceId) -> { status: "pending" | "paid" | "failed" | "expired", amount, paymentId }
|
||
// Реальный клиент появится, когда у владельца будет ключ сервиса; пока — mockAutopayProvider.
|
||
export function mockAutopayProvider() {
|
||
const invoices = new Map();
|
||
let seq = 1;
|
||
return {
|
||
invoices,
|
||
down: false,
|
||
async createInvoice({ orderId, phone, amount }) {
|
||
if (this.down) throw new Error("mock: сервис недоступен");
|
||
const invoiceId = `mock-inv-${seq++}`;
|
||
invoices.set(invoiceId, { orderId, phone, amount, status: "pending", paymentId: null });
|
||
return { invoiceId };
|
||
},
|
||
async getInvoice(invoiceId) {
|
||
if (this.down) throw new Error("mock: сервис недоступен");
|
||
const inv = invoices.get(invoiceId);
|
||
if (!inv) throw new Error("mock: счёт не найден");
|
||
return { status: inv.status, amount: inv.amount, paymentId: inv.paymentId };
|
||
},
|
||
// Тестовый хелпер: имитирует действие покупателя/сервиса.
|
||
settle(invoiceId, patch) {
|
||
Object.assign(invoices.get(invoiceId), patch);
|
||
},
|
||
};
|
||
}
|
||
|
||
export function createAutopayProvider(config) {
|
||
if (config.autopayProvider === "mock") return mockAutopayProvider();
|
||
return null;
|
||
}
|
||
|
||
// "+7 700 111 22 33", "8700...", "700..." -> "77001112233"; иначе null.
|
||
export function normalizePhone(text) {
|
||
const d = String(text).replace(/[\s()\-+]/g, "");
|
||
if (!/^\d+$/.test(d)) return null;
|
||
if (d.length === 11 && /^[78]/.test(d)) return `7${d.slice(1)}`;
|
||
if (d.length === 10) return `7${d}`;
|
||
return null;
|
||
}
|