stripe-payments - 支付成功后如何使用 Stripe payment_intent_data 发送邮件给客户?
问题描述
付款成功后,我无法弄清楚如何向客户发送电子邮件。文档中提到了设置“payment_intent_data.receipt_email”,但我的代码下面的内容不起作用(没有任何东西到达 emai-box)。我如何正确设置它?
app.post('/create-checkout-session', async (req, res) => {
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
**payment_intent_data: {receipt_email: "test@test.com"},**
shipping_rates: ['shr_1JgbJxAuqIBxGbL5lCkeLyfu'],
shipping_address_collection: {
allowed_countries: ['FI'],
},
line_items: [
{
price_data: {
currency: 'eur',
product_data: {
name: 'book',
},
unit_amount: 6200,
},
quantity: 2,
},
],
mode: 'payment',
success_url: 'http://localhost:4200/myynti/success',
cancel_url: 'http://localhost:4200/myynti/cancel',
});
res.json({ id: session.id });
解决方案
付款成功后,Stripe 会自动向客户发送电子邮件。您可以在条带仪表板中编辑此设置。但是,如果这不起作用,或者您想通过您的平台/应用程序发送电子邮件,那么有两种方法可以做到这一点。
当用户完成结帐会话并返回到应用程序时,您必须验证 CHECKOUT_SESSION_ID 并使用 CHECKOUT_SESSION_ID 检索会话。如果正确,则调用您的电子邮件功能向该客户发送电子邮件。
const verifyCheckoutSession =async(req, res) => { const sessionId = req.query.sessionId.toString(); if (!sessionId.startsWith('cs_')) { throw Error('Incorrect CheckoutSession ID.'); } /* retrieve the checkout session */ const checkout_session: Stripe.Checkout.Session = await stripe.checkout.sessions.retrieve( sessionId, { expand: ['payment_intent'], }); /* get the customer id */ const customerId = checkout_session.customer.toString(); /* get the customer through customerId */ const customer = await stripe.customers.retrieve(customerId.toString()); /* 1. get the customer email 2. You can also retrieve other details */ const email = customer.email; /* Now call your email function to send email to this particular user */ /* sendEmailTo(email) */ }
注册 web hook 端点并监听事件 import { buffer } from 'micro';
const __handleStripeEvent = async (req, res) => { if (req.method === 'POST') { /* web hooks only support post type */ const requestBuffer = await buffer(req); const sig = req.headers['stripe-signature'] as string; let event: Stripe.Event; try{ event = stripe.webhooks.constructEvent(requestBuffer.toString(), sig, webhookSecret); switch (event.type) { case 'checkout.session.completed': /* repeat the same process */ /* get checkout session id, then customer id, customer */ /* and send email */ break; } } catch(error){ /* do something on error */ } } }
推荐阅读
- python - Librosa 频谱图参数
- reactjs - 尽管设置正确,但将 firestore 连接到 redux 存储时出错
- laravel - 如何使用 Laravel 按时间过滤数据?
- python - fit_transform、transform 和 TfidfVectorizer 的工作原理
- javascript - 基于具有变换属性的另一个路径协调插入 svg 路径
- c# - 使用 C# ASP.NET 发送电子邮件延迟太多
- mysql - google cloud sql的气流连接错误
- r - 使用R中给定范围的偶数总和
- django - 服务器连接被拒绝 - Django/Postgresql
- java - MediatorLiveData 是否有可能等待两个 livedata 也收到更新?