Stripe Subscriptions module API explorer

/api/user/subscriptions/create-tax-id (POST)

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

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

Returns object

{
  "taxid": "txi_1LEOdVHHqepMFuCXebzgEmkO",
  "object": "taxid",
  "accountid": "acct_1c6809d688ddeb4a",
  "customerid": "cus_LwHBWMHSpNH7Ja",
  "appid": "tests_1656123732",
  "stripeObject": {
    "id": "txi_1LEOdVHHqepMFuCXebzgEmkO",
    "object": "tax_id",
    "country": "DE",
    "created": 1656123733,
    "customer": "cus_LwHBWMHSpNH7Ja",
    "livemode": false,
    "type": "eu_vat",
    "value": "DE123456789",
    "verification": {
      "status": "pending",
      "verified_address": null,
      "verified_name": null
    }
  },
  "type": null,
  "value": null,
  "createdAt": "2022-06-25T02:22:13.386Z",
  "updatedAt": "2022-06-25T02:22:13.386Z"
}

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-customerid missing querystring customerid
invalid querystring customerid
invalid-type missing posted type
invalid posted type
invalid-value invalid posted value

NodeJS source (view on github)

const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
const validTypes = [
  'ae_trn',
  'au_abn',
  'au_arn',
  'bg_uic',
  'br_cnpj',
  'br_cpf',
  'ca_bn',
  'ca_gst_hst',
  'ca_pst_bc',
  'ca_pst_mb',
  'ca_pst_sk',
  'ca_qst',
  'ch_vat',
  'cl_tin',
  'es_cif',
  'eu_oss_vat',
  'eu_vat',
  'gb_vat',
  'ge_vat',
  'hk_br',
  'hu_tin',
  'id_npwp',
  'il_vat',
  'in_gst',
  'is_vat',
  'jp_cn',
  'jp_rn',
  'kr_brn',
  'li_uid',
  'mx_rfc',
  'my_frp',
  'my_itn',
  'my_sst',
  'no_vat',
  'nz_gst',
  'ru_inn',
  'ru_kpp',
  'sa_vat',
  'sg_gst',
  'sg_uen',
  'si_tin',
  'th_vat',
  'tw_vat',
  'ua_vat',
  'us_ein',
  'za_vat'
]

module.exports = {
  post: async (req) => {
    if (!req.query || !req.query.customerid) {
      throw new Error('invalid-customerid')
    }
    const customer = await global.api.user.subscriptions.Customer.get(req)
    if (!customer) {
      throw new Error('invalid-customerid')
    }
    if (!req.body || !req.body.type || !req.body.type.length || validTypes.indexOf(req.body.type) === -1) {
      throw new Error('invalid-type')
    }
    if (!req.body.value || !req.body.value.length) {
      throw new Error('invalid-value')
    }
    const taxidInfo = {
      type: req.body.type,
      value: req.body.value
    }
    let taxId
    try {
      taxId = await stripeCache.execute('customers', 'createTaxId', req.query.customerid, taxidInfo, req.stripeKey)
    } catch (error) {
      if (error.message === 'invalid-taxid') {
        throw new Error('invalid-value')
      }
      throw error
    }
    await subscriptions.Storage.TaxId.create({
      appid: req.appid || global.appid,
      taxid: taxId.id,
      customerid: req.query.customerid,
      accountid: req.account.accountid,
      stripeObject: taxId
    })
    req.query.taxid = taxId.id
    return global.api.user.subscriptions.TaxId.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-tax-id', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-customerid', () => {
      it('missing querystring customerid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-tax-id')
        req.account = user.account
        req.session = user.session
        req.body = {
          type: 'au_abn',
          value: 'abcdefg'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })

      it('invalid querystring customerid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-tax-id?customerid=invalid')
        req.account = user.account
        req.session = user.session
        req.body = {
          type: 'au_abn',
          value: 'abcdefg'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async () => {
        const user = await TestHelper.createUser()
        const user2 = await TestHelper.createUser()
        await TestHelper.createCustomer(user2, {
          email: user2.profile.contactEmail
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-tax-id?customerid=${user2.customer.customerid}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          type: 'au_abn',
          value: 'abcdefg'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })

    describe('invalid-type', () => {
      it('missing posted type', async () => {
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-tax-id?customerid=${user.customer.customerid}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          type: '',
          value: 'abcdefg'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-type')
      })

      it('invalid posted type', async () => {
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-tax-id?customerid=${user.customer.customerid}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          type: 'invalid',
          value: 'abcdefg'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-type')
      })
    })

    describe('invalid-value', () => {
      it('invalid posted value', async () => {
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-tax-id?customerid=${user.customer.customerid}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          type: 'au_abn',
          value: 'abcdefg'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-value')
      })
    })
  })

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