Stripe Subscriptions module API explorer

/api/user/subscriptions/active-product (GET)

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

await global.api.user.subscriptions.ActiveProduct.get(req)

Returns object

{
  "productid": "prod_LwH7Eh3kv6SqDX",
  "object": "product",
  "stripeObject": {
    "id": "prod_LwH7Eh3kv6SqDX",
    "object": "product",
    "active": true,
    "attributes": [],
    "created": 1656123484,
    "default_price": null,
    "description": null,
    "images": [],
    "livemode": false,
    "metadata": {},
    "name": "product251",
    "package_dimensions": null,
    "shippable": null,
    "statement_descriptor": "product251 description",
    "tax_code": "txcd_41060003",
    "type": "service",
    "unit_label": "thing",
    "updated": 1656123484,
    "url": null
  },
  "active": true,
  "appid": "tests_1656123484",
  "createdAt": "2022-06-25T02:18:04.519Z",
  "updatedAt": "2022-06-25T02:18:04.519Z"
}

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-product ineligible querystring product is not active
invalid-productid missing querystring productid
invalid querystring productid

NodeJS source (view on github)

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

module.exports = {
  auth: false,
  get: async (req) => {
    if (!req.query || !req.query.productid) {
      throw new Error('invalid-productid')
    }
    let product = await dashboard.StorageCache.get(req.query.productid)
    if (!product) {
      const productInfo = await subscriptions.Storage.Product.findOne({
        where: {
          productid: req.query.productid,
          appid: req.appid || global.appid
        }
      })
      if (!productInfo) {
        throw new Error('invalid-productid')
      }
      product = {}
      for (const field of productInfo._options.attributes) {
        product[field] = productInfo.get(field)
      }
      await dashboard.StorageCache.set(req.query.productid, product)
      if (!product.stripeObject.active) {
        throw new Error('invalid-product')
      }
    }
    return product
  }
}

Test source (view on github)

/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')

describe('/api/user/subscriptions/active-product', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-productid', () => {
      it('missing querystring productid', async () => {
        const req = TestHelper.createRequest('/api/user/subscriptions/active-product')
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-productid')
      })

      it('invalid querystring productid', async () => {
        const req = TestHelper.createRequest('/api/user/subscriptions/active-product?productid=invalid')
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-productid')
      })
    })

    describe('invalid-product', () => {
      it('ineligible querystring product is not active', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'false'
        })
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/active-product?productid=${administrator.product.productid}`)
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-product')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      const user = await TestHelper.createUser()
      const req = TestHelper.createRequest(`/api/user/subscriptions/active-product?productid=${administrator.product.productid}`)
      req.account = user.account
      req.session = user.session
      req.filename = __filename
      req.saveResponse = true
      const product = await req.get()
      assert.strictEqual(product.productid, administrator.product.productid)
    })
  })
})