/api/administrator/subscriptions/invoice (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.Invoice.get(req)Returns object
{
"invoiceid": "in_1LEOJZHHqepMFuCXudUgZp2B",
"object": "invoice",
"stripeObject": {
"id": "in_1LEOJZHHqepMFuCXudUgZp2B",
"object": "invoice",
"account_country": "AU",
"account_name": "test-subscriptions",
"account_tax_ids": null,
"amount_due": 1000,
"amount_paid": 1000,
"amount_remaining": 0,
"application": null,
"application_fee_amount": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": false,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_reason": "subscription_create",
"charge": "ch_3LEOJaHHqepMFuCX1rlCIhhF",
"collection_method": "charge_automatically",
"created": 1656122497,
"currency": "usd",
"custom_fields": null,
"customer": "cus_LwGrNEdfBbV2BE",
"customer_address": null,
"customer_email": "Ernestine3@gmail.com",
"customer_name": null,
"customer_phone": null,
"customer_shipping": null,
"customer_tax_exempt": "none",
"customer_tax_ids": [],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"discounts": [],
"due_date": null,
"ending_balance": 0,
"footer": null,
"hosted_invoice_url": "https://invoice.stripe.com/i/acct_1KhiP8HHqepMFuCX/test_YWNjdF8xS2hpUDhISHFlcE1GdUNYLF9Md0dyNXpXeUEzbEVDak44YmNOUzNaTEUyVWJ3Smc3LDQ2NjYzMzAx02001ql5PjDs?s=ap",
"invoice_pdf": "https://pay.stripe.com/invoice/acct_1KhiP8HHqepMFuCX/test_YWNjdF8xS2hpUDhISHFlcE1GdUNYLF9Md0dyNXpXeUEzbEVDak44YmNOUzNaTEUyVWJ3Smc3LDQ2NjYzMzAx02001ql5PjDs/pdf?s=ap",
"last_finalization_error": null,
"lines": {
"object": "list",
"data": [
{
"id": "il_1LEOJZHHqepMFuCXGcAI0grH",
"object": "line_item",
"amount": 1000,
"amount_excluding_tax": 1000,
"currency": "usd",
"description": "1 thing × product179 (at $10.00 / month)",
"discount_amounts": [],
"discountable": true,
"discounts": [],
"livemode": false,
"metadata": {},
"period": {
"end": 1658714497,
"start": 1656122497
},
"plan": {
"id": "price_1LEOJVHHqepMFuCXcdlODsmY",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656122493,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwGrisTQCH1ifo",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOJVHHqepMFuCXcdlODsmY",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656122493,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwGrisTQCH1ifo",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 1000,
"unit_amount_decimal": "1000"
},
"proration": false,
"proration_details": {
"credited_items": null
},
"quantity": 1,
"subscription": "sub_1LEOJZHHqepMFuCXHOwQZTh6",
"subscription_item": "si_LwGrsfPz4zdpVg",
"tax_amounts": [],
"tax_rates": [],
"type": "subscription",
"unit_amount_excluding_tax": "1000"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/in_1LEOJZHHqepMFuCXudUgZp2B/lines"
},
"livemode": false,
"metadata": {},
"next_payment_attempt": null,
"number": "C811BEDB-0001",
"on_behalf_of": null,
"paid": true,
"paid_out_of_band": false,
"payment_intent": "pi_3LEOJaHHqepMFuCX1h9kEvNS",
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null
},
"period_end": 1656122497,
"period_start": 1656122497,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"quote": null,
"receipt_number": null,
"rendering_options": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "paid",
"status_transitions": {
"finalized_at": 1656122497,
"marked_uncollectible_at": null,
"paid_at": 1656122497,
"voided_at": null
},
"subscription": "sub_1LEOJZHHqepMFuCXHOwQZTh6",
"subtotal": 1000,
"subtotal_excluding_tax": 1000,
"tax": null,
"test_clock": null,
"total": 1000,
"total_discount_amounts": [],
"total_excluding_tax": 1000,
"total_tax_amounts": [],
"transfer_data": null,
"webhooks_delivered_at": null
},
"customerid": "cus_LwGrNEdfBbV2BE",
"subscriptionid": "sub_1LEOJZHHqepMFuCXHOwQZTh6",
"accountid": "acct_3e6d36515ef38a28",
"appid": "tests_1656122493",
"createdAt": "2022-06-25T02:01:40.867Z",
"updatedAt": "2022-06-25T02:01:41.558Z"
}
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-invoiceid | missing querystring invoiceid |
invalid querystring invoiceid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.invoiceid) {
throw new Error('invalid-invoiceid')
}
let invoice = await dashboard.StorageCache.get(req.query.invoiceid)
if (!invoice) {
const invoiceInfo = await subscriptions.Storage.Invoice.findOne({
where: {
invoiceid: req.query.invoiceid,
appid: req.appid || global.appid
}
})
if (!invoiceInfo) {
throw new Error('invalid-invoiceid')
}
invoice = {}
for (const field of invoiceInfo._options.attributes) {
invoice[field] = invoiceInfo.get(field)
}
await dashboard.StorageCache.set(req.query.invoiceid, invoice)
}
return invoice
}
}
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')
describe('/api/administrator/subscriptions/invoice', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-invoiceid', () => {
it('missing querystring invoiceid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/invoice')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} 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/invoice?invoiceid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/invoice?invoiceid=${user.invoice.invoiceid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const invoice = await req.get()
assert.strictEqual(invoice.object, 'invoice')
})
})
})