Stripe Subscriptions module API explorer

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

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

await global.api.user.subscriptions.CreateCustomer.post(req)

Returns object

{
  "customerid": "cus_LwH9p2g0EsOean",
  "object": "customer",
  "accountid": "acct_fdc7f5b086520e8d",
  "couponid": null,
  "stripeObject": {
    "id": "cus_LwH9p2g0EsOean",
    "object": "customer",
    "address": null,
    "balance": 0,
    "created": 1656123603,
    "currency": null,
    "default_source": null,
    "delinquent": false,
    "description": "Work VISA",
    "discount": null,
    "email": "Steve12@yahoo.com",
    "invoice_prefix": "4B6E9862",
    "invoice_settings": {
      "custom_fields": null,
      "default_payment_method": null,
      "footer": null,
      "rendering_options": null
    },
    "livemode": false,
    "metadata": {},
    "name": null,
    "next_invoice_sequence": 1,
    "phone": null,
    "preferred_locales": [],
    "shipping": null,
    "tax_exempt": "none",
    "test_clock": null
  },
  "appid": "tests_1656123603",
  "createdAt": "2022-06-25T02:20:03.871Z",
  "updatedAt": "2022-06-25T02:20:03.871Z"
}

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-account ineligible accessing account
invalid-accountid missing querystring accountid
invalid querystring accountid

NodeJS source (view 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
    }
    if (global.requireBillingProfileAddress && !global.stripeJS) {
      customerInfo.address = {}
      for (const field of ['line1', 'line2', 'city', 'state', 'postal_code', 'country']) {
        if (req.body[field] && req.body[field].length) {
          customerInfo.address[field] = req.body[`${field}`]
        }
      }
    }
    const customer = await stripeCache.execute('customers', 'create', customerInfo, req.stripeKey)
    await subscriptions.Storage.Customer.create({
      appid: req.appid || global.appid,
      customerid: customer.id,
      accountid: req.account.accountid,
      stripeObject: customer
    })
    req.query.customerid = customer.id
    return global.api.user.subscriptions.Customer.get(req)
  }
}

Test source (view on github)

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

describe('/api/user/subscriptions/create-customer', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  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: 'Work VISA'
        }
        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: 'Work VISA'
        }
        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: 'Work VISA'
      }
      req.filename = __filename
      req.saveResponse = true
      const customer = await req.post()
      assert.strictEqual(customer.object, 'customer')
    })
  })
})