如何在Stripe中使用’test_clock’时捕获客户的电子邮件和姓名?

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

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;
}

huangapple
  • 本文由 发表于 2023年5月13日 23:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243560.html
匿名

发表评论

匿名网友

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

确定