#ruul_error

1 messages ¡ Page 1 of 1 (latest)

rotund vaporBOT
wintry fieldBOT
#

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

rotund vaporBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1248219776355143784

📝 Have more to share? Add details, code, screenshots, videos, etc. below.

polar pilot
#
    try {
      return {
        event: this.stripe.webhooks.constructEvent(
          (req as any).rawBody, // even though we have defs, we get ts2339
          req.headers["stripe-signature"],
          env.STRIPE_WEBHOOKS_SECRET_KEY
        ) as Stripe.Event,
        error: null,
      };
    } catch (error) {
      this.loggerService.error(error.type);
      return {
        event: null,
        error,
      };
    }
  }``` ```import { express, IRouteType } from "@bluelibs/apollo-bundle";
import { ContainerInstance, EventManager } from "@bluelibs/core";
import { StripeEvent } from "../events";
import { PaymentService } from "../services";

export const webhooksRoute: IRouteType = {
  path: "/stripe/webhooks",
  type: "post",
  handler: async (
    container: ContainerInstance,
    req: express.Request,
    res: express.Response
  ) => {
    const paymentService = container.get(PaymentService);
    const eventManager = container.get(EventManager);

    const { event, error } = paymentService.constructEvent(req);

    if (error) {
      return res.status(400).send(`Error: ${error.toString()}`);
    }

    eventManager.emit(new StripeEvent({ event }));

    return res.json({
      received: true,
    });
  },
};``` ``` new ApolloBundle({
      port: env.ROOT_PORT,
      url: env.ROOT_URL,
      enableSubscriptions: true,
      useJSONMiddleware: false,
      middlewares: [
        cors({ origin: "*" }),
        ["/stripe/webhooks", stripeWebhookMiddleware],
        // session({
        //   cookie: {
        //     secure: "auto",
        //     maxAge: 60 * 60 * 1000, //1h,
        //   },
        //   store: MongoStore.create({
        //     mongoUrl: env.MONGO_URL_SESSIONS,
        //     crypto: {
        //       secret: "squirrel",
        //     },
        //     touchAfter: 24 * 3600, // 14 days,
        //   }),
        //   secret: env.SESSION_SECRET,
        //   saveUninitialized: false,
        //   resave: false,
        // }),
      ],
    }),``` ```const stripeWebhookMiddleware = bodyParser.raw({
  type: "*/*",
  verify: (req: any, _res, buf, _encoding) => {
    req.rawBody = buf;
  },
});```
#

I asked something similar on the same context yesterday

#
    try {
      return {
        event: this.stripe.webhooks.constructEvent(
          (req as any).rawBody, // even though we have defs, we get ts2339
          req.headers["stripe-signature"],
          env.STRIPE_WEBHOOKS_SECRET_KEY
        ) as Stripe.Event,
        error: null,
      };
    } catch (error) {
      this.loggerService.error(error.type);
      return {
        event: null,
        error,
      };
    }
  }```
#

(method) Stripe.Webhooks.constructEvent(payload: string | Buffer, header: string | string[] | Buffer, secret: string, tolerance?: number, cryptoProvider?: Stripe.CryptoProvider): Stripe.Event it displays this however it has a rejected promise ?

wraith wharf
#

is stripe's construct event really synchronous ?

yes, but there is a constructEventAsync you can optionally use

polar pilot
#

how would this happen

#

again when used on other deployments that uses local listener (cli) this never happened before

#

the prod one uses hosted endpoint one not a local listener

wraith wharf
#

why would what happen?

wraith wharf
#

it's different for stripe listen and for each endpoint.

polar pilot
#

Ik we updated it because client thought it was the endpoint id instead of the secret key at start

#

it sometimes works

#

and sometimes throws the error

#

that's one of the main concerns here

#

why would it even work if secret was incorrect

wraith wharf
#

maybe your rawBody is not correct? as a completele guess, maybe it's an encoding issue and you don't treat the body as UTF-8, so if events have some special characters from things like customer names or so on, those ones don't get handled properly

#

you'd need to deeply debug this and try to look at the ones that fail and find the commonalities. It's really hard for me to say much here

polar pilot
#

alright ty

#

however still

#

why would it throw a rejected promise error

wraith wharf
#

I think it's the Promise inside your own code that's wrapping the call(i.e the HTTP response handler is inside a Promise, when code in that Promise throws an exception(the Stripe call), that Promise is rejected).

polar pilot
#
    try {
      // Check if rawBody is present
      if (!(req as any).rawBody) {
        return {
          event: null,
          error: new Error("Raw body is missing or not properly parsed"),
        };
      }

      // Check if stripe-signature header is present
      const signature = req.headers["stripe-signature"];
      if (!signature) {
        return {
          event: null,
          error: new Error("Stripe signature header is missing"),
        };
      }

      // Check if the secret key is available
      if (!env.STRIPE_WEBHOOKS_SECRET_KEY) {
        return {
          event: null,
          error: new Error("Stripe webhook secret key is not configured"),
        };
      }

      const event = await this.stripe.webhooks.constructEventAsync(
        (req as any).rawBody,
        signature,
        env.STRIPE_WEBHOOKS_SECRET_KEY
      );

      return { event, error: null };
    } catch (error) {
      this.loggerService.error(error.type);
      return {
        event: null,
        error: new Error(`Stripe webhook construction error: ${error.message}`),
      };
    }
  }``` hope this will solve it for now
#

the thing is

#

on the current one

#

it has a try catch

#

& it doesn't throw the error to caller

#

but returns the error

#

so if it's sync it wouldn't be unhandled

rotund vaporBOT
polar pilot
#

quick question for evt_1PObSuJlGzvMDWmesMFyIl4S

#

I just noticed

#

it might be a utf issue as @wraith wharf said

#

invoice has ø in it

#

however from what I've seen so far after the first webhook event fails stripe sends an another one and it's 200

wraith wharf
#

maybe you have multiple instances of your code running and a load balancer or so on, so the same code is not running each time. Hard for us to say much here

polar pilot
#

ok

#

should I use utf-8 or utf-16

narrow night
#

I believe UTF-8