shablony-prodavanam/src/autopay.js
Peppa 4172c24daf
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 3с
peppa/build build: success за 1с
ci / checks (pull_request) Successful in 10s
deploy / deploy (pull_request) Successful in 13s
SP-5: автоподтверждение оплаты Kaspi (мок-провайдер), заключение по ApiPay
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 16:39:08 +05:00

43 lines
1.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Контракт платёжного провайдера автоподтверждения 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;
}