Stripe Webhook - AfterCreate Event Handler - Creating/updating a database entry

I’m trying to create or update a row in a database table from a Stripe Webhook data creation (checkout.session.completed) in the StripeEvents table.
I have tried using the AfterCreate Event Handler in cloud code, but the event is not invoked when the webhook entry is created in the StripeEvents table.
If I create an entry in the StripeEvents table using the REST Console the AfterCreate Event Handler is invoked and works fine. So I can see the listener does not get invoked when the webhook entry is created.

Is there a way around this? Or is there a better way to do this? I would just like to run logic (i.e create a database entry) when the Stripe Checkout is completed. Any help would be appreciated.

Thank you

Here is an example of the Event Handler logic:

Hi H S,

The reason your event handler is not called is Backendless does not allow the invocation of Cloud Code event handlers when the API is called by the Cloud Code itself. This is done to prevent circular dependencies.

There are two workarounds for this:

  1. Implement your own webhook handler - it is implemented as an API service.

or

  1. Create a timer (a background job) that runs every X minutes and checks if there are new entries in the StripeEvents table. When a new record is detected, run your logic.

Regards,
Mark

Hi Mark,

1. Implement your own webhook handler - it is implemented as an API service.

This would be my preferred solution. I was looking at how to implement this, but I don’t really understand how it works. I know how to create an API service using Codeless, but the Stripe Webhook endpoint implementation looks like it requires coding.

2. Create a timer (a background job) that runs every X minutes and checks if there are new entries in the StripeEvents table. When a new record is detected, run your logic.

What would be the best way to detect a new record without using the Event Handlers? Do I have to keep count of how many entries are in the table? Also, I assume running a timer constantly every few seconds would be very costly.

Thank you for your response, I really appreciate your assistance.

Hello H S!

  1. I recommend that you watch this video, I think it will help you solve this task.
  2. Yes, you can use count or add an additional column with status. It is hard to say how costly this will be in resources because it will depend on how often the timer runs and the logic it executes.

For me, too, the first option looks better.
If you choose the first option and after watching the video you still have questions, don’t hesitate to ask, we will be happy to help you solve them.

Regards,
Alexander

Hi Alexander,

I did actually implement the timer using an extra status column to check if new, which works fine.
I will have a look at the video you have suggested, as I would still prefer the other method.

I have been trying to build the webhook using node.js project following these:

https://backendless.com/docs/bl-js/bl_manual_setup.html

I got quite far with this, but then I started to encounter errors verifying the stripe-signature.
If it’s possible using codeless that would be great.

Thank you very much

I have managed to implement the Stripe Webhook using codeless. I used the custom code block with async. Code used from the stripe webhook endpoint builder.

Screenshot 2022-12-12 at 23.27.01

I am able to send a 200 response to stripe with no errors from stripe and everything works fine. But I noticed an error in the Backendless Real-Time Logging:

| SERVER_CODE | ERROR | [1920] (node:1920) UnhandledPromiseRejectionWarning: Error: Cannot find module 'stripe' Require stack: - /opt/backendless/repo/1d8b3893-9437-6ffd-ffd5-9c8207032b00/files/servercode/CODELESS/CustomStripe/PRODUCTION/services/CustomStripe/index.js - /usr/local/lib/node_modules/backendless-coderunner/lib/server-code/api/index.js - /usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/cloud-worker.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15) at Function.Module._load (internal/modules/cjs/loader.js:745:27) at Module.require (internal/modules/cjs/loader.js:961:19) at Module.require (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/cloud-worker.js:133:26) at require (internal/modules/cjs/helpers.js:92:18) at /opt/backendless/repo/1d8b3893-9437-6ffd-ffd5-9c8207032b00/files/servercode/CODELESS/CustomStripe/PRODUCTION/services/CustomStripe/index.js:63:21 at /opt/backendless/repo/1d8b3893-9437-6ffd-ffd5-9c8207032b00/files/servercode/CODELESS/CustomStripe/PRODUCTION/services/CustomStripe/index.js:114:7 at CustomStripe.webhook (/opt/backendless/repo/1d8b3893-9437-6ffd-ffd5-9c8207032b00/files/servercode/CODELESS/CustomStripe/PRODUCTION/services/CustomStripe/index.js:116:5) at ServiceWrapper.invokeMethod (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/model/service.js:81:29) at Domain. (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/tasks/invoke-service.js:78:31) (Use `node --trace-warnings ...` to show where the warning was created)
23:11:9.510 | SERVER_CODE | ERROR | [1920] (node:1920) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
23:11:9.510 | SERVER_CODE | ERROR | [1920] (node:1920) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This doesn’t affect the webhook, so is this an error I can ignore? Do I need to installed the stripe node module or something?

Thanks again.

The error tells me your custom code is not executing.

Yes it seems I need to install the stripe dependencies in order to verify the stripe signature.

Is it possible to install the stripe library for use in codeless service?

Hi @H_S

Please take a look at this guide
How to use NPM modules in Codeless logic in API Services, Event Handlers and Timers

Regards,
Viktor

Hi Viktor,

The NPM guide was very useful, thank you. I have managed to install, backendless, stripe, and express modules using npm.

I am using the custom code provided by stripe, but I had to take out a couple of lines as it was giving a backendless error regarding “response.send”.

The webhook is sending a 200 response to Stripe with NULL, with no errors from Backendless or Stripe. I am able to receive the request data from stripe and apply logic to it. I assume that the stripe verification is working as I’m not receiving any errors. Although, I believe I should be sending something in the body instead of just NULL.

Thank you

Hello @H_S

Is it now working as you expect?

Regards,
Inna