examples/storefront Public
// Stripe behind the Payments interface. Only the payments Worker holds STRIPE_SECRET_KEY.import Stripe from "stripe";import type { Payments } from "../../../src/payments.ts";export function stripePayments(secretKey: string, fetchFn?: typeof fetch): Payments { const stripe = new Stripe(secretKey, { httpClient: Stripe.createFetchHttpClient(fetchFn), maxNetworkRetries: 2, timeout: 10_000, }); return { async createIntent(c) { const intent = await stripe.paymentIntents.create( { amount: c.amount_cents, currency: c.currency, receipt_email: c.email, automatic_payment_methods: { enabled: true }, metadata: { order_id: c.order_id }, }, { idempotencyKey: `order-${c.order_id}` }, ); if (!intent.client_secret) throw new Error(`PaymentIntent ${intent.id} has no client_secret`); return { id: intent.id, client_secret: intent.client_secret }; }, async cancelIntent(intentId) { try { await stripe.paymentIntents.cancel(intentId); return true; } catch (e) { if (e instanceof Stripe.errors.StripeInvalidRequestError && e.code === "payment_intent_unexpected_state") return false; throw e; } }, };}