/api/user/subscriptions/set-subscription-quantity (PATCH)
await global.api.user.subscriptions.SetSubscriptionQuantity.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-quantity | invalid posted quantity |
invalid posted quantity is negative | |
invalid posted quantity is unchanged | |
invalid posted quantity is zero |
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')
}
const subscription = await global.api.user.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionid')
}
if (!req.body || !req.body.quantity) {
throw new Error('invalid-quantity')
}
try {
const quantity = parseInt(req.body.quantity, 10)
if (quantity < 1 || quantity.toString() !== req.body.quantity) {
throw new Error('invalid-quantity')
}
if (subscription.quantity === quantity) {
throw new Error('invalid-quantity')
}
} catch (error) {
throw new Error('invalid-quantity')
}
// req.query.customerid = subscription.customer.id || subscription.customer
// const customer = await global.api.user.subscriptions.Customer.get(req)
const updateInfo = {
items: [{
id: subscription.items.data[0].id,
quantity: req.body.quantity
}]
}
const subscriptionNow = await stripeCache.execute('subscriptions', 'update', req.query.subscriptionid, updateInfo, req.stripeKey)
if (!subscriptionNow) {
throw new Error('unknown-error')
}
// let upcomingInvoice = await stripeCache.execute('invoices', 'create', {
// customer: customer.id,
// subscription: subscription.id,
// metadata: {
// appid: req.appid,
// accountid: req.account.accountid
// }
// }, req.stripeKey)
// const amount = upcomingInvoice.total
// if (amount > 0) {
// upcomingInvoice = await stripeCache.exeecute('invoices', 'pay', upcomingInvoice.id, {
// payment_method: req.body.paymentmethodid
// }, req.stripeKey)
// await stripeCache.update(upcomingInvoice)
// }
return subscriptionNow
}
}
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-quantity', 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 = {
quantity: '10'
}
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 = {
quantity: '10'
}
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 user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-quantity?subscriptionid=${user.subscription.id}`)
req.account = user2.account
req.session = user2.session
req.body = {
quantity: '1'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-quantity', () => {
it('invalid posted quantity', 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-quantity?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: 'letters'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-quantity')
})
it('invalid posted quantity 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-quantity?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '1'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-quantity')
})
it('invalid posted quantity is negative', 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-quantity?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '-1'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-quantity')
})
it('invalid posted quantity is zero', 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-quantity?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '0'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-quantity')
})
})
})
describe('returns', () => {
it('object', 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-quantity?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '2'
}
req.filename = __filename
req.saveResponse = true
const subscriptionNow = await req.patch()
assert.strictEqual(subscriptionNow.quantity, 2)
})
})
})