Stripe Subscriptions module API explorer

/api/administrator/subscriptions/coupons (GET)

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

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

Returns array

[
  {
    "couponid": "COUPON37",
    "object": "coupon",
    "stripeObject": {
      "id": "COUPON37",
      "object": "coupon",
      "amount_off": 37,
      "created": 1656122226,
      "currency": "usd",
      "duration": "repeating",
      "duration_in_months": 1,
      "livemode": false,
      "max_redemptions": null,
      "metadata": {},
      "name": "coupon name",
      "percent_off": null,
      "redeem_by": null,
      "times_redeemed": 0,
      "valid": true
    },
    "active": null,
    "appid": "tests_1656122225",
    "createdAt": "2022-06-25T01:57:06.569Z",
    "updatedAt": "2022-06-25T01:57:06.569Z"
  },
  {
    "couponid": "COUPON36",
    "object": "coupon",
    "stripeObject": {
      "id": "COUPON36",
      "object": "coupon",
      "amount_off": 36,
      "created": 1656122226,
      "currency": "usd",
      "duration": "repeating",
      "duration_in_months": 1,
      "livemode": false,
      "max_redemptions": 111,
      "metadata": {},
      "name": "coupon name",
      "percent_off": null,
      "redeem_by": null,
      "times_redeemed": 0,
      "valid": true
    },
    "active": null,
    "appid": "tests_1656122225",
    "createdAt": "2022-06-25T01:57:06.274Z",
    "updatedAt": "2022-06-25T01:57:06.647Z"
  },
  {
    "couponid": "COUPON35",
    "object": "coupon",
    "stripeObject": {
      "id": "COUPON35",
      "object": "coupon",
      "amount_off": null,
      "created": 1656122225,
      "currency": null,
      "duration": "repeating",
      "duration_in_months": 1,
      "livemode": false,
      "max_redemptions": 188,
      "metadata": {},
      "name": "coupon name",
      "percent_off": 35,
      "redeem_by": null,
      "times_redeemed": 0,
      "valid": true
    },
    "active": null,
    "appid": "tests_1656122225",
    "createdAt": "2022-06-25T01:57:05.954Z",
    "updatedAt": "2022-06-25T01:57:06.371Z"
  }
]

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
    }
    let couponids
    if (req.query.all) {
      couponids = await subscriptions.Storage.Coupon.findAll({
        where,
        attributes: ['couponid'],
        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
      couponids = await subscriptions.Storage.Coupon.findAll({
        where,
        attributes: ['couponid'],
        offset,
        limit,
        order: [
          ['createdAt', 'DESC']
        ]
      })
    }
    if (!couponids || !couponids.length) {
      return null
    }
    const items = []
    for (const couponInfo of couponids) {
      req.query.couponid = couponInfo.dataValues.couponid
      const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
      items.push(coupon)
    }
    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/coupons', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  let cachedResponses, cachedCoupons
  async function bundledData (retryNumber) {
    if (retryNumber > 0) {
      cachedResponses = {}
    }
    if (cachedResponses && cachedResponses.finished) {
      return
    }
    cachedResponses = {}
    cachedCoupons = []
    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++) {
      await TestHelper.createCoupon(administrator, {
        duration: 'repeating',
        duration_in_months: '1'
      })
      cachedCoupons.unshift(administrator.coupon.couponid)
    }
    const req1 = TestHelper.createRequest('/api/administrator/subscriptions/coupons?offset=1')
    req1.account = administrator.account
    req1.session = administrator.session
    cachedResponses.offset = await req1.get()
    const req2 = TestHelper.createRequest('/api/administrator/subscriptions/coupons?limit=1')
    req2.account = administrator.account
    req2.session = administrator.session
    cachedResponses.limit = await req2.get()
    const req3 = TestHelper.createRequest('/api/administrator/subscriptions/coupons?all=true')
    req3.account = administrator.account
    req3.session = administrator.session
    cachedResponses.all = await req3.get()
    const req4 = TestHelper.createRequest('/api/administrator/subscriptions/coupons')
    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 couponsNow = cachedResponses.offset
      for (let i = 0, len = global.pageSize; i < len; i++) {
        assert.strictEqual(couponsNow[i].couponid, cachedCoupons[offset + i])
      }
    })

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

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

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

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