/api/user/subscriptions/create-usage-record (POST)
await global.api.user.subscriptions.CreateUsageRecord.post(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-subscription | invalid querystring subscription is not "metered" |
invalid-account | ineligible accessing account |
invalid posted quantity is negative | |
invalid-quantity | invalid posted quantity is not integer |
invalid posted action | |
invalid-action | missing posted action |
invalid posted subscriptionitemid | |
invalid-subscriptionitemid | missing posted subscriptionitemid |
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
action | string | required | POST |
quantity | string | required | POST |
subscriptionitemid | string | required | POST |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const dashboard = require('@userdashboard/dashboard')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
post: 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 (subscription.plan.usage_type !== 'metered') {
throw new Error('invalid-subscription')
}
try {
const quantity = parseInt(req.body.quantity, 10)
if (req.body.quantity !== quantity.toString()) {
throw new Error('invalid-quantity')
}
} catch (s) {
throw new Error('invalid-quantity')
}
if (req.body.quantity < 0) {
throw new Error('invalid-quantity')
}
if (!req.body.action || (req.body.action !== 'increment' && req.body.action !== 'set')) {
throw new Error('invalid-action')
}
if (!req.body.subscriptionitemid || !req.body.subscriptionitemid.length) {
throw new Error('invalid-subscriptionitemid')
}
let found = false
for (const item of subscription.items.data) {
found = item.id === req.body.subscriptionitemid
if (found) {
break
}
}
if (!found) {
throw new Error('invalid-subscriptionitemid')
}
const usageInfo = {
action: req.body.action,
quantity: req.body.quantity
}
if (dashboard.Timestamp.now >= subscription.current_period_start) {
usageInfo.timestamp = dashboard.Timestamp.now
} else {
usageInfo.timestamp = subscription.current_period_start
}
const usageRecord = await stripeCache.execute('subscriptionItems', 'createUsageRecord', req.body.subscriptionitemid, usageInfo, req.stripeKey)
await stripeCache.update(usageRecord)
return usageRecord
}
}
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/create-usage-record', 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/create-usage-record')
req.account = user.account
req.session = user.session
req.body = {
email: user.profile.contactEmail,
description: 'customer'
}
let errorMessage
try {
await req.post()
} 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/create-usage-record?subscriptionid=invalid')
req.account = user.account
req.session = user.session
req.body = {
email: user.profile.contactEmail,
description: 'customer'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
})
describe('invalid-subscription', () => {
it('invalid querystring subscription is not "metered"', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'licensed'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscription')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestHelper.createUser()
const user2 = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user2, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user2.subscription.id}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-quantity', () => {
it('invalid posted quantity is not integer', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: 'abcde',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-quantity')
})
it('invalid posted quantity is negative', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '-100',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-quantity')
})
})
describe('invalid-action', () => {
it('missing posted action', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: '',
subscriptionitemid: user.subscription.items.data[0].id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-action')
})
it('invalid posted action', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'invalid',
subscriptionitemid: user.subscription.items.data[0].id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-action')
})
})
describe('invalid-subscriptionitemid', () => {
it('missing posted subscriptionitemid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: ''
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
it('invalid posted subscriptionitemid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: 'invalid'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
})
})
describe('receives', () => {
it('required posted quantity', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
const usageRecord = await req.post()
assert.strictEqual(usageRecord.quantity, 100)
})
it('required posted action', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
const usageRecord = await req.post()
assert.strictEqual(usageRecord.quantity, 100)
// TODO: thections can be verified setting/incrementing
// when the usage record summaries are aavailable
})
it('required posted subscriptionitemid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
const usageRecord = await req.post()
assert.strictEqual(usageRecord.subscription_item, user.subscription.items.data[0].id)
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({
usage_type: 'metered'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-usage-record?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
quantity: '100',
action: 'set',
subscriptionitemid: user.subscription.items.data[0].id
}
req.filename = __filename
req.saveResponse = true
const usageRecord = await req.post()
assert.strictEqual(usageRecord.object, 'usage_record')
})
})
})