Stripe Subscriptions module API explorer

/api/user/subscriptions/set-customer-coupon (PATCH)

await global.api.user.subscriptions.SetCustomerCoupon.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 customerid
invalid-customerid missing querystring customerid
invalid-customer invalid querystring customer has coupon
invalid-account ineligible account accessing
invalid posted couponid
invalid-couponid missing posted couponid
invalid-coupon invalid posted coupon is not published
invalid posted coupon is unpublished

NodeJS source (edit on github)

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

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

module.exports = {
  patch: async (req) => {
    if (!req.query || !req.query.customerid) {
      throw new Error('invalid-customerid')
    }
    if (!req.body || !req.body.couponid) {
      throw new Error('invalid-couponid')
    }
    const customer = await global.api.user.subscriptions.Customer.get(req)
    if (!customer) {
      throw new Error('invalid-customer')
    }
    if (customer.discount) {
      throw new Error('invalid-customer')
    }
    const couponExists = await subscriptions.StorageList.exists(`${req.appid}/coupons`, req.body.couponid)
    if (!couponExists) {
      throw new Error('invalid-couponid')
    }
    req.query.couponid = req.body.couponid
    const coupon = await global.api.user.subscriptions.PublishedCoupon.get(req)
    if (!coupon) {
      throw new Error('invalid-couponid')
    }
    const customerInfo = {
      coupon: req.body.couponid
    }
    const customerNow = await stripeCache.execute('customers', 'update', req.query.customerid, customerInfo, req.stripeKey)
    await stripeCache.update(customerNow)
    return customerNow
  }
}

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/user/subscriptions/set-customer-coupon', () => {
  describe('exceptions', () => {
    describe('invalid-customerid', () => {
      it('missing querystring customerid', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createCoupon(administrator, {
          published: 'true'
        })
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/set-customer-coupon')
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: administrator.coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })

      it('invalid querystring customerid', async () => {
        const administrator = await TestHelper.createOwner()
        await TestHelper.createCoupon(administrator, {
          published: 'true'
        })
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/set-customer-coupon?customerid=invalid')
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: administrator.coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })
    })

    describe('invalid-customer', () => {
      it('invalid querystring customer has coupon', async () => {
        const administrator = await TestHelper.createOwner()
        const coupon1 = await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25'
        })
        const coupon2 = await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25'
        })
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        user.customer = await TestHelper.createCustomerDiscount(administrator, user.customer, coupon1)
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: coupon2.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customer')
      })
    })

    describe('invalid-account', () => {
      it('ineligible account accessing', async () => {
        const administrator = await TestHelper.createOwner()
        const coupon = await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25'
        })
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const user2 = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
        req.account = user2.account
        req.session = user2.session
        req.body = {
          couponid: coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })

    describe('invalid-couponid', () => {
      it('missing posted couponid', async () => {
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: ''
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-couponid')
      })

      it('invalid posted couponid', async () => {
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: 'invalid'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-couponid')
      })
    })

    describe('invalid-coupon', () => {
      it('invalid posted coupon is not published', async () => {
        const administrator = await TestHelper.createOwner()
        const coupon = await TestHelper.createCoupon(administrator, {
        })
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-coupon')
      })

      it('invalid posted coupon is unpublished', async () => {
        const administrator = await TestHelper.createOwner()
        const coupon = await TestHelper.createCoupon(administrator, {
          published: 'true',
          unpublished: 'true'
        })
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          couponid: coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-coupon')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      const coupon = await TestHelper.createCoupon(administrator, {
        published: 'true'
      })
      const user = await TestHelper.createUser()
      await TestHelper.createCustomer(user, {
        email: user.profile.contactEmail,
        description: user.profile.firstName,
        country: 'US'
      })
      const req = TestHelper.createRequest(`/api/user/subscriptions/set-customer-coupon?customerid=${user.customer.id}`)
      req.account = user.account
      req.session = user.session
      req.body = {
        couponid: coupon.id
      }
      req.filename = __filename
      req.saveResponse = true
      const customerNow = await req.patch()
      assert.strictEqual(customerNow.discount.coupon.id, coupon.id)
    })
  })
})