Fallback invalid CoinPay checkout amounts (#42)

Co-authored-by: Codex Microtask Operator <codex-microtask@example.com>
This commit is contained in:
Autowebassat-blip 2026-06-14 07:55:53 +02:00 committed by GitHub
parent ffefd364ce
commit 055cc09d14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -221,6 +221,29 @@ describe("POST /api/hire-us/coinpay-checkout", () => {
expect(body.payment.checkout_url).toBe("https://checkout.stripe.test/card-only");
});
it("falls back when CoinPay returns an invalid checkout 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", 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 ?? 250);
return json(
{
success: true,
payment: {
id: payment.id,
amount_usd: Number(payment.amount_usd ?? payment.amount ?? 250),
amount_usd: Number.isFinite(amountUsd) ? 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,