/api/user/subscriptions/create-setup-intent (POST)
await global.api.user.subscriptions.CreateSetupIntent.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-account | ineligible accessing account |
invalid posted paymentmethodid | |
invalid-paymentmethodid | missing posted paymentmethodid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
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')
}
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-customerid')
}
if (!req.body || !req.body.paymentmethodid) {
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 setupIntent = await stripeCache.execute('setupIntents', 'create', {
payment_method_types: ['card'],
customer: req.query.customerid,
payment_method: req.body.paymentmethodid
}, req.stripeKey)
await stripeCache.update(setupIntent)
await subscriptions.StorageList.addMany({
[`${req.appid}/setupIntents`]: setupIntent.id,
[`${req.appid}/customer/setupIntents/${req.query.customerid}`]: setupIntent.id,
[`${req.appid}/account/setupIntents/${req.account.accountid}`]: setupIntent.id
})
return setupIntent
}
}
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-setup-intent', () => {
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async () => {
global.stripeJS = false
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-setup-intent')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
it('invalid querystring customerid', async () => {
global.stripeJS = false
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-setup-intent?customerid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
global.stripeJS = false
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-setup-intent?customerid=${user.customer.id}`)
req.account = user2.account
req.session = user2.session
req.body = {
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-paymentmethodid', () => {
it('missing posted paymentmethodid', async () => {
global.stripeJS = false
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
description: user.profile.firstName
})
const req = TestHelper.createRequest(`/api/user/subscriptions/create-setup-intent?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: ''
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
it('invalid posted paymentmethodid', async () => {
global.stripeJS = false
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
description: user.profile.firstName
})
const req = TestHelper.createRequest(`/api/user/subscriptions/create-setup-intent?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: 'invalid'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
})
})
describe('returns', () => {
it('object', async () => {
global.stripeJS = false
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/create-setup-intent?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: user.paymentMethod.id
}
req.filename = __filename
req.saveResponse = true
const setupIntent = await req.post()
assert.strictEqual(setupIntent.object, 'setup_intent')
})
})
})