Stripe Subscriptions module API explorer

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

await global.api.administrator.subscriptions.CreatePlan.post(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-planid invalid posted planid is not alphanumeric_
posted planid is too long
invalid-planid-length posted planid is too short
invalid posted productid
invalid-productid missing posted productid
invalid-product ineligible posted product is not published
invalid posted amount
invalid-amount missing posted amount
invalid posted currency
invalid-currency missing posted currency
invalid posted interval
invalid-interval missing posted interval
invalid posted interval_count
invalid-interval-count missing posted interval_count
invalid-trial_period invalid posted trial_period
invalid-usage_type invalid posted usage_type

Receives

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

Field Value Required Type
published boolean optional POST
trial_period_days integer optional POST
usage_type licensed, metered optional POST

NodeJS source (edit on github)

If you see a problem with the source submit a pull request on Github.

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

module.exports = {
  post: async (req) => {
    if (!req.body || !req.body.planid) {
      throw new Error('invalid-planid')
    }
    if (!req.body.planid.match(/^[a-zA-Z0-9_ ]+$/)) {
      throw new Error('invalid-planid')
    }
    if (global.minimumPlanIDLength > req.body.planid.length ||
      global.maximumPlanIDLength < req.body.planid.length) {
      throw new Error('invalid-planid-length')
    }
    if (!req.body.productid || !req.body.productid.length) {
      throw new Error('invalid-productid')
    }
    if (!req.body.currency || req.body.currency.length !== 3) {
      throw new Error('invalid-currency')
    }
    req.query = req.query || {}
    req.query.productid = req.body.productid
    const product = await global.api.administrator.subscriptions.Product.get(req)
    if (!product) {
      throw new Error('invalid-productid')
    }
    if (!product.metadata.published || product.metadata.unpublished) {
      throw new Error('invalid-product')
    }
    if (req.body.usage_type && req.body.usage_type !== 'metered' && req.body.usage_type !== 'licensed') {
      throw new Error('invalid-usage_type')
    }
    if (!req.body.amount) {
      throw new Error('invalid-amount')
    }
    try {
      const amount = parseInt(req.body.amount, 10)
      if (req.body.amount !== amount.toString()) {
        throw new Error('invalid-amount')
      }
    } catch (s) {
      throw new Error('invalid-amount')
    }
    if (req.body.amount < 0) {
      throw new Error('invalid-amount')
    }
    if (!req.body.interval_count) {
      throw new Error('invalid-interval_count')
    }
    if (req.body.interval !== 'day' && req.body.interval !== 'week' && req.body.interval !== 'month' && req.body.interval !== 'year') {
      throw new Error('invalid-interval')
    }
    try {
      req.body.interval_count = parseInt(req.body.interval_count, 10)
      if (!req.body.interval_count) {
        throw new Error('invalid-interval_count')
      }
    } catch (s) {
      throw new Error('invalid-interval_count')
    }
    if (req.body.interval_count < 1) {
      throw new Error('invalid-interval_count')
    }
    if (req.body.trial_period_days) {
      try {
        const trialPeriodDays = parseInt(req.body.trial_period_days, 10)
        if (trialPeriodDays < 0 || trialPeriodDays > 90) {
          throw new Error('invalid-trial_period_days')
        }
        if (req.body.trial_period_days !== trialPeriodDays.toString()) {
          throw new Error('invalid-trial_period_days')
        }
      } catch (s) {
        throw new Error('invalid-trial_period_days')
      }
    }
    const planInfo = {
      id: req.body.planid,
      product: req.body.productid,
      currency: req.body.currency,
      amount: req.body.amount || 0,
      interval: req.body.interval,
      interval_count: req.body.interval_count || 0,
      trial_period_days: req.body.trial_period_days || 0,
      usage_type: req.body.usage_type || 'licensed',
      metadata: {
        appid: req.appid
      }
    }
    if (req.body.published) {
      planInfo.metadata.published = dashboard.Timestamp.now
    }
    const plan = await stripeCache.execute('plans', 'create', planInfo, req.stripeKey)
    const indexing = {
      [`${req.appid}/plans`]: plan.id
    }
    if (plan.metadata.published) {
      indexing[`${req.appid}/published/plans`] = plan.id
    }
    await subscriptions.StorageList.addMany(indexing)
    return plan
  }
}

Test source (edit on github)

Tests perform real HTTP requests against a running Dashboard server.

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

describe('/api/administrator/subscriptions/create-plan', () => {
  describe('exceptions', () => {
    describe('invalid-planid', () => {
      it('invalid posted planid is not alphanumeric_', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: '123123!@#!@#!',
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-planid')
      })
    })

    describe('invalid-planid-length', () => {
      it('posted planid is too short', async () => {
        global.minimumPlanIDLength = 100
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'this',
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-planid-length')
      })

      it('posted planid is too long', async () => {
        global.maximumPlanIDLength = 1
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'that',
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-planid-length')
      })
    })

    describe('invalid-productid', () => {
      it('missing posted productid', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: ''
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-productid')
      })

      it('invalid posted productid', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: 'invalid'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-productid')
      })
    })

    describe('invalid-product', () => {
      it('ineligible posted product is not published', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {})
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-product')
      })
    })

    describe('invalid-amount', () => {
      it('missing posted amount', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: 'invalid',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-amount')
      })
    })

    describe('invalid-currency', () => {
      it('missing posted currency', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: '',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-currency')
      })

      it('invalid posted currency', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'invalid',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-currency')
      })
    })

    describe('invalid-interval', () => {
      it('missing posted interval', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: '',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-interval')
      })

      it('invalid posted interval', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'invalid',
          interval_count: '1',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-interval')
      })
    })

    describe('invalid-interval-count', () => {
      it('missing posted interval_count', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-interval_count')
      })

      it('invalid posted interval_count', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: 'nvalid',
          trial_period_days: '0',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-interval_count')
      })
    })

    describe('invalid-trial_period', () => {
      it('invalid posted trial_period', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: 'invalid',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-trial_period_days')
      })
    })

    describe('invalid-usage_type', () => {
      it('invalid posted usage_type', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createProduct(administrator, {
          published: 'true'
        })
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
          currency: 'usd',
          amount: '1000',
          interval: 'month',
          interval_count: '1',
          trial_period_days: '7',
          usage_type: 'invalid',
          productid: administrator.product.id
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-usage_type')
      })
    })
  })

  describe('receives', () => {
    it('optional posted published (boolean)', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        published: 'true'
      })
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
        currency: 'usd',
        amount: '1000',
        interval: 'month',
        interval_count: '1',
        trial_period_days: '0',
        productid: administrator.product.id,
        published: 'true'
      }
      const plan = await req.post()
      assert.notStrictEqual(plan.metadata.published, undefined)
      assert.notStrictEqual(plan.metadata.published, null)
    })

    it('optional posted trial_period_days (integer)', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        published: 'true'
      })
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
        currency: 'usd',
        amount: '1000',
        interval: 'month',
        interval_count: '1',
        trial_period_days: '3',
        productid: administrator.product.id
      }
      const plan = await req.post()
      assert.strictEqual(plan.trial_period_days, 3)
    })

    it('optional posted usage_type (licensed|metered)', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        published: 'true'
      })
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
        currency: 'usd',
        amount: '1000',
        interval: 'month',
        interval_count: '1',
        trial_period_days: '3',
        usage_type: 'metered',
        productid: administrator.product.id
      }
      const plan = await req.post()
      assert.strictEqual(plan.usage_type, 'metered')
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        published: 'true'
      })
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        planid: 'plan' + new Date().getTime() + 'r' + Math.ceil(Math.random() * '1000'),
        currency: 'usd',
        amount: '1000',
        interval: 'month',
        interval_count: '1',
        trial_period_days: '0',
        productid: administrator.product.id
      }
      req.filename = __filename
      req.saveResponse = true
      const plan = await req.post()
      assert.strictEqual(plan.object, 'plan')
    })
  })

  describe('configuration', () => {
    it('environment MINIMUM_PLANID_LENGTH', async () => {
      global.minimumPlanIDLength = 100
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        published: 'true'
      })
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        planid: 'this',
        currency: 'usd',
        amount: '1000',
        interval: 'month',
        interval_count: '1',
        trial_period_days: '0',
        productid: administrator.product.id
      }
      let errorMessage
      try {
        await req.post()
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-planid-length')
    })

    it('environment MAXIMUM_PLANID_LENGTH', async () => {
      global.maximumPlanIDLength = 1
      const administrator = await TestHelper.createOwner()
      await TestHelper.createProduct(administrator, {
        published: 'true'
      })
      const req = TestHelper.createRequest('/api/administrator/subscriptions/create-plan')
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        planid: 'that',
        currency: 'usd',
        amount: '1000',
        interval: 'month',
        interval_count: '1',
        trial_period_days: '0',
        productid: administrator.product.id
      }
      let errorMessage
      try {
        await req.post()
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-planid-length')
    })
  })
})