/api/administrator/subscriptions/delete-plan (DELETE)
await global.api.administrator.subscriptions.DeletePlan.delete(req) Located in Stripe Subscriptions module API
Returns boolean
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 querystring planid | |
invalid-planid | missing querystring planid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
delete: async (req) => {
if (!req.query || !req.query.planid) {
throw new Error('invalid-planid')
}
const plan = await global.api.administrator.subscriptions.Plan.get(req)
if (!plan) {
throw new Error('invalid-planid')
}
await stripeCache.execute('plans', 'del', req.query.planid, req.stripeKey)
await subscriptions.StorageList.remove(`${req.appid}/plans`, req.query.planid)
if (plan.metadata.unpublished) {
await subscriptions.StorageList.remove(`${req.appid}/unpublished/plans`, req.query.planid)
} else if (plan.metadata.published) {
await subscriptions.StorageList.remove(`${req.appid}/published/plans`, req.query.planid)
}
await stripeCache.delete(req.query.planid, req.stripeKey)
return true
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
describe('/api/administrator/subscriptions/delete-plan', () => {
describe('exceptions', () => {
describe('invalid-planid', () => {
it('missing querystring planid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/delete-plan')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-planid')
})
it('invalid querystring planid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/delete-plan?planid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-planid')
})
})
})
describe('returns', () => {
it('boolean', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/delete-plan?planid=${administrator.plan.id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})