/api/administrator/subscriptions/price (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.Price.get(req)Returns object
{
"priceid": "price_1LEOMJHHqepMFuCX6LkORnSq",
"object": "price",
"stripeObject": {
"id": "price_1LEOMJHHqepMFuCX6LkORnSq",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656122667,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwGujmjEUvyXyd",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 1000,
"unit_amount_decimal": "1000"
},
"productid": "prod_LwGujmjEUvyXyd",
"active": true,
"appid": "tests_1656122667",
"createdAt": "2022-06-25T02:04:28.027Z",
"updatedAt": "2022-06-25T02:04:28.027Z"
}
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-priceid | missing querystring priceid |
invalid querystring priceid |
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.priceid) {
throw new Error('invalid-priceid')
}
let price = await dashboard.StorageCache.get(req.query.priceid)
if (!price) {
const priceInfo = await subscriptions.Storage.Price.findOne({
where: {
priceid: req.query.priceid,
appid: req.appid || global.appid
}
})
if (!priceInfo) {
throw new Error('invalid-priceid')
}
price = {}
for (const field of priceInfo._options.attributes) {
price[field] = priceInfo.get(field)
}
await dashboard.StorageCache.set(req.query.priceid, price)
}
return price
}
}
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')
describe('/api/administrator/subscriptions/price', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-priceid', () => {
it('missing querystring priceid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/price')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-priceid')
})
it('invalid querystring priceid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/price?priceid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-priceid')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/price?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const price = await req.get()
assert.strictEqual(price.priceid, administrator.price.priceid)
})
})
})