英文:
How do I capture a customers email & name when using 'test_clock' in Stripe?
问题
I need to ensure that my webhook will capture a customer's name
and email address
when subscription payments are made.
I thought I'd try out the test_clock
feature offered by Stripe to simulate this.
The Advance time feature works perfectly because I can see that payments are deducted from the given card, however, I can't seem to be able to capture the customer's email address and name from the event
response that is returned.
Find below my code that handles the event.type
:
// Handle the event
switch (event.type) {
case 'customer.subscription.updated':
console.log(`Subscription status updated: ${event.data.object.status}`);
const subscription = event.data.object;
const customerName = subscription.customer.name;
const customerEmail = subscription.customer.email;
console.log('customer Name is: ' + customerName);
console.log('customer Email is: ' + customerEmail);
英文:
I need to ensure that my webhook will capture a customer's name
and email address
when subscription payments are made.
I thought I'd try out the test_clock
feature offered by Stripe to simulate this.
The Advance time feature works perfectly because I can see that payments are deducted from the given card, however, I can't seem to be able to capture the customer's email address and name from the event
response that is returned.
Find below my code that handles the event.type
:
// Handle the event
switch (event.type) {
case 'customer.subscription.updated':
console.log(`Subscription status updated: ${event.data.object.status}`);
const subscription = event.data.object;
const customerName = subscription.customer.name;
const customerEmail = subscription.customer.email;
console.log('customer Name is: ' + customerName);
console.log('customer Email is: ' + customerEmail);
The code above yields:
Subscription status updated: active
customer Name is: undefined
customer Email is: undefined
How do I capture the customer's name and email address?
答案1
得分: 1
In subscription object (event.data.object
), customer
contains the customer ID which is a string instead of an object.
You need to fetch the customer details separately using the customer ID.
// 处理事件
switch (event.type) {
case 'customer.subscription.updated':
console.log(`订阅状态已更新:${event.data.object.status}`);
const subscription = event.data.object;
const customerId = subscription.customer;
// 使用客户ID单独获取客户详情
const customer = await stripe.customers.retrieve(customerId);
const customerName = customer.name;
const customerEmail = customer.email;
console.log('客户姓名:' + customerName);
console.log('客户电子邮件:' + customerEmail);
break;
}
英文:
In subscription object (event.data.object
), customer
contains the customer ID which is a string instead of an object.
You need to fetch the customer details separately using the customer ID.
// Handle the event
switch (event.type) {
case 'customer.subscription.updated':
console.log(`Subscription status updated: ${event.data.object.status}`);
const subscription = event.data.object;
const customerId = subscription.customer;
// Fetch customer details using the customer ID
const customer = await stripe.customers.retrieve(customerId);
const customerName = customer.name;
const customerEmail = customer.email;
console.log('Customer Name is: ' + customerName);
console.log('Customer Email is: ' + customerEmail);
break;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论