Stripe Subscriptions module API explorer

/api/administrator/subscriptions/update-coupon (PATCH)

await global.api.administrator.subscriptions.UpdateCoupon.patch(req)

Returns object

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 querystring couponid
invalid-couponid missing querystring couponid
invalid-coupon ineligible querystring coupon is unpublished
invalid posted name
invalid-name missing posted name

Receives

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

Field Value Required Type
name string required POST

NodeJS source (edit on github)

If you see a problem with the source submit a pull request on Github.

const stripeCache = require('../../../../stripe-cache.js')

module.exports = {
  patch: async (req) => {
    if (!req.query || !req.query.couponid) {
      throw new Error('invalid-couponid')
    }
    const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
    if (!coupon) {
      throw new Error('invalid-couponid')
    }
    if (coupon.metadata.unpublished) {
      throw new Error('invalid-coupon')
    }
    if (!req.body.name || !req.body.name.length) {
      throw new Error('invalid-name')
    }
    const updateInfo = {
      name: req.body.name
    }
    const couponNow = await stripeCache.execute('coupons', 'update', req.query.couponid, updateInfo, req.stripeKey)
    await stripeCache.update(couponNow)
    return couponNow
  }
}

Test source (edit on github)

Tests perform real HTTP requests against a running Dashboard server.

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

describe('/api/administrator/subscriptions/update-coupon', () => {
  describe('exceptions', () => {
    describe('invalid-couponid', () => {
      it('missing querystring couponid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/update-coupon')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'name'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-couponid')
      })

      it('invalid querystring couponid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/update-coupon?couponid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'name'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-couponid')
      })
    })

    describe('invalid-coupon', () => {
      it('ineligible querystring coupon is unpublished', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createCoupon(administrator, {
          published: 'true',
          unpublished: 'true',
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: 'new-name',
          statement_descriptor: 'new-description',
          unit_label: 'new-thing'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-coupon')
      })
    })

    describe('invalid-name', () => {
      it('missing posted name', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: ''
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-name')
      })

      it('invalid posted name', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          name: ''
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-name')
      })
    })
  })

  describe('receives', () => {
    it('required posted name', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createCoupon(administrator, {
        published: 'true',
        percent_off: '25',
        duration: 'repeating',
        duration_in_months: '3'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.id}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'testing'
      }
      const couponNow = await req.patch(req)
      assert.strictEqual(couponNow.name, 'testing')
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      await TestHelper.createCoupon(administrator, {
        published: 'true',
        percent_off: '25',
        duration: 'repeating',
        duration_in_months: '3'
      })
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.id}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        name: 'new-name'
      }
      req.filename = __filename
      req.saveResponse = true
      const coupon = await req.patch()
      assert.strictEqual(coupon.object, 'coupon')
    })
  })
})