Stripe Subscriptions module API explorer

/api/administrator/subscriptions/customer (GET)

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

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

Returns object

{
  "customerid": "cus_LwGqzgxgSKCG6o",
  "object": "customer",
  "accountid": "acct_d62b310c53a34055",
  "couponid": null,
  "stripeObject": {
    "id": "cus_LwGqzgxgSKCG6o",
    "object": "customer",
    "address": null,
    "balance": 0,
    "created": 1656122423,
    "currency": null,
    "default_source": null,
    "delinquent": false,
    "description": "Laser (work)",
    "discount": null,
    "email": "Valerie.Thompson89@gmail.com",
    "invoice_prefix": "0E84339A",
    "invoice_settings": {
      "custom_fields": null,
      "default_payment_method": "pm_1LEOINHHqepMFuCXsmlpIsAS",
      "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_1656122422",
  "createdAt": "2022-06-25T02:00:23.230Z",
  "updatedAt": "2022-06-25T02:00:26.059Z"
}

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

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

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/administrator/subscriptions/customer', 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 TestHelper.createOwner()
    const user = await TestStripeAccounts.createUserWithPaymentMethod()
    const req = TestHelper.createRequest('/api/administrator/subscriptions/customer')
    req.account = administrator.account
    req.session = administrator.session
    try {
      await req.get()
    } catch (error) {
      cachedResponses.missing = error.message
    }
    const req2 = TestHelper.createRequest('/api/administrator/subscriptions/customer')
    req2.account = administrator.account
    req2.session = administrator.session
    try {
      await req2.get()
    } catch (error) {
      cachedResponses.invalid = error.message
    }
    const req3 = TestHelper.createRequest(`/api/administrator/subscriptions/customer?customerid=${user.customer.customerid}`)
    req3.account = administrator.account
    req3.session = administrator.session
    req3.filename = __filename
    req3.saveResponse = true
    cachedResponses.returns = await req3.get()
    cachedResponses.finished = true
  }

  describe('exceptions', () => {
    describe('invalid-customerid', () => {
      it('missing querystring customerid', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.missing
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })

      it('invalid querystring customerid', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalid
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const customer = cachedResponses.returns
      assert.strictEqual(customer.object, 'customer')
    })
  })
})