Stripe Subscriptions module API explorer

/api/user/subscriptions/create-customer (POST)

await global.api.user.subscriptions.CreateCustomer.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 querystring accountid
invalid-accountid missing querystring accountid
invalid-account ineligible accessing account

NodeJS source (edit on github)

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

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

module.exports = {
  post: async (req) => {
    if (!req.query || !req.query.accountid) {
      throw new Error('invalid-accountid')
    }
    const account = await global.api.user.Account.get(req)
    if (!account) {
      throw new Error('invalid-account')
    }
    if (!req.body || !req.body.email || !req.body.email.length) {
      throw new Error('invalid-email')
    }
    if (!req.body.description || !req.body.description.length) {
      throw new Error('invalid-description')
    }
    const customerInfo = {
      email: req.body.email,
      description: req.body.description,
      metadata: {
        appid: req.appid,
        accountid: req.account.accountid
      }
    }
    const customer = await stripeCache.execute('customers', 'create', customerInfo, req.stripeKey)
    await stripeCache.update(customer)
    await subscriptions.StorageList.addMany({
      [`${req.appid}/customers`]: customer.id,
      [`${req.appid}/account/customers/${req.query.accountid}`]: customer.id
    })
    await subscriptions.Storage.write(`${req.appid}/map/accountid/customerid/${customer.id}`, req.query.accountid)
    return customer
  }
}

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/user/subscriptions/create-customer', () => {
  describe('exceptions', () => {
    describe('invalid-accountid', () => {
      it('missing querystring accountid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-customer')
        req.account = user.account
        req.session = user.session
        req.body = {
          email: user.profile.contactEmail,
          description: 'customer'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-accountid')
      })

      it('invalid querystring accountid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-customer?accountid=invalid')
        req.account = user.account
        req.session = user.session
        req.body = {
          email: user.profile.contactEmail,
          description: 'customer'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-accountid')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async () => {
        const user = await TestHelper.createUser()
        const user2 = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-customer?accountid=${user2.account.accountid}`)
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const user = await TestHelper.createUser()
      const req = TestHelper.createRequest(`/api/user/subscriptions/create-customer?accountid=${user.account.accountid}`)
      req.account = user.account
      req.session = user.session
      req.body = {
        email: user.profile.contactEmail,
        description: 'customer'
      }
      req.filename = __filename
      req.saveResponse = true
      const customer = await req.post()
      assert.strictEqual(customer.object, 'customer')
    })
  })
})