#Mao

1 messages ยท Page 1 of 1 (latest)

gray groveBOT
brave breach
#

Hi again!

tulip mural
#

Hi ! ๐Ÿ™‚

#

I did what you told me.
And I have an idea, I think my problem is due to my app.use(express.json()), which parses my data

brave breach
#

Can you share your full code, and the exact error message you get?

tulip mural
#

My route :


  app.route("/stripe/webhook")
    .post(CommandeController.webhook, express.raw({ type: 'application/json' }));
#

And the code :

const stripe = new Stripe(process.env.STRIPE_PK_KEY, {
        apiVersion: "2022-08-01"
    });

    const endpointSecret = "whsec_53a5c720f4f70edzhjzdhjzdhjdzhjzda33b53d0451f9172e4d925c9c42634fa8";

    const sig = String(req.headers['stripe-signature']);

    let event;
    console.log("=====================================");
    console.log(req);
    console.log("=====================================");
    console.log(sig);

    try {
        event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
#

And in req.body I have undefined

brave breach
#

I recommend editing your message above to remove the whsec_xxx

tulip mural
#

Why ?

#

It's not the real endpointSecret

brave breach
#

Not sure what you mean by "not the real endpointSecret".

tulip mural
#

I voluntarily changed the endpointSecret for security so no problem

brave breach
#

Got it!

#

And the console.log(req) and console.log(sig) is working in your code? only req.body is undefined?

tulip mural
#

console.log(sig) working, not problem.
console.log(req), too
Only the req.body

#

It's strange

open turtle
#

Hi ๐Ÿ‘‹ jumping in as my teammate needs to step away, can you share the contents of what you see when you log req? Maybe that'll give us some more insight into how that object is structured.

tulip mural
#

If I add express.json() it works I have a req.body

#

But it modifies the JSON of the req.body sent by Stripe

open turtle
#

Can you elaborate on how you're adding express.json(), I'm wondering if adding express.raw() in the same fashion would give you the raw body.

tulip mural
#
  app.route("/")
    .get(index);

  // !OTHERS
  app.route("/client/:clientId([0-9]+)")
    .put(guardJwtFront, FrontClientController.update)
    .delete(guardJwt, FrontClientController.destroy);

  app.route("/stripe/webhook")
    .post(CommandeController.webhook, express.raw({ type: 'application/json' }));
``` Express RAW
```js
  app.route('/admin/*').all(express.json());
``` Express JSON
#

Oooooh that workings !!

#

Now I have that :

=====================================
. dev: <Buffer 7b 0a 20 20 22 69 64 22 3a 20 22 65 76 74 5f 31 4c 69 65 62 36 41 5a 78 64 63 55 7a 32 68 58 50 62 65 48 56 53 4e 6f 22 2c 0a 20 20 22 6f 62 6a 65 63 ... 2407 more bytes>

And the next :

Unhandled event type charge.succeeded

Thank's you very much !!

open turtle
#

Wooh, glad to hear you were able to get that working!

tulip mural
#

Thank's !!! โค๏ธ

#

I have another question

#

In preproduction, should I be successful?

open turtle
#

That looks like the screen that shows the delivery status of webhook events. I think that's indicating your endpoint returned an error rather than a 2XX status.

tulip mural
#

Yep :

Webhook Error: Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing

whyyyyyy ๐Ÿ˜ข

open turtle
#

That's usually caused by one of two reasons. Either the wrong webhook signing secret is being used, or you're not getting the raw contents of the request body.

tulip mural
#

But in local, that working, it's the same code for the raw request body

open turtle
#

When you say local, were you using the Stripe CLI to forward the events to your endpoint?

tulip mural
#

Yep !

open turtle
#

The Stripe CLI generates its own webhook signing secret, which is different from the ones that your Webhook Endpoints will use. Each Webhook Endpoint that you create will generate its own unique signing secret that must be used to validate its events.

tulip mural
#

Oh alright !

#

So I will test in pre-production

open turtle
tulip mural
#

I will test in pre-production with the pre-production webhook key

#

It's working thank's !!!!

open turtle
#

Awesome!

#

I'm glad to hear that worked!

tulip mural
#

Thank's !