#Bason91

1 messages ยท Page 1 of 1 (latest)

torn viperBOT
marsh scaffold
#

๐Ÿ‘‹ happy to help

polar wolf
#

Hello

marsh scaffold
#

would be with you shortly, in the meantime would mind moving your other message here to keep track of the whole conversation in one place?

polar wolf
#

Yep, just removed it.

#

application.post(
'/webhook/stripe', bodyParser.raw({}
),
async (request, response) => {
const stripe = new Stripe(Environment.BILLING_STRIPE_SECRET_KEY, null);
const event = (request as any).rawBody || request.body;
const sig = request.headers['stripe-signature'];
try {
const result = stripe.webhooks.constructEvent(
event,
sig,
'whsec_************' //I am using my developer stripe webhook secret here
);
console.log(result);
} catch (e) {
console.log(e.message);
}
await stripeEventsHandler.registerEvent(event);
response.status(200).json({ received: true });
});

#

I am using a local servet at local host:8080
Using the Stripe CLI for the events in the following way
stripe listen --forward-to localhost:8080/webhook/stripe

#

and then on an another terminal:
stripe trigger customer.updated

#

be back in a bit

white sparrow
#

Hey! Taking over for my colleague. Let me catch up.

polar wolf
#

Cool, I'll be back in around 15 minutes, getting some food ๐Ÿฅ“

white sparrow
#

The idea is to make sure that you are using the right values of those parameters. And to make sure that there is no middleware in your integration that modifies the body of the request. It must be send to the stripe.constructEvent as rawbody without any modification

polar wolf
#

What webhook secret should I use ?

#

the one from the Stripe CLI or the one from the test account?

white sparrow
polar wolf
#

Hello, just did it - I still get the same error.

cobalt orchid
#

Can you share an evt_xxx

polar wolf
#

Sure

#

evt_1M1q8MJWbpnZzq9uG1KQ2UKM

cobalt orchid
#

And the whsec_xxx is the one the CLI spits out when you run stripe listen?

polar wolf
#

yep

#

I triggered the event I shared from the stripe dashboard - i manually updated ac ustomer's subscription in test mode

cobalt orchid
#

I triggered the event I shared from the stripe dashboard
I thought you said you used stripe trigger?

#

Triggering an event from the Dashboard will use a different whsec_xxx

#

You need to use stripe trigger customer.subscription.updated and then separately stripe listen --forward-to and use the whsec_xxx the listen command prints

#

If you're triggering actions via the Dashboard, they will be signed with a different secret

polar wolf
#

that's what im doing

#

In this case I should use my whsec from the webhooks panel in the stripe dashobard, correct?

cobalt orchid
#

How are you actually triggering the events tough?

polar wolf
#

From the Stripe dashboard via the UI

#

I can also use Stripe CLi trigger

cobalt orchid
#

You need to use stripe trigger customer.subscription.updated as I explained:

Triggering an event from the Dashboard will use a different whsec_xxx
If you're triggering actions via the Dashboard, they will be signed with a different secret

#

If you're using the CLI it's best to just keep triggers isolated to there

polar wolf
#

If I trigger the event from the Dashboard - the whsec_xxx will be my signing secret code in the Webhook page in the Dashboard ?

cobalt orchid
#

Yes

polar wolf
#

if I trigger the event from the CLI - th whsec will be what the CLI spits out?

cobalt orchid
#

Yes

polar wolf
#

Currently, I am using the whsec_xxx from the Webhook page in the Dashboard and triggering the events from the dashboard while I have initliazed the Stripe CLI with
stripe listen --events customer.subscription.updated,customer.updated --forward-to localhost:8080/webhook/stripe

cobalt orchid
#

Sorry, to clarify. You need to use the whsec_xxx from stripe listen command

polar wolf
#

ok, give me a sec

#

and I should trigger the event from the stripe cli?

cobalt orchid
#

Ah, I just checked your code. Our recommendation is to use express.raw as opposed to bodyParser.raw:

app.post('/webhook/stripe', express.raw({type: 'application/json'}), (request, response) => {
polar wolf
#

ok, give me a sec

#

Are you ok with me pasting the payload that I receive ?

cobalt orchid
#

Sure, assuming there's no PII

polar wolf
#

request.body right now is of type Buffer {
"type": "Buffer",
"data": [
123,
10,
32,
32,
34,
105,
100,
34,
58,
32,
34,
101,
118,
116,
95,
49,
77,
49,
113,
104,
99,
74,
87,
98,
112,
110,
90,
122,
113,
57,
117,
55,
52,
109,
87,
70,
82,
112,
98,
34,
44,
10,
32,
32,
34,
111,
98,
106,
101,
99,
116,
34,
58,
32,
34,
101,
118,
101,
110,
116,
34,
44,
10,
32,
32,
34,
97,
112,
105,
95,
118,
101,
114,
115,
105,
111,
110,
34,
58,
32,
34,
50,
48,
49,
56,
45,
48,
57,
45,
50,
52,
34,
44,
10,
32,
32,
34,
99,
114,
101,
97,
116,
101,
100,
34,
58,
32,
49,
54,
54,
55,
57,
48,
57,
54,
57,
49,
44,
10,
32,
32,
34,
100,
97,
116,
97,
34,
58,
32,
123,
10,
32,
32,
32,
32,
34,
111,
98,
106,
101,

#

So we are not parsing it in any way and I get a new error

#

But that is not from the actual stripe.construct event

#

and the actual event is then reconstructed

#

so I guess that works

cobalt orchid
#

Can you share your full webhook handler code please

polar wolf
#

sure

#
const application: express.Express = express();
        application.use((req, res, next) => {
            if (req.originalUrl === '/webhook/stripe') {
                next();
            } else {
                express.json()(req, res, next);
            }
        });
 
application.post(
        '/webhook/stripe',
        express.raw({ type: 'application/json' }),
        async (request, response) => {
            const stripe = new Stripe(Environment.BILLING_STRIPE_SECRET_KEY, null);
            const event = request.body;
            const sig = request.headers['stripe-signature'];
            try {
                const result = stripe.webhooks.constructEvent(
                    event,
                    sig,
                    'whsec_6b5553314dbd17f6dcb1de18b4e603c2649f3069246a245e084c9d5e354c7fdc'
                );
                
            } catch (e) {
                console.log(e.message);
            }
        });
cobalt orchid
#

I'd redact that whsec_xxx

#

Anyway, you need to pass request.body to constructEvent, not your event variable (as that will be malformed - it requires the raw payload):

stripe.webhooks.constructEvent(request.body, sig, 'whsec_xxx')
polar wolf
#

However event = request.body - there is no parsing here.

cobalt orchid