nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Multiple stripe webhooks

Open annakopp opened this issue 3 years ago • 8 comments

Is it possible to set this up to work with two different webhook endpoints? (I need one for connect accounts & one for direct accounts.)

annakopp avatar Jan 03 '22 20:01 annakopp

You can setup stripe with one webhook url and use mulitple @StripeWebhookHandler with different events

CarsonF avatar Jan 10 '22 20:01 CarsonF

@CarsonF That's what I was doing initially but then I needed to create a webhook that used stripe connect (stripe sends different events to direct vs connect webhooks).

I think I can create the 2nd webhook to hit the same endpoint, but the webhook secret will be different, so that won't work.

annakopp avatar Jan 10 '22 20:01 annakopp

Ahh my bad you're right.

I think you could configure the module twice with a different controller prefix. The decorator uses their discovery library which I believe is global, so both modules would find all handlers. Each webhook controller though would use its own signing secret verification first. Worth a shot.

CarsonF avatar Jan 10 '22 20:01 CarsonF

I'd definitely try out the suggestion provided by @CarsonF first and see if it works out for you. In a future version we might be able to provide support for multiple configurations with some kind of name value that allows you to correlate webhook handlers to only be linked to the specific signing secret that matters for them.

Are there any major differences between connect accounts and direct accounts other than the signing secret?

WonderPanda avatar Jan 13 '22 20:01 WonderPanda

Are there any major differences between connect accounts and direct accounts other than the signing secret?

I think that's the only difference as far as I understand.

annakopp avatar Jan 19 '22 17:01 annakopp

I tried multiple setups of two webhook endpoints - two separate modules, two configurations in one module. Unfotunately nothing worked. The issues I encounted during my testing where:

  • Basically, one of the configurations didn't expose endpoint at all. It kept returning 404 in Stripe dashboard.
  • The other endpoint was established but returned an error No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe

Once I removed any one of them and kept just one, it started to work perfectly.

Is there any other way to handle two webhook secrets? I'll have to switch to some custom endpoint otherwise :(

ofilipowicz avatar Mar 01 '22 19:03 ofilipowicz

As a last resort you could probably monkey patch it:

        StripePayloadService.prototype.tryHydratePayload = function (
          signature: string,
          payload: Buffer
        ) {
          const decodedPayload = JSON.parse(
            Buffer.isBuffer(payload) ? payload.toString('utf8') : payload
          );

          return this.stripeClient.webhooks.constructEvent(
            payload,
            signature,
            decodedPayload.account
              ? stripeWebhookConnectSecret
              : stripeWebhookDirectSecret
          );
        };

laserchicken avatar Apr 13 '22 10:04 laserchicken

This is really a need, Direct and Connect hooks should be processed in different endpoints. I use Direct for my business webhooks, subscription start, end, upgrade etc. Connect is for people who have connected their stripe accounts so we can get updates from their accounts.

image

I'm happy to collaborate on implementing that, if you can guide me over

nusu avatar Aug 18 '22 08:08 nusu

@underfisk can we have a release on #522 ?

nusu avatar Dec 05 '22 07:12 nusu

@underfisk can we have a release on #522 ?

I don't have permissions to do that, only @WonderPanda

underfisk avatar Dec 05 '22 13:12 underfisk