payload icon indicating copy to clipboard operation
payload copied to clipboard

plugin-ecommerce: Stripe adapter hangs on Cloudflare Workers (workerd) — new Stripe() needs a fetch httpClient

Open cshyang opened this issue 5 months ago • 1 comments

Describe the Bug

The Stripe payment adapter instantiates the SDK without an httpClient:

// packages/plugin-ecommerce/src/payments/adapters/stripe/initiatePayment.ts
// packages/plugin-ecommerce/src/payments/adapters/stripe/confirmOrder.ts
const stripe = new Stripe(secretKey, {
  apiVersion: apiVersion || '...',
  appInfo: appInfo || { ... },
})

With no httpClient, the Stripe Node SDK falls back to its default Node https-module (socket-based) client. That client does not work on Cloudflare Workers (workerd), where all outbound requests must go through fetch. The result: initiatePayment (and confirmOrder) hang indefinitely — no error, no timeout, the request never resolves.

The insidious part: this is invisible in local development. next dev runs on Node, where the default client works fine, so the entire checkout passes local testing. It only fails on the real Workers runtime (opennextjs-cloudflare preview or production), where checkout silently hangs.

Root cause

The Stripe Node SDK requires httpClient: Stripe.createFetchHttpClient() to run on Workers/edge runtimes. The adapter neither sets it nor exposes a way for the consumer to set it (stripeAdapter accepts secretKey / publishableKey / webhookSecret, but no httpClient or Stripe config passthrough).

Fix (verified end-to-end on workerd)

Adding the fetch HTTP client to both new Stripe() calls resolves it completely:

const stripe = new Stripe(secretKey, {
  httpClient: Stripe.createFetchHttpClient(),
  apiVersion: apiVersion || '...',
  appInfo: appInfo || { ... },
})

With this change, the full money path runs on workerd: initiatePayment → PaymentIntent + transaction, confirmOrder → order created + stock decremented. Without it, initiatePayment hangs forever.

Two possible directions:

  1. Detect a non-Node runtime and default to createFetchHttpClient(), or
  2. Expose an httpClient (or general Stripe config) option on stripeAdapter so consumers can inject it. This would also let people avoid patching the package to deploy on Workers.

Ruled out

Bumping compatibility_date (2025-08-15 → 2025-11-01) with nodejs_compat enabled does not fix it — it is specifically the HTTP client, not a missing Node polyfill.

Environment

  • payload 3.82.1, @payloadcms/plugin-ecommerce 3.82.1, stripe ^18.5.0
  • Deploy: @opennextjs/cloudflare → Cloudflare Workers (workerd), D1 database
  • compatibility_flags: ["nodejs_compat"]
  • Reproduces under opennextjs-cloudflare preview and in production; does not reproduce under next dev (Node).

cshyang avatar Jul 04 '26 15:07 cshyang

The hang is real, but the missing httpClient is not the cause — the constructor is fine. stripe already declares a workerd export condition ([email protected] package.json: "workerd": { "import": "./esm/stripe.esm.worker.js" }), which selects the fetch-based client. What fails is the bundler: Next/Turbopack + OpenNext resolve stripe under the default condition, so the Node build is selected and the request never leaves the Worker.

Fixed with build config only — zero plugin changes. In next.config.ts, production build only:

turbopack: { resolveAlias: { stripe: './src/stubs/stripe-worker.ts' } }

where the stub re-exports node_modules/stripe/esm/stripe.esm.worker.js by deep relative path. That indirection is necessary, not stylistic: every key in the package's exports map is a condition, not a subpath, so stripe/esm/stripe.esm.worker.js is ERR_PACKAGE_PATH_NOT_EXPORTED, and aliasing stripe to itself is a no-op.

Measured on a built OpenNext Worker (wrangler dev, local D1), POST /api/payments/stripe/initiate with a CAD cart and a Stripe test key:

  • before (Node build selected): HTTP 000, curl exit 28 — it hangs. Capped at 45 s here; an earlier run of the same request was left to run and returned nothing after 150 s, past Stripe's own timeout.
  • after (workerd build selected): HTTP 200 in 0.82 s, returning a real PaymentIntent, with the matching transactions row written pending at 1999 CAD.

A bundle-level check on the built worker, if you want one that discriminates: the Node build's TLS cipher string DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5 (unique to net/NodeHttpClient.js) reads 4 occurrences without the alias and 0 with it, while createNodeHttpClient not implemented. stays at 4 in both, confirming stripe is bundled either way. Worth flagging that several obvious markers do not discriminate — createNodeHttpClient, return "fetch" and secureConnect all appear in both builds, the first two because they live in the shared abstract base class and the third because FetchHttpClient.js uses it too.

A second, separate defect surfaces once payments work: webhooks.ts uses the synchronous constructEvent, which throws SubtleCryptoProvider cannot be used in a synchronous context on workerd, so every webhook 400s while answering {"received": true}. One-line fix, PR: https://github.com/payloadcms/payload/pull/18125

LuigiNicaPRO avatar Sep 08 '26 04:09 LuigiNicaPRO