#Kvts-webhook-error

1 messages · Page 1 of 1 (latest)

celest panther
#

Hey there. That error would imply to me that for whatever reason we cannot reach your webhook handler

#

So because we didn't receive a 2xx response initially, we'll reattempt delivery X number of times over a certain period

high prism
#

but im making 200 response

celest panther
#

Can you share the ID of the Event object? (evt_xxx)

#

The FUNCTION_INVOCATION_TIMEOUT error would imply that your code isn't even being ran/invoked

#

But that's not really a Stripe issue

high prism
#

but I can see my payment in payments section with correct order items

#

so it should ran

celest panther
#

Can you share the ID of the Event object? (evt_xxx)

#

You should probably check the function logs on Vercel, too

high prism
#

yes i have made a new order now

#

evt_1KEYRAKaeuXjQloxAwAr6v7F

#

this is the id

celest panther
#

The amount of time (in seconds) that a Serverless Function is allowed to process an HTTP request before it must respond. The maximum execution timeout is 5 seconds when deployed on a Personal Account (Hobby plan). For Teams, the execution timeout is 15 seconds (Pro plan) or 30 seconds (Enterprise plan).
I guess this is your issue: https://vercel.com/docs/concepts/limits/overview#serverless-function-execution-timeout

Vercel Documentation

A list of limits and limitations that apply on the Vercel platform.

#

Your fulfillOrder function probably isn't resolving within the 5 second limit

high prism
#

yes i writte at the beginning that this is vercel error

#

can i handle it?

celest panther
#

You probably just need to handle that synchronously and return the 2xx response immediately

high prism
#

before fullfill funcion?

celest panther
#

That's our recommended approach (we expect a 2xx response within a ~couple seconds)

#

Not before, no. Otherwise fulfillOrder will never be hit

#

Just don't use a promise chain

#

You just call fulfillOrder and then res.send

high prism
#

now it's just like that

celest panther
#

It's not though. You're still waiting on fulfillOrder to resolve before res.status

high prism
#

maybe i should put it at the beggining of fulfill function?

celest panther
#

Alternatively you could optimise that fulfillOrder function as its seemingly taking a while to resolve

high prism
#

but it's a simple firebase function

#

i don't know how to optimise that

celest panther
#

I guess you're just working within the confines of the Vercel limits

high prism
#

ok but my order still completes

celest panther
#

Did you check the Vercel logs?

high prism
#

Fullfilling Order!!!
SUCCESS: Order cs_test_a1k9VlFppKHg1jDr6iDu6usXu1GUipRHm76wHFczVActHpdxqVEjlKOddn has been added to DB!
2022-01-05T12:18:03.252Z d498a695-9aa4-4d03-adfc-1e7f53ac498f Task timed out after 10.01 seconds

#

this is the log

#

i could just simply stop that webhook retry option

#

that would solve my problem

#

but i do not know how

celest panther
#

You can't disable retries anyway

#

You need to reconfigure your webhook handler. I think the res.status(200) is never being hit in the promise chain

#

Probably because of the .then in your fulfillOrder function

high prism
#

what do you suggest apart of then() ?

celest panther
#

You just need to return from that function, without the .then

high prism
#

i put console.log() before res.status and it fired

#

so i think res.status hits

#

but i will try your way

#

didn't worked :/

celest panther
#

What didn't?

high prism
#

returning res.status from that function

celest panther
#

No, you just need to return that call to Firebase without the .then()

high prism
#

just like that?

#

and where to put res.status(200)?

celest panther
#

No, inside the fullfillOrder function. Remove the .then (with the console.log)

high prism
#

same result

#

removed then() from firebase function

celest panther
#

This isn't really a Stripe issue, so I'm somewhat limited in what support I can provide. But your webhook should instead look something like:

try {
  if (event.type === 'checkout.session.completed') {
    await fulfillOrder(session)  
  }

  res.status(200).send(`Received!`)
} catch (err) {
  res.status(400).send(`Error: ${err}`)
}
high prism
#

that doesnt help too..

#

:/

celest panther
#

Are you redeploying this function each time to Vercel? Have you tried using the Stripe CLI when testing webhooks?

high prism
#

yes i'm redeploying

#

vercel env has my signing sercret

celest panther
#

Can you paste the full code (not screenshots) of both the handler and the fullfillOrder function

high prism
#

import { buffer } from "micro";
import * as admin from "firebase-admin";

// Secure a connection to firebase

const serviceAccount = require("../../../permissions.json");
const app = !admin.apps.length
? admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
: admin.app();

// Stripe

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

const endpointSecurit = process.env.STRIPE_SIGNING_SECRET;

const fullfillOrder = async (session) => {
console.log("Fullfilling Order!!!");

return app
.firestore()
.collection("users")
.doc(session.metadata.email)
.collection("orders")
.doc(session.id)
.set({
amount: session.amount_total / 100,
amount_shipping: session.total_details.amount_shipping / 100,
images: JSON.parse(session.metadata.images),
timestamp: admin.firestore.FieldValue.serverTimestamp(),
email: session.metadata.email,
})
};

export default async (req, res) => {
if (req.method === "POST") {
const requestBuffer = await buffer(req);
const payload = requestBuffer.toString();
const sig = req.headers["stripe-signature"];

let event;

try {
  event = await stripe.webhooks.constructEvent(
    payload,
    sig,
    endpointSecurit
  );
} catch (e) {
  console.log("ERROR", e.message);
  return res.status(400).send({ message: "Webhook error: " + e.message });
}
try {
  if (event.type === "checkout.session.completed") {
    const session = event.data.object;
    await fullfillOrder(session);
  }

  res.status(200).send(`Received!`);
} catch (err) {
  res.status(400).send(`Error: ${err}`);
}

}
};

export const config = {
api: {
bodyParser: false,
externalResolver: true,
},
};

celest panther
#

And what error are you seeing in Vercel on the most recent deployment?

high prism
#

Fullfilling Order!!!
SUCCESS: Order cs_test_a1A6THXv6GsHM9idzgNKl3qm3H4qBJeYVYo1qF608Q23sWvHZxFibqJhaY has been added to DB!
2022-01-05T12:58:39.025Z 425ff1a8-715c-4140-a0b9-c32de351160f Task timed out after 10.01 seconds

#

always same error

celest panther
#

I don't see this log:
SUCCESS: Order cs_test_a1A6THXv6GsHM9idzgNKl3qm3H4qBJeYVYo1qF608Q23sWvHZxFibqJhaY has been added to DB!
in the code your shared above

#

You sure you're deploying and using the latest?

#

Also, that timestamp is ~15 minutes ago

high prism
#

sorry, I forgot to make one thing

#

now it works fine!

#

thank you very much!