Rebasely Beta: data may be purged

examples/storefront Public

TypeScript · 40 lines · 2.2 KiB · at c6a00ae

import assert from "node:assert/strict";import { test } from "node:test";import { stripePayments } from "../src/payments.ts";type Seen = { url: string; method: string; headers: Headers; body: URLSearchParams };function fakeStripe(respond: (seen: Seen) => { status?: number; body: unknown }) {  const seen: Seen[] = [];  const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {    const s = { url: String(input), method: init?.method ?? "GET", headers: new Headers(init?.headers), body: new URLSearchParams(String(init?.body ?? "")) };    seen.push(s);    const { status = 200, body } = respond(s);    return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json", "request-id": "req_test" } });  }) as typeof fetch;  return { seen, fetchFn };}test("creates a PaymentIntent for the order total with the order id as idempotency key", async () => {  const stripe = fakeStripe(() => ({ body: { id: "pi_123", object: "payment_intent", client_secret: "pi_123_secret_abc" } }));  const intent = await stripePayments("sk_test_123", stripe.fetchFn).createIntent({ order_id: "ord-1", amount_cents: 6000, currency: "usd", email: "ada@park.test" });  assert.deepEqual(intent, { id: "pi_123", client_secret: "pi_123_secret_abc" });  const [req] = stripe.seen;  assert.equal(req!.method, "POST");  assert.equal(new URL(req!.url).pathname, "/v1/payment_intents");  assert.equal(req!.headers.get("idempotency-key"), "order-ord-1");  assert.equal(req!.body.get("amount"), "6000");  assert.equal(req!.body.get("currency"), "usd");  assert.equal(req!.body.get("metadata[order_id]"), "ord-1");  assert.equal(req!.body.get("automatic_payment_methods[enabled]"), "true");});test("cancelling an intent that already succeeded answers false instead of throwing", async () => {  const stripe = fakeStripe(() => ({    status: 400,    body: { error: { type: "invalid_request_error", code: "payment_intent_unexpected_state", message: "This PaymentIntent has already succeeded." } },  }));  assert.equal(await stripePayments("sk_test_123", stripe.fetchFn).cancelIntent("pi_123"), false);  assert.equal(new URL(stripe.seen[0]!.url).pathname, "/v1/payment_intents/pi_123/cancel");});