简体   繁体   中英

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). 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. 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. 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';

     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 . Only in "live" mode , it's possible. *In "test" mode , sending custom email using webhook is still possible.

Actually, I couldn't find the information above on stripe documentation so I asked stripe support , then, they said so as I said above.

You don't necessarily need to pass the customer's email in payment_intent_data.receipt_email .

When using Checkout, you can automatically email your customers upon successful payments. 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 . 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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