/api/user/subscriptions/subscriptions (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.Subscriptions.get(req)Returns array
[
{
"subscriptionid": "sub_1LEOohHHqepMFuCXf6ilUkH0",
"object": "subscription",
"stripeObject": {
"id": "sub_1LEOohHHqepMFuCXf6ilUkH0",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": {
"enabled": false
},
"billing_cycle_anchor": 1656124427,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "charge_automatically",
"created": 1656124427,
"current_period_end": 1658716427,
"current_period_start": 1656124427,
"customer": "cus_LwHNrDM7419n35",
"days_until_due": null,
"default_payment_method": "pm_1LEOoGHHqepMFuCXeBnVk3RJ",
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_LwHNdJP4rCYmOP",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656124428,
"metadata": {},
"plan": {
"id": "price_1LEOogHHqepMFuCXu1WKU0Xa",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 3000,
"amount_decimal": "3000",
"billing_scheme": "per_unit",
"created": 1656124426,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOogHHqepMFuCXu1WKU0Xa",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656124426,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 3000,
"unit_amount_decimal": "3000"
},
"quantity": 1,
"subscription": "sub_1LEOohHHqepMFuCXf6ilUkH0",
"tax_rates": []
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_1LEOohHHqepMFuCXf6ilUkH0"
},
"latest_invoice": "in_1LEOohHHqepMFuCXX4mILXOP",
"livemode": false,
"metadata": {},
"next_pending_invoice_item_invoice": null,
"pause_collection": null,
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null,
"save_default_payment_method": "off"
},
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"plan": {
"id": "price_1LEOogHHqepMFuCXu1WKU0Xa",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 3000,
"amount_decimal": "3000",
"billing_scheme": "per_unit",
"created": 1656124426,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start_date": 1656124427,
"status": "active",
"test_clock": null,
"transfer_data": null,
"trial_end": null,
"trial_start": null
},
"customerid": "cus_LwHNrDM7419n35",
"accountid": "acct_26b131fc4371614b",
"paymentmethodid": null,
"productid": null,
"priceids": [
"price_1LEOogHHqepMFuCXu1WKU0Xa"
],
"couponid": null,
"appid": "tests_1656124399",
"createdAt": "2022-06-25T02:33:50.005Z",
"updatedAt": "2022-06-25T02:33:53.115Z"
},
{
"subscriptionid": "sub_1LEOobHHqepMFuCXfz8Jg3kl",
"object": "subscription",
"stripeObject": {
"id": "sub_1LEOobHHqepMFuCXfz8Jg3kl",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": {
"enabled": false
},
"billing_cycle_anchor": 1656124421,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "charge_automatically",
"created": 1656124421,
"current_period_end": 1658716421,
"current_period_start": 1656124421,
"customer": "cus_LwHNrDM7419n35",
"days_until_due": null,
"default_payment_method": "pm_1LEOoGHHqepMFuCXeBnVk3RJ",
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_LwHNfRcSLyQYN3",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656124421,
"metadata": {},
"plan": {
"id": "price_1LEOoaHHqepMFuCXlsxtq0XZ",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 3000,
"amount_decimal": "3000",
"billing_scheme": "per_unit",
"created": 1656124420,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOoaHHqepMFuCXlsxtq0XZ",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656124420,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 3000,
"unit_amount_decimal": "3000"
},
"quantity": 1,
"subscription": "sub_1LEOobHHqepMFuCXfz8Jg3kl",
"tax_rates": []
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_1LEOobHHqepMFuCXfz8Jg3kl"
},
"latest_invoice": "in_1LEOobHHqepMFuCXdXDjhrvj",
"livemode": false,
"metadata": {},
"next_pending_invoice_item_invoice": null,
"pause_collection": null,
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null,
"save_default_payment_method": "off"
},
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"plan": {
"id": "price_1LEOoaHHqepMFuCXlsxtq0XZ",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 3000,
"amount_decimal": "3000",
"billing_scheme": "per_unit",
"created": 1656124420,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start_date": 1656124421,
"status": "active",
"test_clock": null,
"transfer_data": null,
"trial_end": null,
"trial_start": null
},
"customerid": "cus_LwHNrDM7419n35",
"accountid": "acct_26b131fc4371614b",
"paymentmethodid": null,
"productid": null,
"priceids": [
"price_1LEOoaHHqepMFuCXlsxtq0XZ"
],
"couponid": null,
"appid": "tests_1656124399",
"createdAt": "2022-06-25T02:33:43.513Z",
"updatedAt": "2022-06-25T02:33:48.324Z"
},
{
"subscriptionid": "sub_1LEOoUHHqepMFuCXtiaQkcOP",
"object": "subscription",
"stripeObject": {
"id": "sub_1LEOoUHHqepMFuCXtiaQkcOP",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": {
"enabled": false
},
"billing_cycle_anchor": 1656124414,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "charge_automatically",
"created": 1656124414,
"current_period_end": 1658716414,
"current_period_start": 1656124414,
"customer": "cus_LwHNrDM7419n35",
"days_until_due": null,
"default_payment_method": "pm_1LEOoGHHqepMFuCXeBnVk3RJ",
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_LwHN9Ts397VP4C",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656124415,
"metadata": {},
"plan": {
"id": "price_1LEOoTHHqepMFuCXO9p53IuN",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 3000,
"amount_decimal": "3000",
"billing_scheme": "per_unit",
"created": 1656124413,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOoTHHqepMFuCXO9p53IuN",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656124413,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 3000,
"unit_amount_decimal": "3000"
},
"quantity": 1,
"subscription": "sub_1LEOoUHHqepMFuCXtiaQkcOP",
"tax_rates": []
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_1LEOoUHHqepMFuCXtiaQkcOP"
},
"latest_invoice": "in_1LEOoUHHqepMFuCXndzOl9pz",
"livemode": false,
"metadata": {},
"next_pending_invoice_item_invoice": null,
"pause_collection": null,
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null,
"save_default_payment_method": "off"
},
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"plan": {
"id": "price_1LEOoTHHqepMFuCXO9p53IuN",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 3000,
"amount_decimal": "3000",
"billing_scheme": "per_unit",
"created": 1656124413,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHM4IKhiMGv0e",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start_date": 1656124414,
"status": "active",
"test_clock": null,
"transfer_data": null,
"trial_end": null,
"trial_start": null
},
"customerid": "cus_LwHNrDM7419n35",
"accountid": "acct_26b131fc4371614b",
"paymentmethodid": null,
"productid": null,
"priceids": [
"price_1LEOoTHHqepMFuCXO9p53IuN"
],
"couponid": null,
"appid": "tests_1656124399",
"createdAt": "2022-06-25T02:33:37.128Z",
"updatedAt": "2022-06-25T02:33:40.896Z"
}
]
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
all | boolean | optional | URL |
limit | integer | optional | URL |
offset | integer | optional | URL |
Exceptions
These exceptions are thrown (NodeJS) or returned as JSON (HTTP) if you provide incorrect data or do not meet the requirements:
Exception | Circumstances |
---|---|
invalid-account | ineligible accessing account |
invalid-accountid | missing querystring accountid |
invalid querystring accountid |
NodeJS source (view on github)
const subscriptions = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.accountid) {
throw new Error('invalid-accountid')
}
const account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-account')
}
const where = {
appid: req.appid || global.appid
}
if (req.query.customerid) {
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-customerid')
}
where.customerid = req.query.customerid
} else {
where.accountid = req.query.accountid
}
let subscriptionids
if (req.query.all) {
subscriptionids = await subscriptions.Storage.Subscription.findAll({
where,
attributes: ['subscriptionid'],
order: [
['createdAt', 'DESC']
]
})
} else {
const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0
const limit = req.query.limit ? parseInt(req.query.limit, 10) : global.pageSize
subscriptionids = await subscriptions.Storage.Subscription.findAll({
where,
attributes: ['subscriptionid'],
offset,
limit,
order: [
['createdAt', 'DESC']
]
})
}
if (!subscriptionids || !subscriptionids.length) {
return null
}
const items = []
for (const subscriptionInfo of subscriptionids) {
req.query.subscriptionid = subscriptionInfo.dataValues.subscriptionid
const subscription = await global.api.user.subscriptions.Subscription.get(req)
items.push(subscription)
}
return items
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/subscriptions/subscriptions', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
let cachedResponses, cachedSubscriptions
async function bundledData (retryNumber) {
if (retryNumber > 0) {
cachedResponses = {}
}
if (cachedResponses && cachedResponses.finished) {
return
}
cachedResponses = {}
cachedSubscriptions = []
await TestHelper.setupBefore()
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
active: 'true'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
for (let i = 0, len = global.pageSize + 2; i < len; i++) {
await TestHelper.createPrice(administrator, {
productid: administrator.product.productid,
currency: 'usd',
unit_amount: 3000,
recurring_interval: 'month',
recurring_interval_count: '1',
recurring_usage_type: 'licensed',
tax_behavior: 'inclusive',
active: 'true'
})
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
cachedSubscriptions.unshift(user.subscription.subscriptionid)
}
const req1 = TestHelper.createRequest(`/api/user/subscriptions/subscriptions?accountid=${user.account.accountid}&offset=1`)
req1.account = user.account
req1.session = user.session
cachedResponses.offset = await req1.get()
const req2 = TestHelper.createRequest(`/api/user/subscriptions/subscriptions?accountid=${user.account.accountid}&limit=1`)
req2.account = user.account
req2.session = user.session
cachedResponses.limit = await req2.get()
const req3 = TestHelper.createRequest(`/api/user/subscriptions/subscriptions?accountid=${user.account.accountid}&all=true`)
req3.account = user.account
req3.session = user.session
cachedResponses.all = await req3.get()
const req4 = TestHelper.createRequest(`/api/user/subscriptions/subscriptions?accountid=${user.account.accountid}`)
req4.account = user.account
req4.session = user.session
req4.filename = __filename
req4.saveResponse = true
cachedResponses.returns = await req4.get()
global.pageSize = 3
cachedResponses.pageSize = await req4.get()
global.pageSize = 2
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/subscriptions')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/subscriptions?accountid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/subscriptions?accountid=${user.account.accountid}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('receives', () => {
it('optional querystring offset (integer)', async function () {
await bundledData(this.test.currentRetry())
const offset = 1
const subscriptionsNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(subscriptionsNow[i].subscriptionid, cachedSubscriptions[offset + i])
}
})
it('optional querystring limit (integer)', async function () {
await bundledData(this.test.currentRetry())
const limit = 1
const subscriptionsNow = cachedResponses.limit
assert.strictEqual(subscriptionsNow.length, limit)
})
it('optional querystring all (boolean)', async () => {
const subscriptionsNow = cachedResponses.all
assert.strictEqual(subscriptionsNow.length, cachedSubscriptions.length)
})
})
describe('returns', () => {
it('array', async () => {
const subscriptionsNow = cachedResponses.returns
assert.strictEqual(subscriptionsNow.length, global.pageSize)
})
})
describe('configuration', () => {
it('environment PAGE_SIZE', async () => {
const subscriptionsNow = cachedResponses.pageSize
assert.strictEqual(subscriptionsNow.length, global.pageSize + 1)
})
})
})