/api/user/subscriptions/customer (GET)
await global.api.user.subscriptions.Customer.get(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 |
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 = {
get: async (req) => {
if (!req.query || !req.query.customerid) {
throw new Error('invalid-customerid')
}
const exists = await subscriptions.StorageList.exists(`${req.appid}/customers`, req.query.customerid)
if (!exists) {
throw new Error('invalid-customerid')
}
const owned = await subscriptions.StorageList.exists(`${req.appid}/account/customers/${req.account.accountid}`, req.query.customerid)
if (!owned) {
throw new Error('invalid-account')
}
const customer = await stripeCache.retrieve(req.query.customerid, 'customers', req.stripeKey)
if (!customer) {
throw new Error('invalid-customerid')
}
if (customer.metadata.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
return customer
}
}
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/customer', () => {
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/customer')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
it('invalid querystring customerid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/customer?customerid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/customer?customerid=${user.customer.id}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/customer?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const customer = await req.get()
assert.strictEqual(customer.id, user.customer.id)
})
})
})