Stripe Subscriptions module API explorer

/api/administrator/subscriptions/subscriptions (GET)

Account information like email addresses is generated with faker-js it is not real user information.

await global.api.administrator.subscriptions.Subscriptions.get(req)

Returns array

[
  {
    "subscriptionid": "sub_1LEOWRHHqepMFuCXnvmMOMRF",
    "object": "subscription",
    "stripeObject": {
      "id": "sub_1LEOWRHHqepMFuCXnvmMOMRF",
      "object": "subscription",
      "application": null,
      "application_fee_percent": null,
      "automatic_tax": {
        "enabled": false
      },
      "billing_cycle_anchor": 1656123294,
      "billing_thresholds": null,
      "cancel_at": null,
      "cancel_at_period_end": false,
      "canceled_at": null,
      "collection_method": "charge_automatically",
      "created": 1656123294,
      "current_period_end": 1658715294,
      "current_period_start": 1656123294,
      "customer": "cus_LwH4CJMGAiXpMy",
      "days_until_due": null,
      "default_payment_method": "pm_1LEOWNHHqepMFuCXPAcbPhWe",
      "default_source": null,
      "default_tax_rates": [],
      "description": null,
      "discount": null,
      "ended_at": null,
      "items": {
        "object": "list",
        "data": [
          {
            "id": "si_LwH4ztQrGf4lIU",
            "object": "subscription_item",
            "billing_thresholds": null,
            "created": 1656123295,
            "metadata": {},
            "plan": {
              "id": "price_1LEOW0HHqepMFuCX6UX1FxgQ",
              "object": "plan",
              "active": true,
              "aggregate_usage": null,
              "amount": 1000,
              "amount_decimal": "1000",
              "billing_scheme": "per_unit",
              "created": 1656123268,
              "currency": "usd",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "nickname": null,
              "product": "prod_LwH4dOSfkdcrD9",
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "price": {
              "id": "price_1LEOW0HHqepMFuCX6UX1FxgQ",
              "object": "price",
              "active": true,
              "billing_scheme": "per_unit",
              "created": 1656123268,
              "currency": "usd",
              "custom_unit_amount": null,
              "livemode": false,
              "lookup_key": null,
              "metadata": {},
              "nickname": null,
              "product": "prod_LwH4dOSfkdcrD9",
              "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": 1000,
              "unit_amount_decimal": "1000"
            },
            "quantity": 1,
            "subscription": "sub_1LEOWRHHqepMFuCXnvmMOMRF",
            "tax_rates": []
          }
        ],
        "has_more": false,
        "total_count": 1,
        "url": "/v1/subscription_items?subscription=sub_1LEOWRHHqepMFuCXnvmMOMRF"
      },
      "latest_invoice": "in_1LEOWRHHqepMFuCXByoNz65J",
      "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_1LEOW0HHqepMFuCX6UX1FxgQ",
        "object": "plan",
        "active": true,
        "aggregate_usage": null,
        "amount": 1000,
        "amount_decimal": "1000",
        "billing_scheme": "per_unit",
        "created": 1656123268,
        "currency": "usd",
        "interval": "month",
        "interval_count": 1,
        "livemode": false,
        "metadata": {},
        "nickname": null,
        "product": "prod_LwH4dOSfkdcrD9",
        "tiers_mode": null,
        "transform_usage": null,
        "trial_period_days": null,
        "usage_type": "licensed"
      },
      "quantity": 1,
      "schedule": null,
      "start_date": 1656123294,
      "status": "active",
      "test_clock": null,
      "transfer_data": null,
      "trial_end": null,
      "trial_start": null
    },
    "customerid": "cus_LwH4CJMGAiXpMy",
    "accountid": "acct_c6acd94c95ef14e7",
    "paymentmethodid": null,
    "productid": null,
    "priceids": [
      "price_1LEOW0HHqepMFuCX6UX1FxgQ"
    ],
    "couponid": null,
    "appid": "tests_1656123267",
    "createdAt": "2022-06-25T02:14:57.598Z",
    "updatedAt": "2022-06-25T02:14:59.074Z"
  },
  {
    "subscriptionid": "sub_1LEOWJHHqepMFuCXcN1MafPm",
    "object": "subscription",
    "stripeObject": {
      "id": "sub_1LEOWJHHqepMFuCXcN1MafPm",
      "object": "subscription",
      "application": null,
      "application_fee_percent": null,
      "automatic_tax": {
        "enabled": false
      },
      "billing_cycle_anchor": 1656123287,
      "billing_thresholds": null,
      "cancel_at": null,
      "cancel_at_period_end": false,
      "canceled_at": null,
      "collection_method": "charge_automatically",
      "created": 1656123287,
      "current_period_end": 1658715287,
      "current_period_start": 1656123287,
      "customer": "cus_LwH4AsFxUuWOwf",
      "days_until_due": null,
      "default_payment_method": "pm_1LEOWGHHqepMFuCXgCwGzIKZ",
      "default_source": null,
      "default_tax_rates": [],
      "description": null,
      "discount": null,
      "ended_at": null,
      "items": {
        "object": "list",
        "data": [
          {
            "id": "si_LwH4NLYeymDJ03",
            "object": "subscription_item",
            "billing_thresholds": null,
            "created": 1656123287,
            "metadata": {},
            "plan": {
              "id": "price_1LEOW0HHqepMFuCX6UX1FxgQ",
              "object": "plan",
              "active": true,
              "aggregate_usage": null,
              "amount": 1000,
              "amount_decimal": "1000",
              "billing_scheme": "per_unit",
              "created": 1656123268,
              "currency": "usd",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "nickname": null,
              "product": "prod_LwH4dOSfkdcrD9",
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "price": {
              "id": "price_1LEOW0HHqepMFuCX6UX1FxgQ",
              "object": "price",
              "active": true,
              "billing_scheme": "per_unit",
              "created": 1656123268,
              "currency": "usd",
              "custom_unit_amount": null,
              "livemode": false,
              "lookup_key": null,
              "metadata": {},
              "nickname": null,
              "product": "prod_LwH4dOSfkdcrD9",
              "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": 1000,
              "unit_amount_decimal": "1000"
            },
            "quantity": 1,
            "subscription": "sub_1LEOWJHHqepMFuCXcN1MafPm",
            "tax_rates": []
          }
        ],
        "has_more": false,
        "total_count": 1,
        "url": "/v1/subscription_items?subscription=sub_1LEOWJHHqepMFuCXcN1MafPm"
      },
      "latest_invoice": "in_1LEOWJHHqepMFuCXcC9QCCTj",
      "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_1LEOW0HHqepMFuCX6UX1FxgQ",
        "object": "plan",
        "active": true,
        "aggregate_usage": null,
        "amount": 1000,
        "amount_decimal": "1000",
        "billing_scheme": "per_unit",
        "created": 1656123268,
        "currency": "usd",
        "interval": "month",
        "interval_count": 1,
        "livemode": false,
        "metadata": {},
        "nickname": null,
        "product": "prod_LwH4dOSfkdcrD9",
        "tiers_mode": null,
        "transform_usage": null,
        "trial_period_days": null,
        "usage_type": "licensed"
      },
      "quantity": 1,
      "schedule": null,
      "start_date": 1656123287,
      "status": "active",
      "test_clock": null,
      "transfer_data": null,
      "trial_end": null,
      "trial_start": null
    },
    "customerid": "cus_LwH4AsFxUuWOwf",
    "accountid": "acct_f32c050e520c24de",
    "paymentmethodid": null,
    "productid": null,
    "priceids": [
      "price_1LEOW0HHqepMFuCX6UX1FxgQ"
    ],
    "couponid": null,
    "appid": "tests_1656123267",
    "createdAt": "2022-06-25T02:14:49.724Z",
    "updatedAt": "2022-06-25T02:14:50.870Z"
  },
  {
    "subscriptionid": "sub_1LEOWCHHqepMFuCXD4ODRXB6",
    "object": "subscription",
    "stripeObject": {
      "id": "sub_1LEOWCHHqepMFuCXD4ODRXB6",
      "object": "subscription",
      "application": null,
      "application_fee_percent": null,
      "automatic_tax": {
        "enabled": false
      },
      "billing_cycle_anchor": 1656123280,
      "billing_thresholds": null,
      "cancel_at": null,
      "cancel_at_period_end": false,
      "canceled_at": null,
      "collection_method": "charge_automatically",
      "created": 1656123280,
      "current_period_end": 1658715280,
      "current_period_start": 1656123280,
      "customer": "cus_LwH4C6510ZNTf2",
      "days_until_due": null,
      "default_payment_method": "pm_1LEOW9HHqepMFuCXjbsyIWhM",
      "default_source": null,
      "default_tax_rates": [],
      "description": null,
      "discount": null,
      "ended_at": null,
      "items": {
        "object": "list",
        "data": [
          {
            "id": "si_LwH4TIPDF2iUoz",
            "object": "subscription_item",
            "billing_thresholds": null,
            "created": 1656123280,
            "metadata": {},
            "plan": {
              "id": "price_1LEOW0HHqepMFuCX6UX1FxgQ",
              "object": "plan",
              "active": true,
              "aggregate_usage": null,
              "amount": 1000,
              "amount_decimal": "1000",
              "billing_scheme": "per_unit",
              "created": 1656123268,
              "currency": "usd",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "nickname": null,
              "product": "prod_LwH4dOSfkdcrD9",
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "price": {
              "id": "price_1LEOW0HHqepMFuCX6UX1FxgQ",
              "object": "price",
              "active": true,
              "billing_scheme": "per_unit",
              "created": 1656123268,
              "currency": "usd",
              "custom_unit_amount": null,
              "livemode": false,
              "lookup_key": null,
              "metadata": {},
              "nickname": null,
              "product": "prod_LwH4dOSfkdcrD9",
              "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": 1000,
              "unit_amount_decimal": "1000"
            },
            "quantity": 1,
            "subscription": "sub_1LEOWCHHqepMFuCXD4ODRXB6",
            "tax_rates": []
          }
        ],
        "has_more": false,
        "total_count": 1,
        "url": "/v1/subscription_items?subscription=sub_1LEOWCHHqepMFuCXD4ODRXB6"
      },
      "latest_invoice": "in_1LEOWCHHqepMFuCX83ns0ie4",
      "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_1LEOW0HHqepMFuCX6UX1FxgQ",
        "object": "plan",
        "active": true,
        "aggregate_usage": null,
        "amount": 1000,
        "amount_decimal": "1000",
        "billing_scheme": "per_unit",
        "created": 1656123268,
        "currency": "usd",
        "interval": "month",
        "interval_count": 1,
        "livemode": false,
        "metadata": {},
        "nickname": null,
        "product": "prod_LwH4dOSfkdcrD9",
        "tiers_mode": null,
        "transform_usage": null,
        "trial_period_days": null,
        "usage_type": "licensed"
      },
      "quantity": 1,
      "schedule": null,
      "start_date": 1656123280,
      "status": "active",
      "test_clock": null,
      "transfer_data": null,
      "trial_end": null,
      "trial_start": null
    },
    "customerid": "cus_LwH4C6510ZNTf2",
    "accountid": "acct_d51c3fd20884864b",
    "paymentmethodid": null,
    "productid": null,
    "priceids": [
      "price_1LEOW0HHqepMFuCX6UX1FxgQ"
    ],
    "couponid": null,
    "appid": "tests_1656123267",
    "createdAt": "2022-06-25T02:14:42.444Z",
    "updatedAt": "2022-06-25T02:14:44.040Z"
  }
]

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

