/api/user/subscriptions/delete-subscription (DELETE)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.DeleteSubscription.delete(req)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-account | ineligible accessing account |
invalid-subscription | ineligible querystring subscription is not active |
invalid-subscriptionid | missing querystring subscriptionid |
invalid querystring subscriptionid |
NodeJS source (view on github)
const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.js')
module.exports = {
delete: async (req) => {
if (!req.query || !req.query.subscriptionid) {
throw new Error('invalid-subscriptionid')
}
const subscription = await global.api.user.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionid')
}
if ((subscription.stripeObject.status !== 'active' && subscription.stripeObject.status !== 'trialing') || subscription.stripeObject.cancel_at_period_end) {
throw new Error('invalid-subscription')
}
if (subscription.stripeObject.status !== 'trialing') {
const invoice = await global.api.user.subscriptions.SubscriptionProratedInvoice.get(req)
if (invoice.amount < 0) {
req.query.accountid = req.account.accountid
req.query.limit = 1
const recentInvoices = await global.api.user.subscriptions.Invoices.get(req)
const refundInfo = {
charge: recentInvoices[0].charge,
amount: -invoice.amount,
reason: 'requested_by_customer'
}
const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
req.query.refundid = refund.id
await subscriptions.Storage.Refund.create({
appid: req.appid || global.appid,
refundid: refund.id,
accountid: req.account.accountid,
subscriptionid: subscription.subscriptionid,
customerid: invoice.customerid,
invoiceid: invoice.invoiceid,
productid: invoice.productid,
paymentmethodid: invoice.paymentmethodid,
stripeObject: refund
})
}
}
const subscriptionNow = await stripeCache.execute('subscriptions', 'del', req.query.subscriptionid, req.stripeKey)
await subscriptions.Storage.Subscription.update({
stripeObject: subscriptionNow
}, {
where: {
subscriptionid: req.query.subscriptionid,
appid: req.appid || global.appid
}
})
return true
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/subscriptions/delete-subscription', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
let cachedResponses
async function bundledData (retryNumber) {
if (retryNumber > 0) {
cachedResponses = {}
}
if (cachedResponses && cachedResponses.finished) {
return
}
cachedResponses = {}
await TestHelper.setupBefore()
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
// missing or invalid id
const req = TestHelper.createRequest('/api/user/subscriptions/delete-subscription')
req.account = user.account
req.session = user.session
req.body = {
refund: 'at_period_end'
}
try {
await req.delete()
} catch (error) {
cachedResponses.missing = error.message
}
const req2 = TestHelper.createRequest('/api/user/subscriptions/delete-subscription?subscriptionid=invalid')
req2.account = user.account
req2.session = user.session
try {
await req2.delete()
} catch (error) {
cachedResponses.invalid = error.message
}
// invalid account
const user2 = await TestHelper.createUser()
const req3 = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-canceled?subscriptionid=${user.subscription.subscriptionid}`)
req3.account = user2.account
req3.session = user2.session
req3.body = {
refund: 'at_period_end'
}
try {
await req3.patch()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// invalid subscription
await TestHelper.cancelSubscription(user)
const req4 = TestHelper.createRequest(`/api/user/subscriptions/delete-subscription?subscriptionid=${user.subscription.subscriptionid}`)
req4.account = user.account
req4.session = user.session
try {
await req4.delete()
} catch (error) {
cachedResponses.invalidSubscription = error.message
}
// returns
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
const req5 = TestHelper.createRequest(`/api/user/subscriptions/delete-subscription?subscriptionid=${user.subscription.subscriptionid}`)
req5.account = user.account
req5.session = user.session
req5.body = {
refund: 'credit'
}
req5.filename = __filename
req5.saveResponse = true
cachedResponses.returns = await req5.delete()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-subscriptionid', () => {
it('missing querystring subscriptionid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
it('invalid querystring subscriptionid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidAccount
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-subscription', () => {
it('ineligible querystring subscription is not active', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidSubscription
assert.strictEqual(errorMessage, 'invalid-subscription')
})
})
})
describe('returns', () => {
it('boolean', async () => {
const deleted = cachedResponses.returns
assert.strictEqual(deleted, true)
})
})
})