/api/administrator/subscriptions/create-product (POST)
await global.api.administrator.subscriptions.CreateProduct.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-name | missing posted name |
posted name too long | |
invalid-product-name-length | posted name too short |
invalid-statement_descriptor | missing posted statement_descriptor |
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
published | boolean | optional | 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 subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
post: async (req) => {
if (!req.body || !req.body.name) {
throw new Error('invalid-name')
}
if (global.minimumProductNameLength > req.body.name.length ||
global.maximumProductNameLength < req.body.name.length) {
throw new Error('invalid-product-name-length')
}
if (!req.body.statement_descriptor || !req.body.statement_descriptor.length) {
throw new Error('invalid-statement_descriptor')
}
if (!req.body.unit_label || !req.body.unit_label.length) {
throw new Error('invalid-unit_label')
}
const productInfo = {
type: 'service',
name: req.body.name,
statement_descriptor: req.body.statement_descriptor,
unit_label: req.body.unit_label,
metadata: {
appid: req.appid
}
}
if (req.body.published) {
productInfo.metadata.published = dashboard.Timestamp.now
}
const product = await stripeCache.execute('products', 'create', productInfo, req.stripeKey)
const indexing = {
[`${req.appid}/products`]: product.id
}
if (product.metadata.published) {
indexing[`${req.appid}/published/products`] = product.id
}
await subscriptions.StorageList.addMany(indexing)
return product
}
}
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')
describe('/api/administrator/subscriptions/create-product', () => {
describe('exceptions', () => {
describe('invalid-name', () => {
it('missing posted name', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: '',
statement_descriptor: 'description',
unit_label: 'thing'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-name')
})
})
describe('invalid-product-name-length', () => {
it('posted name too short', async () => {
global.minimumProductNameLength = 10
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'this',
statement_descriptor: 'description',
unit_label: 'thing'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-product-name-length')
})
it('posted name too long', async () => {
global.maximumProductNameLength = 1
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'that',
statement_descriptor: 'description',
unit_label: 'thing'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-product-name-length')
})
})
describe('invalid-statement_descriptor', () => {
it('missing posted statement_descriptor', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'productName',
statement_descriptor: '',
unit_label: 'thing'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-statement_descriptor')
})
})
})
describe('receives', () => {
it('optional posted published (boolean)', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
published: 'true'
})
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'product' + new Date().getTime() + 'r' + Math.ceil(Math.random() * 1000),
statement_descriptor: 'description',
unit_label: 'thing',
published: 'true'
}
const product = await req.post()
assert.notStrictEqual(product.metadata.published, undefined)
assert.notStrictEqual(product.metadata.published, null)
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
published: 'true'
})
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'product' + new Date().getTime() + 'r' + Math.ceil(Math.random() * 1000),
statement_descriptor: 'description',
unit_label: 'thing'
}
req.filename = __filename
req.saveResponse = true
const product = await req.post()
assert.strictEqual(product.object, 'product')
})
})
describe('configuration', () => {
it('environment MINIMUM_PRODUCT_NAME_LENGTH', async () => {
global.minimumProductNameLength = 10
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'this',
statement_descriptor: 'description',
unit_label: 'thing'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-product-name-length')
})
it('environment MAXIMUM_PRODUCT_NAME_LENGTH', async () => {
global.maximumProductNameLength = 1
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-product')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'that',
statement_descriptor: 'description',
unit_label: 'thing'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-product-name-length')
})
})
})