NodeJS source (view on github)

const subscriptions = require('../../../../../index.js')

module.exports = {
  get: async (req) => {
    req.query = req.query || {}
    const where = {
      appid: req.appid || global.appid
    }
    if (req.query.customerid) {
      const customer = await global.api.administrator.subscriptions.Customer.get(req)
      if (!customer) {
        throw new Error('invalid-customer')
      }
      where.customerid = req.query.customerid
    } else if (req.query.accountid) {
      const account = await global.api.administrator.Account.get(req)
      if (!account) {
        throw new Error('invalid-account')
      }
      where.accountid = req.query.accountid
    } else if (req.query.couponid) {
      const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
      if (!coupon) {
        throw new Error('invalid-couponid')
      }
      where.couponid = req.query.couponid
    }
    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.administrator.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/administrator/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 TestStripeAccounts.createOwnerWithPrice()
    for (let i = 0, len = global.pageSize + 2; i < len; i++) {
      const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
      cachedSubscriptions.unshift(user.subscription.subscriptionid)
    }
    const req1 = TestHelper.createRequest('/api/administrator/subscriptions/subscriptions?offset=1')
    req1.account = administrator.account
    req1.session = administrator.session
    cachedResponses.offset = await req1.get()
    const req2 = TestHelper.createRequest('/api/administrator/subscriptions/subscriptions?limit=1')
    req2.account = administrator.account
    req2.session = administrator.session
    cachedResponses.limit = await req2.get()
    const req3 = TestHelper.createRequest('/api/administrator/subscriptions/subscriptions?all=true')
    req3.account = administrator.account
    req3.session = administrator.session
    cachedResponses.all = await req3.get()
    const req4 = TestHelper.createRequest('/api/administrator/subscriptions/subscriptions')
    req4.account = administrator.account
    req4.session = administrator.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('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 subscriptions = cachedResponses.returns
      assert.strictEqual(subscriptions.length, global.pageSize)
    })
  })

  describe('configuration', () => {
    it('environment PAGE_SIZE', async () => {
      const subscriptions = cachedResponses.pageSize
      assert.strictEqual(subscriptions.length, global.pageSize + 1)
    })
  })
})