首页 > 解决方案 > 支付成功后如何使用 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-payments

解决方案


付款成功后,Stripe 会自动向客户发送电子邮件。您可以在条带仪表板中编辑此设置。但是,如果这不起作用,或者您想通过您的平台/应用程序发送电子邮件,那么有两种方法可以做到这一点。

  1. 当用户完成结帐会话并返回到应用程序时,您必须验证 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) */
    }
    
  2. 注册 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 */
         }
    
        }
    
     }
    

推荐阅读