/api/user/subscriptions/tax-id (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.TaxId.get(req)Returns object
{
"taxid": "txi_1LEOoxHHqepMFuCXFbyWAGmb",
"object": "taxid",
"accountid": "acct_806baba54f2129ce",
"customerid": "cus_LwHN1g5wPcvN90",
"appid": "tests_1656124443",
"stripeObject": {
"id": "txi_1LEOoxHHqepMFuCXFbyWAGmb",
"object": "tax_id",
"country": "DE",
"created": 1656124443,
"customer": "cus_LwHN1g5wPcvN90",
"livemode": false,
"type": "eu_vat",
"value": "DE123456789",
"verification": {
"status": "pending",
"verified_address": null,
"verified_name": null
}
},
"type": null,
"value": null,
"createdAt": "2022-06-25T02:34:04.117Z",
"updatedAt": "2022-06-25T02:34:04.117Z"
}
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-taxid | missing querystring taxid |
invalid querystring taxid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.taxid) {
throw new Error('invalid-taxid')
}
let taxid = await dashboard.StorageCache.get(req.query.taxid)
if (!taxid) {
const taxidInfo = await subscriptions.Storage.TaxId.findOne({
where: {
taxid: req.query.taxid,
appid: req.appid || global.appid
}
})
if (!taxidInfo) {
throw new Error('invalid-taxid')
}
if (taxidInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
taxid = {}
for (const field of taxidInfo._options.attributes) {
taxid[field] = taxidInfo.get(field)
}
await dashboard.StorageCache.set(req.query.taxid, taxid)
}
return taxid
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/subscriptions/tax-id', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-taxid', () => {
it('missing querystring taxid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/tax-id')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-taxid')
})
it('invalid querystring taxid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/tax-id?taxid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-taxid')
})
})
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
})
await TestHelper.createTaxId(user2, user2.customer)
const req = TestHelper.createRequest(`/api/user/subscriptions/tax-id?taxid=${user2.taxid.taxid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail
})
await TestHelper.createTaxId(user, user.customer)
const req = TestHelper.createRequest(`/api/user/subscriptions/tax-id?taxid=${user.taxid.taxid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const taxid = await req.get()
assert.strictEqual(taxid.object, 'taxid')
})
})
})