Help. It seems Stripe does not like body.text()

Hello. I created an HTTPS endpoint with the following function:

// This function is the endpoint's request handler.
exports = async function ({ query, headers, body }, response) {

    // Verify the event came from Stripe

    const stripe = require("stripe")(context.values.get("stripeKeyTEST"));
    const endpointSecret = context.values.get("{ENDPOINT-SECRET}");

    const payload = body.text();
    const signature = headers['Stripe-Signature'][0];

    let event;
    try {

        event = stripe.webhooks.constructEvent(
            payload,
            signature,
            endpointSecret
        );
        
        
    }
    catch (err) {

        console.error(err.message);
        throw err.message;
    }
   return "Hello, World!";
};

The function is based on the snippet in @clueless_dev’s reply:
Continuing the discussion from Stripe Webhooks and AWS-S3 function call issue

However, I got the error:

Cannot access member 'length' of undefined

The signature was successfully gotten, so, by process of elimination, I concluded the payload is causing the problem. What can I do to fix this?

Note: Currently, I’m using a REST API endpoint on AWS to handle the Stripe events, but I’d prefer to do that here on Realm instead.

Hmm, can you check whether your endpoint secret is undefined? I ran into this too and supplying a nonempty webhook secret was the fix.

This function looks good to me otherwise

1 Like