Stripe Subscriptions module API explorer

/api/user/subscriptions/customers (GET)

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

await global.api.user.subscriptions.Customers.get(req)

Returns array

[
  {
    "customerid": "cus_LwHCbkHdHGCZbf",
    "object": "customer",
    "accountid": "acct_8b0825541fc85b15",
    "couponid": null,
    "stripeObject": {
      "id": "cus_LwHCbkHdHGCZbf",
      "object": "customer",
      "address": null,
      "balance": 0,
      "created": 1656123783,
      "currency": null,
      "default_source": null,
      "delinquent": false,
      "description": "Visa Electron",
      "discount": null,
      "email": "Benny_Davis76@yahoo.com",
      "invoice_prefix": "B1818A67",
      "invoice_settings": {
        "custom_fields": null,
        "default_payment_method": null,
        "footer": null,
        "rendering_options": null
      },
      "livemode": false,
      "metadata": {},
      "name": null,
      "next_invoice_sequence": 1,
      "phone": null,
      "preferred_locales": [],
      "shipping": null,
      "tax_exempt": "none",
      "test_clock": null
    },
    "appid": "tests_1656123781",
    "createdAt": "2022-06-25T02:23:03.666Z",
    "updatedAt": "2022-06-25T02:23:03.666Z"
  },
  {
    "customerid": "cus_LwHCGYSoM63ZKP",
    "object": "customer",
    "accountid": "acct_8b0825541fc85b15",
    "couponid": null,
    "stripeObject": {
      "id": "cus_LwHCGYSoM63ZKP",
      "object": "customer",
      "address": null,
      "balance": 0,
      "created": 1656123783,
      "currency": null,
      "default_source": null,
      "delinquent": false,
      "description": "American Express (work)",
      "discount": null,
      "email": "Benny_Davis76@yahoo.com",
      "invoice_prefix": "0C872961",
      "invoice_settings": {
        "custom_fields": null,
        "default_payment_method": null,
        "footer": null,
        "rendering_options": null
      },
      "livemode": false,
      "metadata": {},
      "name": null,
      "next_invoice_sequence": 1,
      "phone": null,
      "preferred_locales": [],
      "shipping": null,
      "tax_exempt": "none",
      "test_clock": null
    },
    "appid": "tests_1656123781",
    "createdAt": "2022-06-25T02:23:03.278Z",
    "updatedAt": "2022-06-25T02:23:03.690Z"
  },
  {
    "customerid": "cus_LwHCdQUJQulynR",
    "object": "customer",
    "accountid": "acct_8b0825541fc85b15",
    "couponid": null,
    "stripeObject": {
      "id": "cus_LwHCdQUJQulynR",
      "object": "customer",
      "address": null,
      "balance": 0,
      "created": 1656123782,
      "currency": null,
      "default_source": null,
      "delinquent": false,
      "description": "MasterCard",
      "discount": null,
      "email": "Benny_Davis76@yahoo.com",
      "invoice_prefix": "FD8F8BAF",
      "invoice_settings": {
        "custom_fields": null,
        "default_payment_method": null,
        "footer": null,
        "rendering_options": null
      },
      "livemode": false,
      "metadata": {},
      "name": null,
      "next_invoice_sequence": 1,
      "phone": null,
      "preferred_locales": [],
      "shipping": null,
      "tax_exempt": "none",
      "test_clock": null
    },
    "appid": "tests_1656123781",
    "createdAt": "2022-06-25T02:23:02.879Z",
    "updatedAt": "2022-06-25T02:23:03.438Z"
  }
]

Receives

API routes may receive parameters from the URL and POST supporting simple and multipart:

Field Value Required Type
all boolean optional URL
limit integer optional URL
offset integer optional URL

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

NodeJS source (view on github)

const subscriptions = require('../../../../../index.js')

