#whiskeywizard-webhooks

1 messages · Page 1 of 1 (latest)

small current
#

hello! i'm afraid i don't have any experience with Cloudflare workers, but what's the issue?

karmic gulch
#

It seems like Cloudflare Workers has native support for Stripe's Node SDK, but I'm getting this error:

TypeError: crypto2.createHmac is not a function
at NodeCryptoProvider.computeHMACSignature (index.js:9582:24)

#

I guess this is more a question for the Cloudflare devs, but looking to see if anyone has done this as I'd expect Cloudflare Workers is a growing usecase for handling Stripe webhooks.

small current
#

which line is that error coming from?

karmic gulch
#

Hi @small current , this is the full error:

TypeError: crypto2.createHmac is not a function
at NodeCryptoProvider.computeHMACSignature (index.js:9582:24)
at Object.verifyHeader (index.js:9655:50)
at Object.constructEvent (index.js:9605:24)
at index.js:19564:35
at async Object.handle [as fetch] (index.js:15655:31) {
stack: TypeError: crypto2.createHmac is not a function
…sync Object.handle [as fetch] (index.js:15655:31),
message: crypto2.createHmac is not a function

#

I believe it's due to Cloudflare updating their "Wrangler" tool since their example. I'm getting help from one of their devs and if I figure it out I'll post here.

karmic gulch
#

FYI for anyone with same problem. Managed to fix it with this code in the Worker:

`// Stripe
import Stripe from 'stripe/lib/stripe.js';
export const webCrypto = Stripe.createSubtleCryptoProvider();
export function getStripe(env){

if(!env.STRIPE_KEY){
    throw new Error('Can not initialise Stripe without STRIPE_KEY');
}

const returnedStripe = Stripe(env.STRIPE_KEY, {
    // Ensure fetch is used
    httpClient: Stripe.createFetchHttpClient(),
    apiVersion: '2022-08-01',
    maxNetworkRetries: 2,
});

// Use web crypto
returnedStripe.CryptoProvider = webCrypto;
return returnedStripe;

}`

Then in the route (using itty-router here):

`// Stripe Webhooks
router.post('/api/stripe/webhooks', async (req, env) => {

try {

    const stripe = getStripe(env);
    const sig = req.headers.get('Stripe-Signature');
    const event = await stripe.webhooks.constructEventAsync(await req.text(), sig, env.STRIPE_WEBHOOK_SECRET, undefined, webCrypto);
    
    // Handle the event
    switch (event.type) {
             ...`