Wrangler with Stripe Error

Can not test my stripe webhook in localhost.

 [ERROR] Uncaught (in promise) TypeError: crypto2.createHmac is not a function

  .createHmac('sha256', secret)
   ^
      at computeHMACSignature
  (/Users/e****/node_modules/stripe/lib/crypto/NodeCryptoProvider.js:11:7)
      at verifyHeader
  (/Users/e****/node_modules/stripe/lib/Webhooks.js:88:45)
      at constructEvent
  (/Users/e****/node_modules/stripe/lib/Webhooks.js:10:19)
      at fetch (/Users/e****/src/index.ts:40:34)

I also updated my wrangler and added
workers_dev = true
node_compat = true

Still, issue continues.

Composer;

{
  "name": "xxx",
  "version": "0.0.0",
  "devDependencies": {
    "@cloudflare/workers-types": "^4.20221111.1",
    "typescript": "^4.9.4",
    "wrangler": "2.6.2"
  },
  "private": true,
  "scripts": {
    "start": "wrangler dev",
    "deploy": "wrangler publish"
  },
  "dependencies": {
    "stripe": "^11.5.0"
  }
}

You should try following this blog post

2 Likes

Thank you that was the solution.

const event = stripe.webhooks.constructEvent(body, sig, endpointSecret)

To

const webCrypto = Stripe.createSubtleCryptoProvider();
const event = await stripe.webhooks.constructEventAsync(body, sig, endpointSecret, undefined, webCrypto)