/api/user/subscriptions/delete-customer (DELETE)
await global.api.user.subscriptions.DeleteCustomer.delete(req) Located in Stripe Subscriptions module API
Returns boolean
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 = {
delete: 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')
}
await stripeCache.execute('customers', 'del', req.query.customerid, req.stripeKey)
await stripeCache.delete(req.query.customerid, req.stripeKey)
await subscriptions.StorageList.remove(`${req.appid}/customers`, req.query.customerid)
await subscriptions.StorageList.remove(`${req.appid}/account/customers/${req.account.accountid}`, req.query.customerid)
await subscriptions.Storage.delete(`${req.appid}/map/accountid/customerid/${req.query.customerid}`)
return true
}
}
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/user/subscriptions/delete-customer', () => {
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/delete-customer')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} 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/delete-customer?customerid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
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/delete-customer?customerid=${user.customer.id}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('boolean', async () => {
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/delete-customer?customerid=${user.customer.id}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})