简体   繁体   中英

retrieve status of subscription stripe node.js

I have this:

 const customers = await stripe.customers.list({
    email: 'contact@Inderatech.com',
  });

  var customerID = customers.data[0].id;


  const subscriptions = await stripe.subscriptions.list({
    customer: customerID
  });

console.log(subscriptions)
  console.log(subscriptions.data[0].status)

and what it is supposed to do is retrieve the customerid of the subscriber, then i try to go to the next function and retrieve the status of the subcsription based on the customerid. This isn't working. it says 2 things.

  1. it just outputs:

    { object: 'list', data: [], has_more: false, url: '/v1/subscriptions' }

  2. (node:30746) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'status' of undefined

which i know is false, because when i just log it like so:

console.log(subscriptions)

it returns over 30 lines of the subcsribers data, including the active status. like so:

{
      id: 'sub_id',
      object: 'subscription',
      application_fee_percent: null,
      automatic_tax: [Object],
      billing_cycle_anchor: 1617410065,
      billing_thresholds: null,
      cancel_at: null,
      cancel_at_period_end: false,
      canceled_at: null,
      collection_method: 'charge_automatically',
      created: 1617410065,
      current_period_end: 1635899665,
      current_period_start: 1633221265,
      customer: 'cus_JEQIfxhiRu7qJh',
      days_until_due: null,
      default_payment_method: 'pm_1IbxRUIPT89VeZtCxyEA8SMu',
      default_source: null,
      default_tax_rates: [],
      discount: null,
      ended_at: null,
      items: [Object],
      latest_invoice: 'in_1JgIfuIPT89VeZtC2jsKCHRp',
      livemode: false,
      metadata: {},
      next_pending_invoice_item_invoice: null,
      pause_collection: null,
      payment_settings: [Object],
      pending_invoice_item_interval: null,
      pending_setup_intent: null,
      pending_update: null,
      plan: [Object],
      quantity: 1,
      schedule: null,
      start_date: 1617410065,
      status: 'active',
      tax_percent: null,
      transfer_data: null,
      trial_end: null,
      trial_start: null
    },

how can i access the status, or any other paramaters from this?

EDIT to make it more clear, I will show this:

when i do this:

    const subscriptions = await stripe.subscriptions.list({
  });

  console.log(subscriptions)

it returns the whole big output (one showed above).

thne when i do this:

    const subscriptions = await stripe.subscriptions.list({
    customer: customerID
  });

  console.log(subscriptions)

it only shows this:

{ object: 'list', data: [], has_more: false, url: '/v1/subscriptions' }

and when i try to log the subscription status of the one that compares the customerid , i get it saying that status is not a part of the returned data.

因此,如果您发布的输出是从条带 API 获得的所有输出,那么访问状态字段的正确方法就是使用

console.log(subscriptions[0].status);

That error message lines up with the output you say you are getting. If the list call does not return any Subscriptions, subscriptions.data[0] will be undefined so there is no object to get a status of. Because your call without the customer filter properly returns subscriptions, the call with the filter should work as long as that customer you pass in has subscriptions that have not been cancelled ( subscriptions.list only returns uncanceled subscriptions by default[1]).

It sounds like this may be happening because of the specific customer ID you are filtering on. If you try one of the customer IDs that you see when you list subscriptions without a filter (like cus_JEQIfxhiRu7qJh ), does your code work? For the customer you listed in your first example, I would double check that customer's page in your dashboard to see if they have uncanceled subscriptions that should be returned by that list call.

[1] https://stripe.com/docs/api/subscriptions/list

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