module.exports = {
  get: async (req) => {
    if (!req.query || !req.query.accountid) {
      throw new Error('invalid-accountid')
    }
    const account = await global.api.user.Account.get(req)
    if (!account) {
      throw new Error('invalid-account')
    }
    const where = {
      accountid: req.query.accountid,
      appid: req.appid || global.appid
    }
    let customerids
    if (req.query.all) {
      customerids = await subscriptions.Storage.Customer.findAll({
        where,
        attributes: ['customerid'],
        order: [
          ['createdAt', 'DESC']
        ]
      })
    } else {
      const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0
      const limit = req.query.limit ? parseInt(req.query.limit, 10) : global.pageSize
      customerids = await subscriptions.Storage.Customer.findAll({
        where,
        attributes: ['customerid'],
        offset,
        limit,
        order: [
          ['createdAt', 'DESC']
        ]
      })
    }
    if (!customerids || !customerids.length) {
      return null
    }
    const items = []
    for (const customerInfo of customerids) {
      req.query.customerid = customerInfo.dataValues.customerid
      const customer = await global.api.user.subscriptions.Customer.get(req)
      items.push(customer)
    }
    return items
  }
}

Test source (view on github)

/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')

describe('/api/user/subscriptions/customers', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  let cachedResponses, cachedCustomers
  async function bundledData (retryNumber) {
    if (retryNumber > 0) {
      cachedResponses = {}
    }
    if (cachedResponses && cachedResponses.finished) {
      return
    }
    cachedResponses = {}
    cachedCustomers = []
    await TestHelper.setupBefore()
    await DashboardTestHelper.setupBeforeEach()
    await TestHelper.setupBeforeEach()
    const administrator = await TestHelper.createOwner()
    await TestHelper.createProduct(administrator, {
      active: 'true'
    })
    const user = await TestHelper.createUser()
    for (let i = 0, len = global.pageSize + 2; i < len; i++) {
      await TestHelper.createCustomer(user, {
        email: user.profile.contactEmail
      })
      cachedCustomers.unshift(user.customer.customerid)
    }
    const req1 = TestHelper.createRequest(`/api/user/subscriptions/customers?accountid=${user.account.accountid}&offset=1`)
    req1.account = user.account
    req1.session = user.session
    cachedResponses.offset = await req1.get()
    const req2 = TestHelper.createRequest(`/api/user/subscriptions/customers?accountid=${user.account.accountid}&limit=1`)
    req2.account = user.account
    req2.session = user.session
    cachedResponses.limit = await req2.get()
    const req3 = TestHelper.createRequest(`/api/user/subscriptions/customers?accountid=${user.account.accountid}&all=true`)
    req3.account = user.account
    req3.session = user.session
    cachedResponses.all = await req3.get()
    const req4 = TestHelper.createRequest(`/api/user/subscriptions/customers?accountid=${user.account.accountid}`)
    req4.account = user.account
    req4.session = user.session
    req4.filename = __filename
    req4.saveResponse = true
    cachedResponses.returns = await req4.get()
    global.pageSize = 3
    cachedResponses.pageSize = await req4.get()
    global.pageSize = 2
    cachedResponses.finished = true
  }
  describe('exceptions', () => {
    describe('invalid-accountid', () => {
      it('missing querystring accountid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/customers')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-accountid')
      })

      it('invalid querystring accountid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/customers?accountid=invalid')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-accountid')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async function () {
        await bundledData(this.test.currentRetry())
        const user = await TestHelper.createUser()
        const user2 = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/customers?accountid=${user.account.accountid}`)
        req.account = user2.account
        req.session = user2.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

  describe('receives', () => {
    it('optional querystring offset (integer)', async function () {
      await bundledData(this.test.currentRetry())
      const offset = 1
      const customersNow = cachedResponses.offset
      for (let i = 0, len = global.pageSize; i < len; i++) {
        assert.strictEqual(customersNow[i].customerid, cachedCustomers[offset + i])
      }
    })

    it('optional querystring limit (integer)', async function () {
      await bundledData(this.test.currentRetry())
      const limit = 1
      const customersNow = cachedResponses.limit
      assert.strictEqual(customersNow.length, limit)
    })

    it('optional querystring all (boolean)', async () => {
      const customersNow = await cachedResponses.all
      assert.strictEqual(customersNow.length, cachedCustomers.length)
    })
  })

  describe('returns', () => {
    it('array', async () => {
      const customers = cachedResponses.returns
      assert.strictEqual(customers.length, global.pageSize)
    })
  })

  describe('configuration', () => {
    it('environment PAGE_SIZE', async () => {
      const customers = cachedResponses.pageSize
      assert.strictEqual(customers.length, global.pageSize + 1)
    })
  })
})