如何使用Stripe CLI发送客户电子邮件?

huangapple go评论48阅读模式
英文:

How to send a customer email from Stripe CLI?

问题

I'm using Stripe Checkout. Once the user has paid, I need to save his payment infos in my database. To this purpose, I use Stripe's webhook.

Since I can't use webhook in test mode, I had to install Stripe CLI. The issue is that I can't send a customer email in my payload. Ergo, I can't save the user payment info in my database during development.

The cli command I use is: stripe trigger invoice.payment_succeeded

The server route called by the webhook looks like this:

const buy = async (req: Request, res: IResponse): Promise<IResponse> => {
  try {
    const signature = req.headers["stripe-signature"];
    const event = stripe.webhooks.constructEvent(
      req.rawBody,
      signature,
      process.env.STRIPE_WEBHOOK_SECRET
    );
    if (event.type === "payment_intent.succeeded") {
      const { id, amount_paid, customer_email } = event.data.object as Invoice;
      const serialNumber = await PaymentControler.generateUniqueSerialNumber();
      await PaymentControler.updateUserMembership(
        customer_email,
        id,
        amount_paid,
        serialNumber
      );
      return res.sendStatus(200);
    }
    res.status(500).send("Error processing payment");
  } catch (err) {
    return res.status(500).send("Error processing payment");
  }
};

Here is how the client sends the request to Stripe Checkout:

const checkoutOptions: RedirectToCheckoutOptions = {
  lineItems: [
    {
      price: process.env.STRIPE_APP_PRICE,
      quantity: 1,
    },
  ],
  mode: "payment",
  successUrl: `${location}?success=true&invoiceID={CHECKOUT_SESSION_ID}`,
  cancelUrl: `${location}?success=false`,
  submitType: "pay",
  customerEmail: user?.email,
};

How to send a customer email from Stripe CLI?

Also, could you please confirm to me that I'm correctly accessing the properties: id, amount_paid, and customer_email in the event.data.object (they don't exist right now, so I want to make sure it'll be alright in production)?

英文:

I'm using Stripe Checkout. Once the user has paid, I need to save his payment infos in my database. To this purpose, I use Stripe's webhook.

Since I can't use webhook in test mode, I had to install Stripe CLI. The issue is that I can't send a customer email in my payload. Ergo, I can't save the user payment info in my database during development.

The cli command I use is: stripe trigger invoice.payment_succeeded

The server route called by the webhook looks like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const buy = async (req: Request, res: IResponse): Promise&lt;IResponse&gt; =&gt; {
  try {
    const signature = req.headers[&quot;stripe-signature&quot;];
    const event = stripe.webhooks.constructEvent(
      req.rawBody,
      signature,
     process.env.STRIPE_WEBHOOK_SECRET
    );
    if (event.type === &quot;payment_intent.succeeded&quot;) {
      const { id, amount_paid, customer_email } = event.data.object as Invoice;
      const serialNumber = await PaymentControler.generateUniqueSerialNumber();
      await PaymentControler.updateUserMembership(
        customer_email,
        id,
        amount_paid,
        serialNumber
      );
      return res.sendStatus(200);
    }
    res.status(500).send(&quot;Error processing payment&quot;);
  } catch (err) {
    return res.status(500).send(&quot;Error processing payment&quot;);
  }
};

<!-- end snippet -->

Here is how the client sends the request to Stripe Checkout:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  const checkoutOptions: RedirectToCheckoutOptions = {
    lineItems: [
      {
        price: process.env.STRIPE_APP_PRICE,
        quantity: 1,
      },
    ],
    mode: &quot;payment&quot;,
    successUrl: `${location}?success=true&amp;invoiceID={CHECKOUT_SESSION_ID}`,
    cancelUrl: `${location}?success=false`,
    submitType: &quot;pay&quot;,
    customerEmail: user?.email,
  };

<!-- end snippet -->

How to send a customer email from Stripe CLI?

Also, could you please confirm to me that I'm correctly accessing the properties: id, amount_paid, and customer_email in the event.data.object (they don't exist right now, so I want to make sure it'll be alright in production)?

答案1

得分: 1

invoice.payment_succeeded事件与Checkout本身没有太多关系。您可能想要触发的事件是checkout.session.completed,并使用--add标志自定义客户电子邮件参数。

例如,类似这样的内容;
stripe trigger checkout.session.completed --add checkout_session:customer_email="test@email.com"

这样可以让您测试您的代码并确保其按预期工作。

英文:

The invoice.payment_succeeded event doesn't really have anything to do with Checkout as such. The event you'd likely want to trigger is checkout.session.completed and use --add flag to customize the customer email parameter

For example something like;
stripe trigger checkout.session.completed --add checkout_session:customer_email=&quot;test@email.com&quot;

That should allow you to test your code and make sure it is working as intended.

答案2

得分: 0

The flag won't take an email as a parameter. However, you can go into your stripe dashboard and see the customerId for the email you want to use. Copy that id and pass it into your flag like this:

stripe trigger checkout.session.completed --add checkout_session:customer="customerId"

This will successfully trigger the event.

英文:

The flag won't take an email as a parameter. However, you can go into your stripe dashboard and see the customerId for the email you want to use. Copy that id and pass it into your flag like this:

stripe trigger checkout.session.completed --add checkout_session:customer=&quot;customerId&quot;

This will successfully trigger the event.

huangapple
  • 本文由 发表于 2023年3月10日 00:25:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定