examples/storefront Public
Move payments into its own Worker #2
- workers/payments/src/index.ts line 10
10 return stripePayments(this.env.STRIPE_SECRET_KEY).createIntent(charge);-
Justin Torre
This builds a Stripe client on every call. It is cheap (no network until the request), and it keeps the key read from
this.envper request instead of caching a client in module scope across secret rotations. If refunds add more methods, a smallprivate get stripe()would tidy it up.
-
Notes for whoever reviews this. The Visual tab is the quickest way in: the system diagram shows stripe-webhook removed and payments added, and in the checkout flow the PaymentIntent step is rerouted from storefront-api to payments with a new PaymentsService step before it. The part I would look at hardest is the rollout order in the description, since the two Workers now bind each other and storefront-api will not deploy until storefront-payments exists. Holding the merge until both Stripe endpoints are registered.
Ready to merge
- Successduragit/ci/testsucceededDetails
- No conflictsmain can fast-forward to the head
Sign in to comment.
storefront-api has held
STRIPE_SECRET_KEYsince checkout landed, next to every public route, the admin routes and image uploads. Stripe code was also split across two Workers: storefront-api created intents andstorefront-stripe-webhookverified events. This moves all of it into one Worker,storefront-payments, ahead of adding refunds.What changes
workers/payments. ItsPaymentsServiceentrypoint hascreateIntentandcancelIntent; its fetch handler is the webhook atPOST /stripe/webhook, moved over unchanged. It holdsSTRIPE_SECRET_KEYandSTRIPE_WEBHOOK_SECRETand settles orders throughOrdersService, as before.PAYMENTSservice binding.src/payments.tsis now just thePaymentsinterface, which the binding andFakePaymentsboth implement. Nostripeimport and no Stripe secret are left insrc/.workers/stripe-webhookis deleted.paymentsreplacesstripe-webhookinsystem.yaml; storefront-api's edge to Stripe becomes an edge to payments. In the checkout and order fulfilment flows the charge and the webhook steps are rerouted through payments, and there is a new flow for order expiry, which now crosses both Workers.paymentsconcept, and updates to the checkout, webhook, local development and testing notes.The Visual tab on Files changed shows the system and flow changes.
Rollout
storefront-paymentsand set its two secrets./stripe/webhookURL as a second endpoint in the Stripe dashboard, subscribed topayment_intent.succeededandpayment_intent.canceled.npx wrangler delete storefront-stripe-webhook.Both endpoints settling the same event is harmless:
markPaidandcancelonly move an order forward once.Testing
npm test(49 tests). The webhook tests run againstworkers/payments/src/webhook.ts, and the Stripe adapter tests againstworkers/payments/src/stripe.ts.kb checkandarch checkpass.