To send emails from Stripe when a payment intent is created, captured, and refunded, you can listen to Stripe webhooks for related events. Then, using the NestJS framework and an email service like Nodemailer, you can send the email notifications accordingly. Here's a simple outline of how to implement this:
- Configure and listen to Stripe webhooks
First, you need to configure your Stripe account to send webhook events to your NestJS app. In your NestJS app, create an endpoint to handle those events. Install
stripe npm package as a dependency to your project:
npm install stripe
Now create a webhook handler in NestJS:
// app.controller.ts
import { Controller, Post, Headers, Req } from '@nestjs/common';
import * as Stripe from 'stripe';
@Controller()
export class AppController {
@Post('webhooks/stripe')
async handleStripeWebhook(@Headers('stripe-signature') signature: string, @Req() request) {
const stripe = new Stripe('your_stripe_secret_key');
const endpointSecret = 'your_stripe_webhook_secret';
const event = stripe.webhooks.constructEvent(request.body, signature, endpointSecret);
switch (event.type) {
case 'payment_intent.created':
// Handle payment intent created
break;
case 'payment_intent.succeeded':
// Handle payment intent captured
break;
case 'charge.refunded':
// Handle payment refunded
break;
}
return { received: true };
}
}
- Send emails using NestJS and Nodemailer
You can use Nodemailer for sending emails in your NestJS application. First, install the required packages:
npm install --save nodemailer @nestjs-modules/mailer mailer-handlebars-adapter
Then, set up a MailerModule in your app.module.ts:
// app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from 'mailer-handlebars-adapter';
import { AppController } from './app.controller';
@Module({
imports: [
MailerModule.forRoot({
transport: {
host: 'your_email_host',
port: 'your_email_port',
secure: false,
auth: {
user: 'your_email',
pass: 'your_email_password',
},
},
defaults: {
from: '"your_app_name" <your_email>',
},
template: {
dir: './templates',
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
}),
],
controllers: [AppController],
})
export class AppModule {}
Next, create methods in your app.controller.ts to send the email notifications:
// app.controller.ts
// Import nodemailer, as well as the related decorators and types
import {
Controller,
Post,
Headers,
Req,
Inject,
} from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
import * as Stripe from 'stripe';
@Controller()
export class AppController {
constructor(
private mailerService: MailerService,
) {}
async sendEmailCreated(paymentIntent: Stripe.PaymentIntent) {
const email = "recipient@example.com";
await this.mailerService.sendMail({
to: email,
subject: 'Payment intent created',
template: 'payment-created', // Name of your email template
context: {
paymentAmount: paymentIntent.amount / 100,
currency: paymentIntent.currency.toUpperCase(),
},
});
}
async sendEmailCaptured(paymentIntent: Stripe.PaymentIntent) {
const email = "recipient@example.com";
await this.mailerService.sendMail({
to: email,
subject: 'Payment successful',
template: 'payment-captured', // Name of your email template
context: {
paymentAmount: paymentIntent.amount / 100,
currency: paymentIntent.currency.toUpperCase(),
bookingReference: "your_booking_reference",
},
});
}
async sendEmailRefunded(refund: Stripe.Refund) {
const email = "recipient@example.com";
await this.mailerService.sendMail({
to: email,
subject: 'Payment refunded',
template: 'payment-refunded', // Name of your email template
context: {
refundAmount: refund.amount / 100,
currency: refund.currency.toUpperCase(),
},
});
}
// Rest of your AppController code
}
Now, in the webhook handler for the payment_intent.created, payment_intent.succeeded, and charge.refunded events, call these respective email methods.
// app.controller.ts
// Inside the handleStripeWebhook method
switch (event.type) {
case 'payment_intent.created':
await this.sendEmailCreated(event.data.object as Stripe.PaymentIntent);
break;
case 'payment_intent.succeeded':
await this.sendEmailCaptured(event.data.object as Stripe.PaymentIntent);
break;
case 'charge.refunded':
await this.sendEmailRefunded(event.data.object as Stripe.Refund);
break;
}
- Passing the booking reference to the success page
To pass the booking reference to the success page after a successful checkout, you can add metadata to your payment intents in Stripe Checkout. Include the booking reference in the metadata when creating the payment intent:
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: 'usd',
metadata: { booking_reference: 'your_booking_reference' },
});
Then, in your application, ensure that your webhook is listening for the payment_intent.succeeded event. After a successful checkout, the webhook will receive the event, and you can extract the booking reference from the metadata:
// app.controller.ts
// Inside the handleStripeWebhook method
case 'payment_intent.succeeded':
const paymentIntent = event.data.object as Stripe.PaymentIntent;
const bookingReference = paymentIntent.metadata.booking_reference;
// Now you can pass this bookingReference to your success page or use it in the email sent
break;
With these steps, your NestJS app should be able to send email notifications for specific Stripe events and pass booking references to a success page.