Stripe Integration
Revana Stripe webhook integration for an Express route
We assume at this point that your route file has the following configured already:
- an Express app
- a configured
stripeinstance - a valid
STRIPE_WEBHOOK_SECRET
Install the package
Before wiring the webhook route into your application, install the Revana JavaScript package using the package manager your project already uses.
npm install @revana/jspnpm add @revana/jsyarn add @revana/jsbun add @revana/jsRevana Setup
In your environment variables create a new variable called REVANA_API_KEY and make sure you pass the correct space api key which you would like the daa to feed into, this is explained over in the revana dashboard under the user dropdown the api managment
simply add the following code to your project where you have your stripe webhook endpoint
import { Revana } from "@revana/js";
const revana = new Revana({
apiKey: process.env.REVANA_API_KEY,
});Webhook Integration
To get started if you just copy this following snippet into your router, this wil fully handle the revana payment revover module for incoming stripe events. its important to note all we do is forward the events were interested in capturing and once done we pass the event straight back to you so you can implement eny webhook logic required on yourside for your application.
app.post(
"/webhooks/stripe",
express.raw({ type: "application/json", limit: "64kb" }),
revana.handleExpressStripeWebhook(
{ stripe, webhookSecret },
(stripeEvent, _request, response) => {
console.log(`Stripe event ${stripeEvent.type} (${stripeEvent.id})`);
return response.status(200);
},
),
);stripeEvent is the verified, already-parsed Stripe event returned by stripe.webhooks.constructEvent(...) inside Revana. That means
your route callback receives the safe event object directly instead of neading to re-read the raw body, extracting the signature, and
constructing the event itself like seen in he example below
Code you no longer need in the route
Without Revana, your Express route usually needs to do the verification work itself before it can access the parsed Stripe event.
app.post(
"/webhooks/stripe",
express.raw({ type: "application/json", limit: "64kb" }),
(request, response) => {
const signature = request.headers["stripe-signature"];
const cleanStripeEvent = stripe.webhooks.constructEvent(
request.body,
signature,
webhookSecret,
);
console.log(
`received Stripe event ${cleanStripeEvent.type} (${cleanStripeEvent.id})`,
);
},
);What Revana's doing behind the scenes
- verifies the Stripe signature using your
stripeinstance andwebhookSecret - parses the Stripe event for you
- forwards the verified payload to the Revana backend for processing and revenue recapture
- passes the verified event into your route callback as
stripeEventso you can perform your own actions on any given stripe event
What still matters in Express
- The webhook route must use
express.raw({ type: "application/json", limit: "64kb" }),. - The Stripe route should not be parsed with
express.json()before Revana runs. - Your callback is where you return the final HTTP response for the route. -simply copy the code above and everything will just work