Stripe Subscriptions module API explorer

/api/administrator/subscriptions/set-product-active (PATCH)

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

await global.api.administrator.subscriptions.SetProductActive.patch(req)

Returns object

{
  "productid": "prod_LwH0HKHh1q1e57",
  "object": "product",
  "stripeObject": {
    "id": "prod_LwH0HKHh1q1e57",
    "object": "product",
    "active": true,
    "attributes": [],
    "created": 1656123021,
    "default_price": null,
    "description": null,
    "images": [],
    "livemode": false,
    "metadata": {},
    "name": "product209",
    "package_dimensions": null,
    "shippable": null,
    "statement_descriptor": "product209 description",
    "tax_code": "txcd_41060003",
    "type": "service",
    "unit_label": "thing",
    "updated": 1656123021,
    "url": null
  },
  "active": true,
  "appid": "tests_1656123021",
  "createdAt": "2022-06-25T02:10:21.394Z",
  "updatedAt": "2022-06-25T02:10:22.055Z"
}

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

NodeJS source (view on github)

const dashboard = require('@layeredapps/dashboard')
const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.js')

module.exports = {
  patch: async (req) => {
    if (!req.query || !req.query.productid) {
      throw new Error('invalid-productid')
    }
    const product = await global.api.administrator.subscriptions.Product.get(req)
    if (!product) {
      throw new Error('invalid-productid')
    }
    if (product.stripeObject.active) {
      throw new Error('invalid-product')
    }
    const updateInfo = {
      active: true
    }
    const productNow = await stripeCache.execute('products', 'update', req.query.productid, updateInfo, req.stripeKey)
    await subscriptions.Storage.Product.update({
      stripeObject: productNow,
      active: productNow.active
    }, {
      where: {
        productid: req.query.productid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.StorageCache.remove(req.query.productid)
    return global.api.administrator.subscriptions.Product.get(req)
  }
}

Test source (view on github)

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

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

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

    describe('invalid-product', () => {
      it('invalid querystring product is already active', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-product-active?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch(req)
        } 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: 'false'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-product-active?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.filename = __filename
      req.saveResponse = true
      const product = await req.patch()
      assert.strictEqual(product.stripeObject.active, true)
    })
  })
})