Stripe Subscriptions module API explorer

/api/administrator/subscriptions/create-product (POST)

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

await global.api.administrator.subscriptions.CreateProduct.post(req)

Returns object

{
  "productid": "prod_LwGpBJCRJRwdpI",
  "object": "product",
  "stripeObject": {
    "id": "prod_LwGpBJCRJRwdpI",
    "object": "product",
    "active": true,
    "attributes": [],
    "created": 1656122388,
    "default_price": null,
    "description": null,
    "images": [],
    "livemode": false,
    "metadata": {},
    "name": "product name",
    "package_dimensions": null,
    "shippable": null,
    "statement_descriptor": "description",
    "tax_code": "txcd_41060003",
    "type": "service",
    "unit_label": "thing",
    "updated": 1656122389,
    "url": null
  },
  "active": true,
  "appid": "tests_1656122388",
  "createdAt": "2022-06-25T01:59:49.155Z",
  "updatedAt": "2022-06-25T01:59:49.155Z"
}

Receives

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

Field Value Required Type
active boolean optional POST
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 missing posted name
invalid-product-name-length posted name too short
posted name too long
invalid-statement_descriptor missing posted statement_descriptor
invalid-tax_code missing posted tax_code
invalid posted tax_code

NodeJS source (view on github)

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

module.exports = {
  post: async (req) => {
    if (!req.body || !req.body.name) {
      throw new Error('invalid-name')
    }
    if (global.minimumProductNameLength > req.body.name.length ||
      global.maximumProductNameLength < req.body.name.length) {
      throw new Error('invalid-product-name-length')
    }
    if (!req.body.statement_descriptor || !req.body.statement_descriptor.length) {
      throw new Error('invalid-statement_descriptor')
    }
    if (!req.body.unit_label || !req.body.unit_label.length) {
      throw new Error('invalid-unit_label')
    }
    if (!req.body.tax_code || !req.body.tax_code.length) {
      throw new Error('invalid-tax_code')
    }
    req.query = req.query || {}
    req.query.taxcodeid = req.body.tax_code
    try {
      await global.api.administrator.subscriptions.TaxCode.get(req)
    } catch (error) {
      throw new Error('invalid-tax_code')
    }
    const productInfo = {
      type: 'service',
      name: req.body.name,
      statement_descriptor: req.body.statement_descriptor,
      unit_label: req.body.unit_label,
      tax_code: req.body.tax_code
    }
    if (req.body.active) {
      productInfo.active = true
    }
    const product = await stripeCache.execute('products', 'create', productInfo, req.stripeKey)
    if (!product) {
      throw new Error()
    }
    await subscriptions.Storage.Product.create({
      appid: req.appid || global.appid,
      productid: product.id,
      stripeObject: product,
      active: product.active
    })
    req.query = req.query || {}
    req.query.productid = product.id
    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/create-product', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-name', () => {
      it('missing posted name', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
        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.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-name')
      })
    })

    describe('invalid-product-name-length', () => {
      it('posted name too short', async () => {
        global.minimumProductNameLength = 10
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'this',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-product-name-length')
      })

      it('posted name too long', async () => {
        global.maximumProductNameLength = 1
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-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.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-product-name-length')
      })
    })

    describe('invalid-statement_descriptor', () => {
      it('missing posted statement_descriptor', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'productName',
          statement_descriptor: '',
          unit_label: 'thing',
          tax_code: 'txcd_41060003'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-statement_descriptor')
      })
    })

    describe('invalid-tax_code', () => {
      it('missing posted tax_code', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'productName',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: ''
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-tax_code')
      })

      it('invalid posted tax_code', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'productName',
          statement_descriptor: 'description',
          unit_label: 'thing',
          tax_code: 'invalid'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-tax_code')
      })
    })
  })

  describe('receives', () => {
    it('optional posted active (boolean)', async () => {
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'product name',
        statement_descriptor: 'description',
        unit_label: 'thing',
        active: 'true',
        tax_code: 'txcd_41060003'
      }
      const product = await req.post()
      assert.strictEqual(product.stripeObject.active, true)
    })

    it('required posted name (string)', async () => {
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'product name',
        statement_descriptor: 'description',
        unit_label: 'thing',
        active: 'true',
        tax_code: 'txcd_41060003'
      }
      const product = await req.post()
      assert.strictEqual(product.stripeObject.name, 'product name')
    })

    it('required posted statement_descriptor (string)', async () => {
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'product name',
        statement_descriptor: 'description',
        unit_label: 'thing',
        active: 'true',
        tax_code: 'txcd_41060003'
      }
      const product = await req.post()
      assert.strictEqual(product.stripeObject.statement_descriptor, 'description')
    })

    it('required posted unit_label (string)', async () => {
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'product name',
        statement_descriptor: 'description',
        unit_label: 'thing',
        active: 'true',
        tax_code: 'txcd_41060003'
      }
      const product = await req.post()
      assert.strictEqual(product.stripeObject.unit_label, 'thing')
    })

    it('required posted tax_code (string)', async () => {
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'product name',
        statement_descriptor: 'description',
        unit_label: 'thing',
        active: 'true',
        tax_code: 'txcd_41060003'
      }
      const product = await req.post()
      assert.strictEqual(product.stripeObject.tax_code, 'txcd_41060003')
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'product name',
        statement_descriptor: 'description',
        unit_label: 'thing',
        tax_code: 'txcd_41060003'
      }
      req.filename = __filename
      req.saveResponse = true
      const product = await req.post()
      assert.strictEqual(product.object, 'product')
    })
  })

  describe('configuration', () => {
    it('environment MINIMUM_PRODUCT_NAME_LENGTH', async () => {
      global.minimumProductNameLength = 10
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'this',
        statement_descriptor: 'description',
        unit_label: 'thing',
        tax_code: 'txcd_41060003'
      }
      let errorMessage
      try {
        await req.post()
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-product-name-length')
    })

    it('environment MAXIMUM_PRODUCT_NAME_LENGTH', async () => {
      global.maximumProductNameLength = 1
      const administrator = await TestHelper.createOwner()
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-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.post()
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-product-name-length')
    })
  })
})