Stripe Subscriptions module API explorer

/api/administrator/subscriptions/set-tax-rate-active (PATCH)

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

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

Returns object

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-taxrate invalid taxrate is already active
invalid-taxrateid missing querystring taxrateid
invalid querystring taxrateid

NodeJS source (view on github)

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

module.exports = {
  patch: async (req) => {
    if (!req.query || !req.query.taxrateid) {
      throw new Error('invalid-taxrateid')
    }
    const taxRate = await global.api.administrator.subscriptions.TaxRate.get(req)
    if (!taxRate) {
      throw new Error('invalid-taxrateid')
    }
    if (taxRate.stripeObject.active) {
      throw new Error('invalid-taxrate')
    }
    const taxRateInfo = {
      active: true
    }
    const taxRateNow = await stripeCache.execute('taxRates', 'update', req.query.taxrateid, taxRateInfo, req.stripeKey)
    await subscriptions.Storage.TaxRate.update({
      stripeObject: taxRateNow
    }, {
      where: {
        taxrateid: req.query.taxrateid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.StorageCache.remove(req.query.taxrateid)
    return global.api.administrator.subscriptions.TaxRate.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-tax-rate-active', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-taxrateid', () => {
      it('missing querystring taxrateid', async function () {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/set-tax-rate-active')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-taxrateid')
      })

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

    describe('invalid-taxrate', () => {
      it('invalid taxrate is already active', async function () {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createTaxRate(administrator, {
          active: true
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-tax-rate-active?taxrateid=${administrator.taxRate.taxrateid}`)
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-taxrate')
      })
    })
  })

  describe('returns', () => {
    it('object', async function () {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createTaxRate(administrator, {
        active: 'false'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-tax-rate-active?taxrateid=${administrator.taxRate.taxrateid}`)
      req.account = administrator.account
      req.session = administrator.session
      const taxRateNow = await req.patch()
      assert.strictEqual(taxRateNow.stripeObject.active, true)
    })
  })
})