简体   繁体   English

支付成功后如何使用 Stripe payment_intent_data 发送邮件给客户?

[英]How to use Stripe payment_intent_data to send email to client after succesful payment?

I cannot figure out how to send email to client after successful payment.付款成功后,我无法弄清楚如何向客户发送电子邮件。 Documentation says about setting "payment_intent_data.receipt_email" but what I have below in my code is not working (nothing arrives to emai-box).文档中提到了设置“payment_intent_data.receipt_email”,但我的代码下面的内容不起作用(没有任何东西到达 emai-box)。 How am I correctly to set this?我如何正确设置它?

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 automatically sends email to the client once the payment is successfully done.付款成功后,Stripe 会自动向客户发送电子邮件。 You can edit this settings in your stripe dashboard.您可以在条带仪表板中编辑此设置。 However, if this doesn't work or you want to send email through your platform/application then there are two ways possible ways to do this.但是,如果这不起作用,或者您想通过您的平台/应用程序发送电子邮件,那么有两种方法可以做到这一点。

  1. When the user completes the checkout session and returns back to the application, then you have to verify the CHECKOUT_SESSION_ID and retrieve the session using the CHECKOUT_SESSION_ID.当用户完成结帐会话并返回应用程序时,您必须验证 CHECKOUT_SESSION_ID 并使用 CHECKOUT_SESSION_ID 检索会话。 If it is correct then call your email function to send email to that client.如果正确,则调用您的电子邮件功能向该客户发送电子邮件。

     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) */ }
  2. Register the web hook endpoint and listen to the event import { buffer } from 'micro';注册 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 */ } } }

It seems like your account is "test" mode .您的帐户似乎是“测试”模式 In "test" mode , it's totally impossible to automatically send email from stripe after payment even if you set "receipt_email" parameter .“测试”模式下,即使设置了“receipt_email”参数,也完全不可能在付款后自动从条带发送电子邮件。 Only in "live" mode , it's possible.只有在“实时”模式下,才有可能。 *In "test" mode , sending custom email using webhook is still possible. *在“测试”模式下,仍然可以使用webhook发送自定义电子邮件。

Actually, I couldn't find the information above on stripe documentation so I asked stripe support , then, they said so as I said above.实际上,我在stripe 文档上找不到上面的信息,所以我问了stripe support ,然后,他们说就像我上面说的那样。

You don't necessarily need to pass the customer's email in payment_intent_data.receipt_email .您不一定需要在payment_intent_data.receipt_email中传递客户的电子邮件。

When using Checkout, you can automatically email your customers upon successful payments.使用 Checkout 时,您可以在成功付款后自动向客户发送电子邮件。 Enable this feature with the email customers for successful payments option in your email receipt settings .在您的电子邮件收据设置中使用电子邮件客户成功付款选项启用此功能。

However, the most important point to note is that receipts for payments created using your test API keys are not sent automatically .但是,需要注意的最重要的一点是,使用您的测试 API 密钥创建的付款收据不会自动发送 Instead, you can view or manually send a receipt using the Dashboard by finding the payment in the Dashboard and click "Send Receipt" under "Receipt history".相反,您可以通过在仪表板中查找付款并单击“收据历史记录”下的“发送收据”来使用仪表板查看或手动发送收据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM