/api/administrator/subscriptions/set-invoice-uncollectible (PATCH)
await global.api.administrator.subscriptions.SetInvoiceUncollectible.patch(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 invoiceid | |
invalid-invoiceid | missing querystring invoiceid |
invalid querystring invoice is marked uncollectable | |
invalid-invoice | invalid querystring invoice is paid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.invoiceid) {
throw new Error('invalid-invoiceid')
}
const invoice = await global.api.administrator.subscriptions.Invoice.get(req)
if (!invoice) {
throw new Error('invalid-invoiceid')
}
if (invoice.status !== 'open' && invoice.status !== 'draft') {
throw new Error('invalid-invoice')
}
const invoiceNow = await stripeCache.execute('invoices', 'markUncollectible', req.query.invoiceid, req.stripeKey)
await stripeCache.update(invoiceNow)
return invoiceNow
}
}
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/administrator/subscriptions/set-invoice-uncollectible', function () {
this.timeout(60 * 60 * 1000)
before(TestHelper.setupWebhook)
after(TestHelper.deleteOldWebhooks)
describe('exceptions', () => {
describe('invalid-invoiceid', () => {
it('missing querystring invoiceid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-invoice-uncollectible?invoiceid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
it('invalid querystring invoiceid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-invoice-uncollectible?invoiceid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
})
describe('invalid-invoice', () => {
it('invalid querystring invoice is paid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-invoice-uncollectible?invoiceid=${user.invoice.id}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoice')
})
it('invalid querystring invoice is marked uncollectable', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createAmountOwed(user)
await TestHelper.forgiveInvoice(administrator, user.invoice.id)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-invoice-uncollectible?invoiceid=${user.invoice.id}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoice')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createAmountOwed(user)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-invoice-uncollectible?invoiceid=${user.invoice.id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const invoiceNow = await req.patch()
assert.strictEqual(invoiceNow.status, 'uncollectible')
})
})
})