/api/user/subscriptions/customer (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.Customer.get(req)Returns object
{
"customerid": "cus_LwHCVmszzcT9rn",
"object": "customer",
"accountid": "acct_10b9d4e408dbeb45",
"couponid": null,
"stripeObject": {
"id": "cus_LwHCVmszzcT9rn",
"object": "customer",
"address": null,
"balance": 0,
"created": 1656123767,
"currency": null,
"default_source": null,
"delinquent": false,
"description": "American Express",
"discount": null,
"email": "Charlene.Mueller65@hotmail.com",
"invoice_prefix": "214219B5",
"invoice_settings": {
"custom_fields": null,
"default_payment_method": "pm_1LEOe3HHqepMFuCXHQPlQmGJ",
"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_1656123767",
"createdAt": "2022-06-25T02:22:47.763Z",
"updatedAt": "2022-06-25T02:22:50.178Z"
}
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 |
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.customerid) {
throw new Error('invalid-customerid')
}
let customer = await dashboard.StorageCache.get(req.query.customerid)
if (!customer) {
const customerInfo = await subscriptions.Storage.Customer.findOne({
where: {
customerid: req.query.customerid,
appid: req.appid || global.appid
}
})
if (!customerInfo) {
throw new Error('invalid-customerid')
}
if (customerInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
customer = {}
for (const field of customerInfo._options.attributes) {
customer[field] = customerInfo.get(field)
}
await dashboard.StorageCache.set(req.query.customerid, customer)
}
return customer
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/subscriptions/customer', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
let cachedResponses
async function bundledData (retryNumber) {
if (retryNumber > 0) {
cachedResponses = {}
}
if (cachedResponses && cachedResponses.finished) {
return
}
cachedResponses = {}
await TestHelper.setupBefore()
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const user2 = await TestHelper.createUser()
// invalid account
const req = TestHelper.createRequest(`/api/user/subscriptions/customer?customerid=${user.customer.customerid}`)
req.account = user2.account
req.session = user2.session
try {
await req.get()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// response
const req2 = TestHelper.createRequest(`/api/user/subscriptions/customer?customerid=${user.customer.customerid}`)
req2.account = user.account
req2.session = user.session
req2.filename = __filename
req2.saveResponse = true
cachedResponses.returns = await req2.get()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/customer')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
it('invalid querystring customerid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/customer?customerid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidAccount
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const customer = cachedResponses.returns
assert.strictEqual(customer.object, 'customer')
})
})
})