英文:
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<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");
}
};
<!-- 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: "payment",
successUrl: `${location}?success=true&invoiceID={CHECKOUT_SESSION_ID}`,
cancelUrl: `${location}?success=false`,
submitType: "pay",
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="test@email.com"
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="customerId"
This will successfully trigger the event.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论