Stripe Subscriptions module API explorer

/api/administrator/subscriptions/usage-record (GET)

Account information like email addresses is generated with faker-js it is not real user information.

await global.api.administrator.subscriptions.UsageRecord.get(req)

Returns object

{
  "usagerecordid": "mbur_1LEOYNHHqepMFuCXYffEuMjU",
  "object": "usagerecord",
  "stripeObject": {
    "id": "mbur_1LEOYNHHqepMFuCXYffEuMjU",
    "object": "usage_record",
    "livemode": false,
    "quantity": 100,
    "subscription_item": "si_LwH6LEvpQE0bky",
    "timestamp": 1656123415
  },
  "customerid": "cus_LwH6Wjry7gXhMZ",
  "accountid": "acct_a8fe5ee1043c4495",
  "subscriptionid": "sub_1LEOYLHHqepMFuCXPQlQ4W08",
  "subscriptionitemid": "si_LwH6LEvpQE0bky",
  "appid": "tests_1656123408",
  "createdAt": "2022-06-25T02:16:55.600Z",
  "updatedAt": "2022-06-25T02:16:55.600Z"
}

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-usagerecordid missing querystring usagerecordid
invalid querystring usagerecordid

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.usagerecordid) {
      throw new Error('invalid-usagerecordid')
    }
    let usageRecord = await dashboard.StorageCache.get(req.query.usagerecordid)
    if (!usageRecord) {
      const usageRecordInfo = await subscriptions.Storage.UsageRecord.findOne({
        where: {
          usagerecordid: req.query.usagerecordid,
          appid: req.appid || global.appid
        }
      })
      if (!usageRecordInfo) {
        throw new Error('invalid-usagerecordid')
      }
      usageRecord = {}
      for (const field of usageRecordInfo._options.attributes) {
        usageRecord[field] = usageRecordInfo.get(field)
      }
      await dashboard.StorageCache.set(req.query.usagerecordid, usageRecord)
    }
    return usageRecord
  }
}

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/usage-record', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-usagerecordid', () => {
      it('missing querystring usagerecordid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/usage-record')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-usagerecordid')
      })

      it('invalid querystring usagerecordid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/usage-record?usagerecordid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-usagerecordid')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestStripeAccounts.createOwnerWithPrice({
        active: 'true',
        currency: 'usd',
        recurring_interval: 'month',
        recurring_interval_count: '1',
        recurring_usage_type: 'metered',
        recurring_aggregate_usage: 'sum',
        billing_scheme: 'tiered',
        tax_behavior: 'inclusive',
        tiers_mode: 'volume',
        tier1_up_to: '1000',
        tier1_flat_amount: '9999',
        tier2_up_to: 'inf',
        tier2_flat_amount: '8999'
      })
      const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
      await TestHelper.createUsageRecord(user, 100)
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/usage-record?usagerecordid=${user.usageRecord.usagerecordid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.filename = __filename
      req.saveResponse = true
      const usageRecord = await req.get()
      assert.strictEqual(usageRecord.usagerecordid, user.usageRecord.usagerecordid)
    })
  })
})