Appearance
Webhook
Stripe integrations require that you implement a webhook to receive the many payment events they will send you. For example, when a payment intent succeeds Stripe will send an event object that contains the details of the event so that you can take action. You can read more about the types of events Stripe sends in their docs.
StarterStack includes a webhook route at POST /webhook
that is mapped to middleware located at /backend/stripe/StripeWebhook.js
. The middleware function is called eventHandler
. It receives the Stripe event and passes it off to an event processor if one exists. Otherwise it simply console.logs the event with no action taken.
Stripe Events Handled By StarterStack
To get you going with a minimal Stripe integration we include event processors for the following events:
Stripe Event | StarterStack Event Processor | Behavior |
---|---|---|
payment_intent.succeeded | paymentIntentSucceeded() | create user; send welcome email with reset password |
payment_intent.payment_failed | paymentIntentPaymentFailed() | for you to implement |
Adding an Event Processor
If there is a specific Stripe event you wish to handle in your application you simple need to add a new processor method in the /backend/stripe/StripeWebhook.js
file and map it in the eventHandler
method in the same file.
For example, adding a processor for the setup_intent.created
event:
Create your method to process the event
js
// /backend/stripe/StripeWebhook.js
const paymentSetupIntentCreated = (event) => {
const eventData = event.data.object;
console.log('setup intent created');
// add your custom logic here
}
Then locate the switch statement in the eventHandler
method and map your method to the event.
js
// /backend/stripe/StripeWebhook.js
switch (event.type) {
case 'payment_intent.succeeded':
paymentIntentSucceeded(event)
break;
case 'payment_intent.payment_failed':
paymentIntentPaymentFailed(event)
break;
// ... handle other event types
case 'setup_intent.created':
paymentSetupIntentCreated(event)
break;
default:
console.log(`Unhandled event type ${event.type}`);
}