/api/administrator/subscriptions/update-price (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.UpdatePrice.patch(req)Returns object
{
"priceid": "price_1LEOXKHHqepMFuCXIzTQ4A7x",
"object": "price",
"stripeObject": {
"id": "price_1LEOXKHHqepMFuCXIzTQ4A7x",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656123350,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": "new nickname",
"product": "prod_LwH5Uhb5x6s0AN",
"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_LwH5Uhb5x6s0AN",
"active": true,
"appid": "tests_1656123349",
"createdAt": "2022-06-25T02:15:50.961Z",
"updatedAt": "2022-06-25T02:15:51.300Z"
}
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
nickname | string | required | POST |
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-nickname | missing posted nickname |
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 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')
}
if (!req.body || !req.body.nickname) {
throw new Error('invalid-nickname')
}
const updateInfo = {
nickname: req.body.nickname
}
const priceNow = await stripeCache.execute('prices', 'update', req.query.priceid, updateInfo, req.stripeKey)
await subscriptions.Storage.Price.update({
stripeObject: priceNow,
productid: price.productid
}, {
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/update-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/update-price')
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/update-price?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 not active', async () => {
const administrator = await TestStripeAccounts.createOwnerWithInactivePrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-price?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
nickname: 'new nickname'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-price')
})
})
describe('invalid-nickname', () => {
it('missing posted nickname', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-price?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
nickname: ''
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-nickname')
})
})
})
describe('receives', () => {
it('required posted nickname (string)', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-price?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
nickname: 'updated'
}
await req.patch()
const req2 = TestHelper.createRequest(`/api/administrator/subscriptions/price?priceid=${administrator.price.priceid}`)
req2.account = administrator.account
req2.session = administrator.session
const price = await req2.get()
assert.strictEqual(price.stripeObject.nickname, 'updated')
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-price?priceid=${administrator.price.priceid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
nickname: 'new nickname'
}
req.filename = __filename
req.saveResponse = true
const price = await req.patch()
assert.strictEqual(price.stripeObject.nickname, 'new nickname')
})
})
})