I’m adding products to a cart and then submitting that cart to Stripe
This is all working.
When the payment is completed I would like to send an email.
I have a sendgrid account.
I have added sendgrid to strapi with
yarn add @strapi/provider-email-sendgrid
I have added my sendgrid app key to the .env file
In config/plugins I have
module.exports = ({ env }) => ({
email: {
config: {
provider: 'sendgrid',
providerOptions: {
apiKey: env('SENDGRID_API_KEY'),
},
settings: {
defaultFrom: 'email@email.co.uk',
defaultReplyTo: 'email@email.co.uk',
},
},
},
});
I have a orders content type
In src/api/order/controllers/order.ts I have
"use strict";
// @ts-ignore
const stripe = require("stripe")("sk_test_51H");
/**
* order controller
*/
const { createCoreController } = require("@strapi/strapi").factories;
module.exports = createCoreController("api::order.order");
module.exports = {
setUpStripe: async (ctx) => {
let total = 0
let validatedCart = []
let receiptCart = []
const { cart } = ctx.request.body
await Promise.all(cart.map(async product => {
try {
const validatedProduct = await strapi.db.query('api::product.product').findOne({
where: { id: product.id }
});
if (validatedProduct) {
validatedCart.push(validatedProduct);
receiptCart.push({
id: product.id
})
}
} catch (error) {
console.error('Error while querying the databases:', error);
}
}));
total = validatedCart.reduce((n, { price }) => n + price, 0) || 0;
try {
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: 'usd',
metadata: { cart: JSON.stringify(validatedCart.toString()) },
payment_method_types: ['card']
});
// Send a response back to the client
ctx.send({
message: 'Payment intent created successfully',
paymentIntent,
});
} catch (error) {
// Handle errors and send an appropriate response
ctx.send({
error: true,
message: 'Error in processing payment',
details: error.message,
});
}
},
};
in src > api > order > content-types > order > lifecycles.js
module.exports = {
lifecycles: {
async afterCreate(event) {
const { result } = event
try {
await strapi.plugins['email'].services.email.send({
to: 'email@email.co.uk',
from: 'email@email.co.uk',
subject: 'Thank you for your order',
text: `Thank you fot your font order ${result.name}`
})
} catch (err) {
console.log(err)
}
}
}
}
in the strapi admin > settings > configuration
the send test email works
The email sent lifecycles.js doesn’t work.
Does anyone have any ideas why this might not work or how I can debug this