[英]retrieve status of subscription stripe node.js
我有這個:
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)
它應該做的是檢索訂閱者的customerid
,然后我嘗試轉到下一個函數並根據 customerid 檢索 subcsription 的status
。 這不起作用。 它說 2 件事。
它只是輸出:
{ 對象:'列表',數據:[],has_more:假,網址:'/v1/subscriptions'}
(node:30746) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'status' of undefined
我知道這是錯誤的,因為當我像這樣記錄它時:
console.log(subscriptions)
它返回 30 多行訂閱者數據,包括活動狀態。 像這樣:
{
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
},
我如何從中訪問狀態或任何其他參數?
編輯以使其更清楚,我將展示這一點:
當我這樣做時:
const subscriptions = await stripe.subscriptions.list({
});
console.log(subscriptions)
它返回整個大輸出(上面顯示了一個)。
當我這樣做時:
const subscriptions = await stripe.subscriptions.list({
customer: customerID
});
console.log(subscriptions)
它只顯示這個:
{ object: 'list', data: [], has_more: false, url: '/v1/subscriptions' }
當我嘗試記錄比較customerid
的訂閱狀態時,我得到它說狀態不是返回數據的一部分。
因此,如果您發布的輸出是從條帶 API 獲得的所有輸出,那么訪問狀態字段的正確方法就是使用
console.log(subscriptions[0].status);
該錯誤消息與您所說的輸出一致。 如果列表調用不返回任何訂閱,則subscriptions.data[0]
將是未定義的,因此沒有要獲取status
的對象。 因為您沒有使用customer
過濾器的調用正確返回訂閱,所以只要您傳入的客戶具有尚未取消的訂閱(默認情況下, subscriptions.list
僅返回未取消的subscriptions.list
[1]),帶有過濾器的調用應該可以工作。
聽起來這可能是因為您正在過濾的特定客戶 ID。 如果您嘗試在沒有過濾器的情況下列出訂閱時看到的客戶 ID 之一(例如cus_JEQIfxhiRu7qJh
),您的代碼是否有效? 對於您在第一個示例中列出的客戶,我會在您的儀表板中仔細檢查該客戶的頁面,以查看他們是否有應由該列表調用返回的未取消訂閱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.