#tungtn1099

1 messages ยท Page 1 of 1 (latest)

glossy vineBOT
vapid plume
#

Hi! Let me help you with this.

#

Could you please console.log(req.body)?

trim forum
#

I did try it in my controller class, it returns undefined

vapid plume
#

This means your request body is not provided to you. I don't know how your framework works, but you will need to check how to get the request body yourself.

trim forum
#

That traces back to one of my old error.
I got Webhook payload must be provided as a string or a Buffer (https://nodejs.org/api/buffer.html) instance representing the _raw_ request body.Payload was provided as a parsed JavaScript object instead.
Seems like the request is parsed before getting to .constructEvent
I tried Buffer.from(req.body), getRawBody(req) from raw-body package, and @UseBefore(express.raw()) from routing-controllers

vapid plume
#

Sorry, I think Discord had some issues, we're back now.

#

What framework are you using?

trim forum
#

I'm using nodejs with typescript

vapid plume
#

I mean, what web framework? Like express

trim forum
#

oh yeah express

vapid plume
#

Are you sure this condition is executed: req.originalUrl === '/api/premium/stripe-handler'?

vapid plume
#

I've opened the thread

#

@trim forum

trim forum
#

I'll delete the other message then ๐Ÿ˜…

#

Yeah its executed

glossy vineBOT
vapid plume
#

You might need to handle your webhook at the top of your file, so no middleware affects the payload.

trim forum
#

like this?

private initializeMiddlewares() {
    this.app.use((req: any, res: any, next: any) =>{
      if(req.originalUrl === '/api/premium/stripe-handler'){
        console.log(123);
        next();
      }
      else{
        express.json()(req, res, next);
        express.urlencoded({ extended: true })(req, res, next);
      }
    });
    this.app.use(morgan(config.get('log.format'), { stream }));
    this.app.use(hpp());
    this.app.use(compression());
    // this.app.use(express.json());
    // this.app.use(express.urlencoded({ extended: true }));
    this.app.use(cookieParser());
    this.app.use(express.static('public'));
    this.app.use(cors());
    this.app.use(helmet());
    this.app.use(
      session({
        secret: 'keyboard cat',
        resave: false,
        saveUninitialized: true,
        cookie: { secure: true },
      }),
    );
  }

because this one is in a different file from the controller

gusty timber
#

looks reasonable

trim forum
#

it still returns undefined when i console.log(req.body)

gusty timber
trim forum
#

alright i'll try

trim forum
gusty timber
#

did you update the STRIPE_WEBHOOK_SECRET in the .env file to be the correct one for your endpoint(it's printed whsec_xxx from where you are running stripe listen ... )?