/api/administrator/subscriptions/set-price-active (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.SetPriceActive.patch(req)Returns object
{
"priceid": "price_1LEORiHHqepMFuCXTLJYglJB",
"object": "price",
"stripeObject": {
"id": "price_1LEORiHHqepMFuCXTLJYglJB",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656123002,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwGzqxq4tjeh02",
"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_LwGzqxq4tjeh02",
"active": true,
"appid": "tests_1656123001",
"createdAt": "2022-06-25T02:10:02.721Z",
"updatedAt": "2022-06-25T02:10:03.549Z"
}
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 active |
invalid-priceid | missing querystring priceid |
invalid querystring priceid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.priceid) {
throw new Error('invalid-priceid')
}
const price = await global.api.administrator.subscriptions.Price.get(req)
if (!price) {
throw new Error('invalid-priceid')
}
if (price.stripeObject.active) {
throw new Error('invalid-price')
}
const updateInfo = {
active: true
}
const priceNow = await stripeCache.execute('prices', 'update', req.query.priceid, updateInfo, req.stripeKey)
await subscriptions.Storage.Price.update({
stripeObject: priceNow,
active: priceNow.active
}, {
where: {
priceid: req.query.priceid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.priceid)
return global.api.administrator.subscriptions.Price.get(req)
}
}
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/set-price-active', 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/set-price-active')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} 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/set-price-active?priceid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-priceid')
})
})
describe('invalid-price', () => {
it('ineligible querystring price is active', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-price-active?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-price')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithInactivePrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-price-active?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const price = await req.patch()
assert.strictEqual(price.stripeObject.active, true)
})
})
})