Stripe Subscriptions module API explorer

/api/administrator/subscriptions/update-product (PATCH)

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

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

Returns object

{
  "productid": "prod_LwH6sPnNLOaVNR",
  "object": "product",
  "stripeObject": {
    "id": "prod_LwH6sPnNLOaVNR",
    "object": "product",
    "active": true,
    "attributes": [],
    "created": 1656123395,
    "default_price": null,
    "description": null,
    "images": [],
    "livemode": false,
    "metadata": {},
    "name": "new-name",
    "package_dimensions": null,
    "shippable": null,
    "statement_descriptor": "new-description",
    "tax_code": "txcd_41060003",
    "type": "service",
    "unit_label": "new-thing",
    "updated": 1656123396,
    "url": null
  },
  "active": true,
  "appid": "tests_1656123395",
  "createdAt": "2022-06-25T02:16:35.954Z",
  "updatedAt": "2022-06-25T02:16:36.265Z"
}

Receives

API routes may receive parameters from the URL and POST supporting simple and multipart:

Field Value Required Type
name string required POST
statement_descriptor string required POST
tax_code string required POST
unit_label string required POST

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-name invalid posted name
invalid-name-length posted name too short
posted name too long
invalid-product ineligible querystring product is not active
invalid-productid missing querystring productid
invalid querystring productid
invalid-statement_descriptor invalid posted statement_descriptor
invalid-statement_descriptor-length posted statement_descriptor too short
posted statement_descriptor too long
invalid-tax_code missing posted tax_code
invalid posted tax_code
invalid-unit_label missing posted unit_label

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 = {}
    if (!req.body.name || !req.body.name.length) {
      throw new Error('invalid-name')
    }
    if (global.minimumProductNameLength > req.body.name.length ||
      global.maximumProductNameLength < req.body.name.length) {
      throw new Error('invalid-name-length')
    }
    updateInfo.name = req.body.name
    if (!req.body.statement_descriptor || !req.body.statement_descriptor.length) {
      throw new Error('invalid-statement_descriptor')
    }
    if (req.body.statement_descriptor.length < 5 ||
        req.body.statement_descriptor.length > 22) {
      throw new Error('invalid-statement_descriptor-length')
    }
    updateInfo.statement_descriptor = req.body.statement_descriptor
    if (!req.body.unit_label || !req.body.unit_label.length) {
      throw new Error('invalid-unit_label')
    }
    updateInfo.unit_label = req.body.unit_label
    if (!req.body.tax_code || !req.body.tax_code.length) {
      throw new Error('invalid-tax_code')
    }
    req.query.taxcodeid = req.body.tax_code
    try {
      await global.api.administrator.subscriptions.TaxCode.get(req)
    } catch (error) {
      throw new Error('invalid-tax_code')
    }
    updateInfo.tax_code = req.body.tax_code
    const productNow = await stripeCache.execute('products', 'update', req.query.productid, updateInfo, req.stripeKey)
    await subscriptions.Storage.Product.update({
      stripeObject: productNow
    }, {
      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/update-product', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-productid', () => {
      it('missing querystring productid', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/update-product')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        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()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/update-product?productid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } 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 req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-product')
      })
    })

    describe('invalid-name', () => {
      it('invalid posted name', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: '',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-name')
      })
    })

    describe('invalid-name-length', () => {
      it('posted name too short', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'short',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        global.minimumProductNameLength = 30
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-name-length')
      })

      it('posted name too long', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'looooooooooong',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        global.maximumProductNameLength = 3
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-name-length')
      })
    })

    describe('invalid-statement_descriptor', () => {
      it('invalid posted statement_descriptor', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: '',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-statement_descriptor')
      })
    })

    describe('invalid-statement_descriptor-length', () => {
      it('posted statement_descriptor too short', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'test',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-statement_descriptor-length')
      })

      it('posted statement_descriptor too long', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: '123456789 123456789 123456789',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-statement_descriptor-length')
      })
    })

    describe('invalid-unit_label', () => {
      it('missing posted unit_label', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'description',
          unit_label: '',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-unit_label')
      })
    })

    describe('invalid-tax_code', () => {
      it('missing posted tax_code', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: ''
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-tax_code')
      })

      it('invalid posted tax_code', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          active: 'true'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'that',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'invalid'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-tax_code')
      })
    })
  })

  describe('receives', () => {
    it('required posted name', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'testing',
        statement_descriptor: 'description',
        unit_label: 'thing',
        tax_code: 'txcd_41060003'
      }
      const productNow = await req.patch(req)
      assert.strictEqual(productNow.stripeObject.name, 'testing')
    })

    it('required posted statement_descriptor', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'that',
        statement_descriptor: 'descriptor',
        unit_label: 'thing',
        tax_code: 'txcd_41060003'
      }
      const productNow = await req.patch(req)
      assert.strictEqual(productNow.stripeObject.statement_descriptor, 'descriptor')
    })

    it('required posted unit_label', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'that',
        statement_descriptor: 'description',
        unit_label: 'new-thing',
        tax_code: 'txcd_41060003'
      }
      const productNow = await req.patch(req)
      assert.strictEqual(productNow.stripeObject.unit_label, 'new-thing')
    })

    it('required posted tax_code', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'that',
        statement_descriptor: 'description',
        unit_label: 'thing',
        tax_code: 'txcd_37060012'
      }
      const productNow = await req.patch(req)
      assert.strictEqual(productNow.stripeObject.tax_code, 'txcd_37060012')
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'new-name',
        statement_descriptor: 'new-description',
        unit_label: 'new-thing',
        tax_code: 'txcd_41060003'
      }
      req.filename = __filename
      req.saveResponse = true
      const product = await req.patch()
      assert.strictEqual(product.object, 'product')
    })
  })

  describe('configuration', () => {
    it('environment MINIMUM_PRODUCT_NAME_LENGTH', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      global.minimumProductNameLength = 100
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'new-name',
        statement_descriptor: 'new-description',
        unit_label: 'new-thitxcd_41060003ng',
        tax_code: 'txcd_41060003'
      }
      let errorMessage
      try {
        await req.patch(req)
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-name-length')
    })

    it('environment MAXIMUM_PRODUCT_NAME_LENGTH', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        active: 'true'
      })
      global.maximumProductNameLength = 1
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-product?productid=${administrator.product.productid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'new-name',
        statement_descriptor: 'new-description',
        unit_label: 'new-thing',
        tax_code: 'txcd_41060003'
      }
      let errorMessage
      try {
        await req.patch(req)
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-name-length')
    })
  })
})