/api/user/subscriptions/upcoming-invoice (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.UpcomingInvoice.get(req)Returns object
{
"object": "invoice",
"account_country": "AU",
"account_name": "test-subscriptions",
"account_tax_ids": null,
"amount_due": 1000,
"amount_paid": 0,
"amount_remaining": 1000,
"application": null,
"application_fee_amount": null,
"attempt_count": 0,
"attempted": false,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_reason": "upcoming",
"charge": null,
"collection_method": "charge_automatically",
"created": "{timestamp}",
"currency": "usd",
"custom_fields": null,
"customer": "cus_LwHOUqBBmTWIx3",
"customer_address": null,
"customer_email": "Kevin_OReilly@yahoo.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,
"last_finalization_error": null,
"lines": {
"object": "list",
"data": [
{
"id": "il_tmp_177b1bHHqepMFuCX84e6c85a",
"object": "line_item",
"amount": 1000,
"amount_excluding_tax": 1000,
"currency": "usd",
"description": "1 thing × product284 (at $10.00 / month)",
"discount_amounts": [],
"discountable": true,
"discounts": [],
"livemode": false,
"metadata": {},
"period": {
"end": 1661394867,
"start": 1658716467
},
"plan": {
"id": "price_1LEOpIHHqepMFuCXS2fbCYod",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656124464,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHOSYou6gc4dx",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOpIHHqepMFuCXS2fbCYod",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656124464,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwHOSYou6gc4dx",
"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_1LEOpMHHqepMFuCX7GA1ytPg",
"subscription_item": "si_LwHOQDiTwc88tx",
"tax_amounts": [],
"tax_rates": [],
"type": "subscription",
"unit_amount_excluding_tax": "1000"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/upcoming/lines?customer=cus_LwHOUqBBmTWIx3&subscription=sub_1LEOpMHHqepMFuCX7GA1ytPg"
},
"livemode": false,
"metadata": {},
"next_payment_attempt": "{timestamp}",
"number": null,
"on_behalf_of": null,
"paid": false,
"paid_out_of_band": false,
"payment_intent": null,
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null
},
"period_end": "{timestamp}",
"period_start": "{timestamp}",
"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": "draft",
"status_transitions": {
"finalized_at": null,
"marked_uncollectible_at": null,
"paid_at": null,
"voided_at": null
},
"subscription": "sub_1LEOpMHHqepMFuCX7GA1ytPg",
"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
}
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 |
invalid-subscriptionid | missing querystring subscriptionid |
invalid querystring subscriptionid |
NodeJS source (view on github)
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.subscriptionid) {
throw new Error('invalid-subscriptionid')
}
const subscription = await global.api.user.subscriptions.Subscription.get(req)
const invoice = await stripeCache.execute('invoices', 'retrieveUpcoming', {
customer: subscription.customerid,
subscription: req.query.subscriptionid
}, req.stripeKey)
if (!invoice) {
throw new Error('invalid-subscriptionid')
}
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/upcoming-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/upcoming-invoice?subscriptionid=${user.subscription.subscriptionid}`)
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/upcoming-invoice?subscriptionid=${user.subscription.subscriptionid}`)
req2.account = user.account
req2.session = user.session
req2.filename = __filename
req2.saveResponse = true
cachedResponses.response = await req2.get()
// invalid subscription
await TestHelper.cancelSubscription(user)
const req3 = TestHelper.createRequest(`/api/user/subscriptions/upcoming-invoice?subscriptionid=${user.subscription.subscriptionid}`)
req3.account = user.account
req3.session = user.session
try {
await req3.get()
} catch (error) {
cachedResponses.invalidSubscription = error.message
}
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-subscriptionid', () => {
it('missing querystring subscriptionid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/upcoming-invoice')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
it('invalid querystring subscriptionid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/upcoming-invoice?subscriptionid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
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', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidSubscription
assert.strictEqual(errorMessage, 'invalid-subscription')
})
})
})
describe('returns', () => {
it('object', async () => {
const invoice = cachedResponses.response
assert.strictEqual(invoice.object, 'invoice')
})
})
})