/api/user/subscriptions/create-subscription (POST)
await global.api.user.subscriptions.CreateSubscription.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 customerid | |
invalid-customerid | missing querystring customerid |
invalid-customer | ineligible querystring customer requires payment method |
invalid-account | ineligible accessing account |
invalid posted planid | |
invalid-planid | missing posted planid |
invalid-plan | ineligible posted plan is not published |
ineligible posted plan is unpublished |
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.query || !req.query.customerid) {
throw new Error('invalid-customerid')
}
if (!req.body || !req.body.planid) {
throw new Error('invalid-planid')
}
if (req.body.quantity) {
try {
const quantity = parseInt(req.body.quantity, 10)
if (quantity < 1 || quantity.toString() !== req.body.quantity) {
throw new Error('invalid-quantity')
}
} catch (error) {
throw new Error('invalid-quantity')
}
}
if (req.body.tax_percent) {
try {
const percent = parseInt(req.body.tax_percent, 10)
if (percent < 0 || percent.toString() !== req.body.tax_percent) {
throw new Error('invalid-tax_percent')
}
} catch (error) {
throw new Error('invalid-tax_percent')
}
}
req.query.planid = req.body.planid
const plan = await global.api.user.subscriptions.PublishedPlan.get(req)
if (!plan) {
throw new Error('invalid-planid')
}
if (plan.metadata.unpublished) {
throw new Error('invalid-plan')
}
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!req.body.paymentmethodid || !req.body.paymentmethodid.length) {
req.body.paymentmethodid = customer.invoice_settings.default_payment_method
}
if (plan.amount) {
if (!req.body.paymentmethodid || !req.body.paymentmethodid.length) {
throw new Error('invalid-paymentmethodid')
}
if (process.env.NODE_ENV !== 'testing' && req.body.paymentmethodid.startsWith('pm_card_')) {
req.query.paymentmethodid = req.body.paymentmethodid
const paymentMethod = await global.api.user.subscriptions.PaymentMethod.get(req)
if (!paymentMethod) {
throw new Error('invalid-paymentmethodid')
}
}
}
const subscriptionInfo = {
customer: req.query.customerid,
items: [{
plan: req.body.planid
}],
metadata: {
appid: req.appid,
accountid: req.account.accountid
},
tax_percent: req.body.tax_percent || 0,
enable_incomplete_payments: true
}
if (req.body.quantity && plan.usage_type === 'licensed') {
subscriptionInfo.items[0].quantity = req.body.quantity
}
if (req.body.paymentmethodid) {
subscriptionInfo.default_payment_method = req.body.paymentmethodid
}
if (plan.trial_period_days) {
subscriptionInfo.trial_end = dashboard.Timestamp.now + (plan.trial_period_days * 24 * 60 * 60)
}
const subscription = await stripeCache.execute('subscriptions', 'create', subscriptionInfo, req.stripeKey)
if (!subscription) {
throw new Error('unknown-error')
}
await stripeCache.update(subscription)
await subscriptions.StorageList.addMany({
[`${req.appid}/subscriptions`]: subscription.id,
[`${req.appid}/account/subscriptions/${req.account.accountid}`]: subscription.id,
[`${req.appid}/customer/subscriptions/${req.query.customerid}`]: subscription.id,
[`${req.appid}/account/plan/subscriptions/${req.query.planid}/${req.account.accountid}`]: subscription.id,
[`${req.appid}/account/product/subscriptions/${plan.product}/${req.account.accountid}`]: subscription.id,
[`${req.appid}/account/plan/customers/${req.query.planid}/${req.account.accountid}`]: req.query.customerid,
[`${req.appid}/account/product/customers/${plan.product}/${req.account.accountid}`]: req.query.customerid,
[`${req.appid}/plan/subscriptions/${req.query.planid}`]: subscription.id,
[`${req.appid}/product/subscriptions/${plan.product}`]: subscription.id,
[`${req.appid}/plan/customers/${req.query.planid}`]: req.query.customerid,
[`${req.appid}/product/customers/${plan.product}`]: req.query.customerid
})
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/create-subscription', () => {
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-subscription')
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
it('invalid querystring customerid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-subscription?customerid=invalid')
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-customer', () => {
it('ineligible querystring customer requires payment method', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
description: user.profile.firstName,
country: 'US'
})
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
description: user.profile.firstName,
country: 'US'
})
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user2.account
req.session = user2.session
req.body = {
planid: administrator.plan.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-planid', () => {
it('missing posted planid', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: 'invalid',
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-planid')
})
it('invalid posted planid', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: 'invalid',
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-planid')
})
})
describe('invalid-plan', () => {
it('ineligible posted plan is not published', async () => {
const administrator = await TestStripeAccounts.createOwnerWithNotPublishedPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id,
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-plan')
})
it('ineligible posted plan is unpublished', async () => {
const administrator = await TestStripeAccounts.createOwnerWithUnpublishedPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id,
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-plan')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
planid: administrator.plan.id,
paymentmethodid: user.paymentMethod.id
}
req.filename = __filename
req.saveResponse = true
const subscriptionNow = await req.post()
assert.strictEqual(subscriptionNow.object, 'subscription')
})
})
})