[英]How to use Stripe payment_intent_data to send email to client after succesful payment?
付款成功后,我无法弄清楚如何向客户发送电子邮件。 文档中提到了设置“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 */ } } }
您的帐户似乎是“测试”模式。 在“测试”模式下,即使设置了“receipt_email”参数,也完全不可能在付款后自动从条带发送电子邮件。 只有在“实时”模式下,才有可能。 *在“测试”模式下,仍然可以使用webhook发送自定义电子邮件。
实际上,我在stripe 文档上找不到上面的信息,所以我问了stripe support ,然后,他们说就像我上面说的那样。
您不一定需要在payment_intent_data.receipt_email
中传递客户的电子邮件。
使用 Checkout 时,您可以在成功付款后自动向客户发送电子邮件。 在您的电子邮件收据设置中使用电子邮件客户成功付款选项启用此功能。
但是,需要注意的最重要的一点是,使用您的测试 API 密钥创建的付款收据不会自动发送。 相反,您可以通过在仪表板中查找付款并单击“收据历史记录”下的“发送收据”来使用仪表板查看或手动发送收据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.