feat(payment): configure named gateway providers

This commit is contained in:
Clintchiz committed 2026-08-28 09:49:53 +05:30
1 parent c578bc0f92
commit 95c291db64
5 files changed
+162 -5

No files matched your search

+12 -5
View File
@@ -1,6 +1,7 @@
import { basename, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { definePlugin, type PluginContext } from "@wrnexus/plugin";
import type { PaymentConfig } from "./config.ts";
const root = dirname(dirname(fileURLToPath(import.meta.url)));
const extension = basename(dirname(fileURLToPath(import.meta.url))) === "dist" ? ".js" : ".ts";
@@ -39,17 +40,23 @@ export function paymentPlugin() {
: [];
},
configure(config, context) {
const value = config.payment as
{ default?: string; gateways?: Record<string, unknown>; sandbox?: boolean } | undefined;
const value = config.payment as PaymentConfig | undefined;
context.metadata.set(key, Boolean(value));
if (!value) return;
if (!value.default) throw new Error("WRN-PAYMENT-CONFIG: payment.default is required");
if (!value.gateways?.[value.default] && value.default !== "sandbox")
const legacySandbox = value.default === "sandbox" && !value.gateways;
if (!value.gateways?.[value.default] && !legacySandbox)
throw new Error(
`WRN-PAYMENT-CONFIG: default gateway '${value.default}' has no configuration`,
);
if (process.env.NODE_ENV === "production" && value.sandbox)
throw new Error("WRN-PAYMENT-CONFIG: sandbox cannot be enabled in production");
if (
process.env.NODE_ENV === "production" &&
(legacySandbox ||
Object.values(value.gateways ?? {}).some(
(gateway) => gateway && gateway.provider === "sandbox",
))
)
throw new Error("WRN-PAYMENT-CONFIG: sandbox gateways cannot be enabled in production");
},
migrations(context: PluginContext) {
return context.metadata.get(key)