Fallback malformed CoinPay amounts

This commit is contained in:
Codex Microtask Operator 2026-06-12 18:44:06 +02:00
parent 8f4691584c
commit 4ee9201b9d
2 changed files with 29 additions and 1 deletions

View file

@ -221,6 +221,33 @@ describe("POST /api/hire-us/coinpay-checkout", () => {
expect(body.payment.checkout_url).toBe("https://checkout.stripe.test/card-only");
});
it("falls back to the configured checkout amount when CoinPay returns a malformed amount", async () => {
process.env.COINPAY_API_KEY = "cp_test_key";
process.env.COINPAY_API_URL = "https://coinpayportal.example";
process.env.COINPAY_BUSINESS_ID = "business-123";
process.env.COINPAY_ELIGIBILITY_MERCHANT_ID = "merchant-123";
vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => {
const url = typeof input === "string" ? input : input.toString();
if (url.includes("/api/payments/merchant-eligibility")) {
return jsonResponse({ success: true, accepts_card: true, accepts_crypto: false, chains: [] });
}
return jsonResponse({ success: true, payment: { id: "pay_123", amount_usd: "not-a-number" } }, 201);
});
const response = await coinpayCheckout(
new NextRequest("http://localhost/api/hire-us/coinpay-checkout", {
method: "POST",
headers: { "content-type": "application/json" },
body: "{}"
})
);
const body = await response.json();
expect(response.status).toBe(201);
expect(body.payment.amount_usd).toBe(250);
});
it("does not create checkout when no payment rail is available", async () => {
process.env.COINPAY_API_KEY = "cp_test_key";
process.env.COINPAY_API_URL = "https://coinpayportal.example";

View file

@ -70,12 +70,13 @@ export async function POST(request: NextRequest) {
}
const payment = (payload.payment as Record<string, unknown>) || {};
const amountUsd = Number(payment.amount_usd ?? payment.amount);
return json(
{
success: true,
payment: {
id: payment.id,
amount_usd: Number(payment.amount_usd ?? payment.amount ?? 250),
amount_usd: Number.isFinite(amountUsd) && amountUsd > 0 ? amountUsd : 250,
payment_method: payment.stripe_checkout_url ? "card" : paymentRail.method,
currency: payment.currency ?? payment.blockchain ?? paymentRail.blockchain ?? paymentRail.currency,
crypto_amount: payment.amount_crypto ?? payment.crypto_amount ?? null,