/api/user/subscriptions/invoice (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.Invoice.get(req)Returns object
{
"invoiceid": "in_1LEOfdHHqepMFuCXW1RQF5Rv",
"object": "invoice",
"stripeObject": {
"id": "in_1LEOfdHHqepMFuCXW1RQF5Rv",
"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_3LEOfdHHqepMFuCX0QbmZfSe",
"collection_method": "charge_automatically",
"created": 1656123865,
"currency": "usd",
"custom_fields": null,
"customer": "cus_LwHEDgWqqaQbUC",
"customer_address": null,
"customer_email": "Beverly79@hotmail.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_YWNjdF8xS2hpUDhISHFlcE1GdUNYLF9Md0hFT0VMazQ4NVdYQnVZa3pESldWbnVhN0NMbWFSLDQ2NjY0NjY50200ERApptuw?s=ap",
"invoice_pdf": "https://pay.stripe.com/invoice/acct_1KhiP8HHqepMFuCX/test_YWNjdF8xS2hpUDhISHFlcE1GdUNYLF9Md0hFT0VMazQ4NVdYQnVZa3pESldWbnVhN0NMbWFSLDQ2NjY0NjY50200ERApptuw/pdf?s=ap",
"last_finalization_error": null,
"lines": {
"object": "list",
"data": [
{
"id": "il_1LEOfdHHqepMFuCXEPcclJMq",
"object": "line_item",
"amount": 1000,
"amount_excluding_tax": 1000,
"currency": "usd",
"description": "1 thing × product265 (at $10.00 / month)",
"discount_amounts": [],
"discountable": true,
"discounts": [],
"livemode": false,
"metadata": {},
"period": {
"end": 1658715864,
"start": 1656123864
},
"plan": {
"id": "price_1LEOfZHHqepMFuCXIwikLlGo",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656123861,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHEoz0nrs14q3",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOfZHHqepMFuCXIwikLlGo",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656123861,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwHEoz0nrs14q3",
"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_1LEOfdHHqepMFuCXRVPHNdzu",
"subscription_item": "si_LwHEuICDeBHDct",
"tax_amounts": [],
"tax_rates": [],
"type": "subscription",
"unit_amount_excluding_tax": "1000"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/in_1LEOfdHHqepMFuCXW1RQF5Rv/lines"
},
"livemode": false,
"metadata": {},
"next_payment_attempt": null,
"number": "F1440116-0001",
"on_behalf_of": null,
"paid": true,
"paid_out_of_band": false,
"payment_intent": "pi_3LEOfdHHqepMFuCX0nKlNnNs",
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null
},
"period_end": 1656123864,
"period_start": 1656123864,
"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": 1656123865,
"marked_uncollectible_at": null,
"paid_at": 1656123864,
"voided_at": null
},
"subscription": "sub_1LEOfdHHqepMFuCXRVPHNdzu",
"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_LwHEDgWqqaQbUC",
"subscriptionid": "sub_1LEOfdHHqepMFuCXRVPHNdzu",
"accountid": "acct_87ff65e3cf5e6cf3",
"appid": "tests_1656123860",
"createdAt": "2022-06-25T02:24:28.732Z",
"updatedAt": "2022-06-25T02:24:29.790Z"
}
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-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')
}
if (invoiceInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
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')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/subscriptions/invoice', 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)
const user2 = await TestHelper.createUser()
// invalid account
const req = TestHelper.createRequest(`/api/user/subscriptions/invoice?invoiceid=${user.invoice.invoiceid}`)
req.account = user2.account
req.session = user2.session
try {
await req.get()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// response
const req2 = TestHelper.createRequest(`/api/user/subscriptions/invoice?invoiceid=${user.invoice.invoiceid}`)
req2.account = user.account
req2.session = user.session
req2.filename = __filename
req2.saveResponse = true
cachedResponses.returns = await req2.get()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-invoiceid', () => {
it('missing querystring invoiceid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/invoice')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
it('invalid querystring invoiceid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/invoice?invoiceid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invoiceid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidAccount
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const invoice = cachedResponses.returns
assert.strictEqual(invoice.object, 'invoice')
})
})
})