/api/user/subscriptions/set-invoice-paid (PATCH)
await global.api.user.subscriptions.SetInvoicePaid.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-account | ineligible accessing account |
ineligible posted payment method | |
invalid querystring invoice is marked uncollectable | |
invalid-invoice | invalid querystring invoice is paid |
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 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.user.subscriptions.Invoice.get(req)
if (!invoice) {
throw new Error('invalid-invoiceid')
}
if (invoice.status !== 'draft' && invoice.status !== 'open') {
throw new Error('invalid-invoice')
}
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 invoiceNow = await stripeCache.execute('invoices', 'pay', req.query.invoiceid, {
payment_method: paymentMethod.id
}, 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/user/subscriptions/set-invoice-paid', function () {
this.timeout(60 * 60 * 1000)
afterEach(TestHelper.deleteOldWebhooks)
beforeEach(TestHelper.setupWebhook)
describe('exceptions', () => {
describe('invalid-invoiceid', () => {
it('missing querystring invoiceid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/set-invoice-paid')
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
it('invalid querystring invoiceid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/set-invoice-paid?invoiceid=invalid')
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
})
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'
})
await TestHelper.createAmountOwed(user)
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user2.account
req.session = user2.session
req.body = {
paymentmethodid: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
it('ineligible posted payment method', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createAmountOwed(user)
const user2 = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: user2.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-invoice', () => {
it('invalid querystring invoice is paid', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({ amount: '1000', interval: 'month' })
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: user.paymentMethod.id
}
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/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: user.paymentMethod.id
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoice')
})
})
describe('invalid-paymentmethodid', () => {
it('missing posted paymentmethodid', async () => {
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
description: user.profile.firstName,
country: 'US'
})
await TestHelper.createAmountOwed(user)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: ''
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
it('invalid posted paymentmethodid', async () => {
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
description: user.profile.firstName,
country: 'US'
})
await TestHelper.createAmountOwed(user)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createAmountOwed(user)
const req = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.id}`)
req.account = user.account
req.session = user.session
req.body = {
paymentmethodid: user.paymentMethod.id
}
req.filename = __filename
req.saveResponse = true
const invoice = await req.patch()
assert.strictEqual(invoice.paid, true)
})
})
})