Home > Software design >  Stripe payload coming through empty [Object object] causing error codes in transaction
Stripe payload coming through empty [Object object] causing error codes in transaction

Time:09-15

I'm following along the stripe docs for fulfilling orders on my subscription app and can't get the stripe CLI to send verified 200 status events.

I backed up and believe it's because my payload is coming through totally empty as [Object object] because I can't figure out where to drop in the bodyParser. The stripe docs call for it as part of the route (below) but I've separated my routes and controller functions.

Here's how it looks in the stripe docs:

Stripe function

// Use body-parser to retrieve the raw body as a buffer
const bodyParser = require('body-parser');

app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
  const payload = request.body;

  console.log("Got payload: "   payload);

  response.status(200);
});

My code is separated into a router function and a controller function so here is how I've tried to recreate it in my app:

Router

const bodyParser = require('body-parser');
const JSONBodyParser = bodyParser.raw({type: 'application/json'});

router.route('/stripe/webhook')
    .post( JSONBodyParser, catchAsync( users.stripeWebhook))

Controller

module.exports.stripeWebhook = async (req, res) => {
    const payload = req.body;

    console.log("Got payload: "   payload);

    res.status(200);
};

The console.log(payload) renders as [Object object] but my understanding is there should be customer data attached to the payload. According to the stripe docs, I should see:

  • A checkout.session.completed in the stripe listen output
  • A print statement from your server’s event logs with the checkout.session.completed event

The connection is there because I've setup my stripe cli to listen for the route and log events in the terminal. Transaction events are coming through but are failing to post at the moment because I don't have switches setup in my controller function yet.. again, I've backed up a little bit to try to understand why I'm missing data in my payload.

App.js (added late)

 app.use(express.json({ limit: '1mb' }, {
     // We need the raw body to verify webhook signatures.
     // Let's compute it only when hitting the Stripe webhook endpoint.
     verify: function (req, res, buf) {
         if (req.originalUrl.startsWith("users/stripe/webhook")) {
             req.rawBody = buf.toString();
              }
          },
      })
 );

Any ideas how I could get the bodyParser to work so I can start debugging?

CodePudding user response:

Console log is just printing out [Object object] because that is how your object is being converted to a string using the .toString method. You can use util.inspect to see all of the properties on the object.

import util from 'util';
…
console.log('Got payload:'   util.inspect(payload));

Another option would be to use JSON.stringify to see what the json payload would look like.

CodePudding user response:

I think you are using your middleware incorrectly here:

const bodyParser = require('body-parser');
const JSONBodyParser = bodyParser.raw({type: 'application/json'});

Try this:

router.route('/stripe/webhook')
    .post( bodyParser.json(), catchAsync( users.stripeWebhook))

Alternatively you can use the middleware at a higher level (assuming express).

app.use(bodyParser.json())

Also worth noting that Express 4.16 has this built in and can be called like so:

app.use(express.json())

Here is a reference: https://medium.com/@mmajdanski/express-body-parser-and-why-may-not-need-it-335803cd048c

  • Related