Stripe Subscriptions module API explorer

/api/administrator/subscriptions/customers (GET)

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

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

Returns array

[
  {
    "customerid": "cus_LwGqpAj3VXiLsO",
    "object": "customer",
    "accountid": "acct_a50afcf07a301408",
    "couponid": null,
    "stripeObject": {
      "id": "cus_LwGqpAj3VXiLsO",
      "object": "customer",
      "address": null,
      "balance": 0,
      "created": 1656122438,
      "currency": null,
      "default_source": null,
      "delinquent": false,
      "description": "Diners Club - Carte Blanche",
      "discount": null,
      "email": "Renee62@yahoo.com",
      "invoice_prefix": "D5F7C22F",
      "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_1656122437",
    "createdAt": "2022-06-25T02:00:38.964Z",
    "updatedAt": "2022-06-25T02:00:38.964Z"
  },
  {
    "customerid": "cus_LwGqfSvSBMrtOI",
    "object": "customer",
    "accountid": "acct_364490a4310a757f",
    "couponid": null,
    "stripeObject": {
      "id": "cus_LwGqfSvSBMrtOI",
      "object": "customer",
      "address": null,
      "balance": 0,
      "created": 1656122438,
      "currency": null,
      "default_source": null,
      "delinquent": false,
      "description": "Diners Club - Carte Blanche",
      "discount": null,
      "email": "Christie_Hackett@gmail.com",
      "invoice_prefix": "FD554A81",
      "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_1656122437",
    "createdAt": "2022-06-25T02:00:38.529Z",
    "updatedAt": "2022-06-25T02:00:38.985Z"
  },
  {
    "customerid": "cus_LwGqVcY38rn613",
    "object": "customer",
    "accountid": "acct_d4980c27f0e58f08",
    "couponid": null,
    "stripeObject": {
      "id": "cus_LwGqVcY38rn613",
      "object": "customer",
      "address": null,
      "balance": 0,
      "created": 1656122437,
      "currency": null,
      "default_source": null,
      "delinquent": false,
      "description": "Diners Club - Carte Blanche (work)",
      "discount": null,
      "email": "Dwayne.Hirthe@gmail.com",
      "invoice_prefix": "B9EBBED1",
      "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_1656122437",
    "createdAt": "2022-06-25T02:00:38.107Z",
    "updatedAt": "2022-06-25T02:00:38.776Z"
  }
]

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

NodeJS source (view on github)

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

module.exports = {
  get: async (req) => {
    req.query = req.query || {}
    const where = {
      appid: req.appid || global.appid
    }
    if (req.query.couponid) {
      const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
      if (!coupon) {
        throw new Error('invalid-couponid')
      }
      where.couponid = req.query.couponid
    }
    let customerids
    if (req.query.all) {
      customerids = await subscriptions.Storage.Customer.findAll({
        attributes: ['customerid'],
        order: [
          ['createdAt', 'DESC']
        ],
        where
      })
    } 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({
        attributes: ['customerid'],
        offset,
        limit,
        where,
        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.administrator.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/administrator/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()
    for (let i = 0, len = global.pageSize + 2; i < len; i++) {
      const user = await TestHelper.createUser()
      await TestHelper.createCustomer(user, {
        email: user.profile.contactEmail
      })
      cachedCustomers.unshift(user.customer.customerid)
    }
    const offset = 1
    const limit = 1
    const req1 = TestHelper.createRequest(`/api/administrator/subscriptions/customers?offset=${offset}`)
    req1.account = administrator.account
    req1.session = administrator.session
    cachedResponses.offset = await req1.get()
    const req2 = TestHelper.createRequest(`/api/administrator/subscriptions/customers?limit=${limit}`)
    req2.account = administrator.account
    req2.session = administrator.session
    cachedResponses.limit = await req2.get()
    const req3 = TestHelper.createRequest('/api/administrator/subscriptions/customers?all=true')
    req3.account = administrator.account
    req3.session = administrator.session
    cachedResponses.all = await req3.get()
    const req4 = TestHelper.createRequest('/api/administrator/subscriptions/customers')
    req4.account = administrator.account
    req4.session = administrator.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('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 = customersNow.length; i < len; i++) {
        assert.strictEqual(customersNow[i].customerid, cachedCustomers[i + offset])
      }
    })

    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 customers = cachedResponses.all
      assert.strictEqual(customers.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)
    })
  })
})