/api/user/subscriptions/set-subscription-plan (PATCH)
await global.api.user.subscriptions.SetSubscriptionPlan.patch(req) Located in Stripe Subscriptions module API
Returns object
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 subscriptionid | |
invalid-subscriptionid | missing querystring subscriptionid |
invalid-account | ineligible accessing account |
invalid posted planid | |
invalid-planid | missing posted planid |
invalid-plan | invalid posted plan is not published |
invalid posted plan is unchanged | |
invalid posted plan is unpublished | |
invalid-paymentmethodid | invalid customer requires payments method |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.subscriptionid) {
throw new Error('invalid-subscriptionid')
}
let subscription = await global.api.user.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionid')
}
if ((subscription.status !== 'active' && subscription.status !== 'trialing') || subscription.cancel_at_period_end) {
throw new Error('invalid-subscription')
}
if (!req.body || !req.body.planid) {
throw new Error('invalid-planid')
}
if (subscription.plan === req.body.planid) {
throw new Error('invalid-plan')
}
if (subscription.plan && subscription.plan.id && subscription.plan.id === req.body.planid) {
throw new Error('invalid-plan')
}
req.query.customerid = subscription.customer.id || subscription.customer
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-customerid')
}
req.query.planid = req.body.planid
const newPlan = await global.api.user.subscriptions.PublishedPlan.get(req)
if (!newPlan.metadata.published || newPlan.metadata.unpublished) {
throw new Error('invalid-plan')
}
if (newPlan.amount) {
if (!req.body.paymentmethodid || !req.body.paymentmethodid.length) {
throw new Error('invalid-paymentmethodid')
}
req.query.paymentmethodid = req.body.paymentmethodid
const paymentMethod = await global.api.user.subscriptions.PaymentMethod.get(req)
if (!paymentMethod) {
throw new Error('invalid-paymentmethodid')
}
}
const updateInfo = {
items: [{
id: subscription.items.data[0].id,
plan: req.body.planid
}]
}
// const oldPlan = subscription.plan
subscription = await stripeCache.execute('subscriptions', 'update', req.query.subscriptionid, updateInfo, req.stripeKey)
if (!subscription) {
throw new Error('unknown-error')
}
await stripeCache.update(subscription)
// if (newPlan.amount > oldPlan.amount || newPlan.interval !== oldPlan.interval) {
// const upcomingInvoice = await stripeCache.execute('invoices', 'create', {
// customer: customer.id,
// subscription: subscription.id,
// metadata: {
// appid: req.appid,
// accountid: req.account.accountid
// }
// }, req.stripeKey)
// const upcomingInvoiceNow = await stripeCache.execute('invoices', 'pay', upcomingInvoice.id, {
// payment_method: req.body.paymentmethodid
// }, req.stripeKey)
// await stripeCache.update(upcomingInvoiceNow)
// }
return subscription
}
}
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/user/subscriptions/set-subscription-plan', function () {
this.timeout(60 * 60 * 1000)
afterEach(TestHelper.deleteOldWebhooks)
beforeEach(TestHelper.setupWebhook)
describe('exceptions', () => {
describe('invalid-subscriptionid', () => {
it('missing querystring subscriptionid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/set-subscription-quantity')
req.account = user.account
req.session = user.session
req.body = {
planid: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
it('invalid querystring subscriptionid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/set-subscription-quantity?subscriptionid=invalid')
req.account = user.account
req.session = user.session
req.body = {
planid: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const plan2 = await TestHelper.createPlan(administrator, {
productid: administrator.product.id,
usage_type: 'licensed',
published: 'true',
trial_period_days: '0',
amount: '2000'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const user2 = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user2, plan2.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user2.account
req.session = user2.session
req.body = {
planid: administrator.plan.id,
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-planid', () => {
it('missing posted planid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: '',
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-planid')
})
it('invalid posted planid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: 'invalid',
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-planid')
})
})
describe('invalid-plan', () => {
it('invalid posted plan is not published', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const plan1 = administrator.plan
const plan2 = await TestHelper.createPlan(administrator, {
productid: administrator.product.id,
usage_type: 'licensed',
trial_period_days: '0',
amount: '1000'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, plan1.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: plan2.id,
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-plan')
})
it('invalid posted plan is unpublished', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const plan1 = administrator.plan
const plan2 = await TestHelper.createPlan(administrator, {
productid: administrator.product.id,
usage_type: 'licensed',
published: 'true',
unpublished: 'true',
trial_period_days: '0',
amount: '1000'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, plan1.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: plan2.id,
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-plan')
})
it('invalid posted plan is unchanged', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id,
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-plan')
})
})
})
describe('invalid-paymentmethodid', () => {
it('invalid customer requires payments method', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({ amount: 0 })
const plan1 = administrator.plan
const plan2 = await TestHelper.createPlan(administrator, {
productid: administrator.product.id,
usage_type: 'licensed',
published: 'true',
trial_period_days: '0',
amount: '2000'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, plan1.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: plan2.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const plan1 = administrator.plan
const plan2 = await TestHelper.createPlan(administrator, {
productid: administrator.product.id,
usage_type: 'licensed',
published: 'true',
amount: '1000'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, plan1.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-plan?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: plan2.id,
paymentmethodid: user.paymentMethod.id
}
req.filename = __filename
req.saveResponse = true
const subscriptionNow = await req.patch()
assert.strictEqual(subscriptionNow.plan.id, plan2.id)
})
})
})