Skip to content

Commit 055cc09

Browse files
Autowebassat-blipCodex Microtask Operator
andauthored
Fallback invalid CoinPay checkout amounts (#42)
Co-authored-by: Codex Microtask Operator <codex-microtask@example.com>
1 parent ffefd36 commit 055cc09

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

apps/logicsrc-web/contract/logicsrc-web.contract.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,29 @@ describe("POST /api/hire-us/coinpay-checkout", () => {
221221
expect(body.payment.checkout_url).toBe("https://checkout.stripe.test/card-only");
222222
});
223223

224+
it("falls back when CoinPay returns an invalid checkout amount", async () => {
225+
process.env.COINPAY_API_KEY = "cp_test_key";
226+
process.env.COINPAY_API_URL = "https://coinpayportal.example";
227+
process.env.COINPAY_BUSINESS_ID = "business-123";
228+
process.env.COINPAY_ELIGIBILITY_MERCHANT_ID = "merchant-123";
229+
230+
vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => {
231+
const url = typeof input === "string" ? input : input.toString();
232+
if (url.includes("/api/payments/merchant-eligibility")) {
233+
return jsonResponse({ success: true, accepts_card: true, accepts_crypto: false, chains: [] });
234+
}
235+
return jsonResponse({ success: true, payment: { id: "pay_123", amount_usd: "not-a-number" } }, 201);
236+
});
237+
238+
const response = await coinpayCheckout(
239+
new NextRequest("http://localhost/api/hire-us/coinpay-checkout", { method: "POST", body: "{}" })
240+
);
241+
const body = await response.json();
242+
243+
expect(response.status).toBe(201);
244+
expect(body.payment.amount_usd).toBe(250);
245+
});
246+
224247
it("does not create checkout when no payment rail is available", async () => {
225248
process.env.COINPAY_API_KEY = "cp_test_key";
226249
process.env.COINPAY_API_URL = "https://coinpayportal.example";

apps/logicsrc-web/src/app/api/hire-us/coinpay-checkout/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ export async function POST(request: NextRequest) {
7070
}
7171

7272
const payment = (payload.payment as Record<string, unknown>) || {};
73+
const amountUsd = Number(payment.amount_usd ?? payment.amount ?? 250);
7374
return json(
7475
{
7576
success: true,
7677
payment: {
7778
id: payment.id,
78-
amount_usd: Number(payment.amount_usd ?? payment.amount ?? 250),
79+
amount_usd: Number.isFinite(amountUsd) ? amountUsd : 250,
7980
payment_method: payment.stripe_checkout_url ? "card" : paymentRail.method,
8081
currency: payment.currency ?? payment.blockchain ?? paymentRail.blockchain ?? paymentRail.currency,
8182
crypto_amount: payment.amount_crypto ?? payment.crypto_amount ?? null,

0 commit comments

Comments
 (0)