/api/user/subscriptions/active-price (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.ActivePrice.get(req)Returns object
{
"priceid": "price_1LEOZNHHqepMFuCXSmmMK7Q2",
"object": "price",
"stripeObject": {
"id": "price_1LEOZNHHqepMFuCXSmmMK7Q2",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656123477,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwH7i0rLkusjTx",
"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_LwH7i0rLkusjTx",
"active": true,
"appid": "tests_1656123476",
"createdAt": "2022-06-25T02:17:57.524Z",
"updatedAt": "2022-06-25T02:17:57.524Z"
}
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-price | ineligible querystring price is not active |
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 = {
auth: false,
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)
if (!price.stripeObject.active) {
throw new Error('invalid-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/user/subscriptions/active-price', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-priceid', () => {
it('missing querystring priceid', async () => {
const req = TestHelper.createRequest('/api/user/subscriptions/active-price')
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-priceid')
})
it('invalid querystring priceid', async () => {
const req = TestHelper.createRequest('/api/user/subscriptions/active-price?priceid=invalid')
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-priceid')
})
})
describe('invalid-price', () => {
it('ineligible querystring price is not active', async () => {
const administrator = await TestStripeAccounts.createOwnerWithInactivePrice()
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/active-price?priceid=${administrator.price.priceid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-price')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/active-price?priceid=${administrator.price.priceid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const price = await req.get()
assert.strictEqual(price.priceid, administrator.price.priceid)
})
})
})