Stripe Subscriptions module API explorer

/api/administrator/subscriptions/subscription (GET)

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

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

Returns object

{
  "subscriptionid": "sub_1LEOVHHHqepMFuCXDKecXoiF",
  "object": "subscription",
  "stripeObject": {
    "id": "sub_1LEOVHHHqepMFuCXDKecXoiF",
    "object": "subscription",
    "application": null,
    "application_fee_percent": null,
    "automatic_tax": {
      "enabled": false
    },
    "billing_cycle_anchor": 1656123223,
    "billing_thresholds": null,
    "cancel_at": null,
    "cancel_at_period_end": false,
    "canceled_at": null,
    "collection_method": "charge_automatically",
    "created": 1656123223,
    "current_period_end": 1658715223,
    "current_period_start": 1656123223,
    "customer": "cus_LwH3tHALnSHqSx",
    "days_until_due": null,
    "default_payment_method": "pm_1LEOVEHHqepMFuCXenQa7UDu",
    "default_source": null,
    "default_tax_rates": [],
    "description": null,
    "discount": null,
    "ended_at": null,
    "items": {
      "object": "list",
      "data": [
        {
          "id": "si_LwH3aTk58tSFPR",
          "object": "subscription_item",
          "billing_thresholds": null,
          "created": 1656123224,
          "metadata": {},
          "plan": {
            "id": "price_1LEOVDHHqepMFuCXGrqT7BXF",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 1000,
            "amount_decimal": "1000",
            "billing_scheme": "per_unit",
            "created": 1656123219,
            "currency": "usd",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {},
            "nickname": null,
            "product": "prod_LwH3Mvlc4SGboL",
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "price": {
            "id": "price_1LEOVDHHqepMFuCXGrqT7BXF",
            "object": "price",
            "active": true,
            "billing_scheme": "per_unit",
            "created": 1656123219,
            "currency": "usd",
            "custom_unit_amount": null,
            "livemode": false,
            "lookup_key": null,
            "metadata": {},
            "nickname": null,
            "product": "prod_LwH3Mvlc4SGboL",
            "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_1LEOVHHHqepMFuCXDKecXoiF",
          "tax_rates": []
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/subscription_items?subscription=sub_1LEOVHHHqepMFuCXDKecXoiF"
    },
    "latest_invoice": "in_1LEOVHHHqepMFuCX1b0TjRG7",
    "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_1LEOVDHHqepMFuCXGrqT7BXF",
      "object": "plan",
      "active": true,
      "aggregate_usage": null,
      "amount": 1000,
      "amount_decimal": "1000",
      "billing_scheme": "per_unit",
      "created": 1656123219,
      "currency": "usd",
      "interval": "month",
      "interval_count": 1,
      "livemode": false,
      "metadata": {},
      "nickname": null,
      "product": "prod_LwH3Mvlc4SGboL",
      "tiers_mode": null,
      "transform_usage": null,
      "trial_period_days": null,
      "usage_type": "licensed"
    },
    "quantity": 1,
    "schedule": null,
    "start_date": 1656123223,
    "status": "active",
    "test_clock": null,
    "transfer_data": null,
    "trial_end": null,
    "trial_start": null
  },
  "customerid": "cus_LwH3tHALnSHqSx",
  "accountid": "acct_6de29b544b28f5a2",
  "paymentmethodid": null,
  "productid": null,
  "priceids": [
    "price_1LEOVDHHqepMFuCXGrqT7BXF"
  ],
  "couponid": null,
  "appid": "tests_1656123219",
  "createdAt": "2022-06-25T02:13:45.871Z",
  "updatedAt": "2022-06-25T02:13:47.206Z"
}

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-subscriptionid missing querystring subscriptionid
invalid querystring subscriptionid

NodeJS source (view on github)

const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')

module.exports = {
  get: async (req) => {
    if (!req.query || !req.query.subscriptionid) {
      throw new Error('invalid-subscriptionid')
    }
    let subscription = await dashboard.StorageCache.get(req.query.subscriptionid)
    if (!subscription) {
      const subscriptionInfo = await subscriptions.Storage.Subscription.findOne({
        where: {
          subscriptionid: req.query.subscriptionid,
          appid: req.appid || global.appid
        }
      })
      if (!subscriptionInfo) {
        throw new Error('invalid-subscriptionid')
      }
      subscription = {}
      for (const field of subscriptionInfo._options.attributes) {
        subscription[field] = subscriptionInfo.get(field)
      }
      await dashboard.StorageCache.set(req.query.subscriptionid, subscription)
    }
    return subscription
  }
}

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')

describe('/api/administrator/subscriptions/subscription', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-subscriptionid', () => {
      it('missing querystring subscriptionid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/subscription')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-subscriptionid')
      })

      it('invalid querystring subscriptionid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/subscription?subscriptionid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-subscriptionid')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestStripeAccounts.createOwnerWithPrice()
      const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/subscription?subscriptionid=${user.subscription.subscriptionid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.filename = __filename
      req.saveResponse = true
      const subscriptionNow = await req.get()
      assert.strictEqual(subscriptionNow.subscriptionid, user.subscription.subscriptionid)
    })
  })
})