Stripe Subscriptions module API explorer

/api/administrator/subscriptions/set-subscription-coupon (PATCH)

await global.api.administrator.subscriptions.SetSubscriptionCoupon.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 subscriptionid
invalid-subscriptionid missing querystring subscriptionid
ineligible subscription has coupon
invalid-subscription ineligible subscription is canceling
invalid posted couponid
invalid-couponid missing posted couponid
invalid-coupon ineligible posted coupon is not published
ineligible 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.subscriptionid) {
      throw new Error('invalid-subscriptionid')
    }
    const subscriptionExists = await subscriptions.StorageList.exists(`${req.appid}/subscriptions`, req.query.subscriptionid)
    if (!subscriptionExists) {
      throw new Error('invalid-subscriptionid')
    }
    if (!req.body || !req.body.couponid) {
      throw new Error('invalid-couponid')
    }
    const couponExists = await subscriptions.StorageList.exists(`${req.appid}/coupons`, req.body.couponid)
    if (!couponExists) {
      throw new Error('invalid-couponid')
    }
    const subscription = await global.api.administrator.subscriptions.Subscription.get(req)
    if (!subscription) {
      throw new Error('invalid-subscriptionid')
    }
    if (subscription.discount && subscription.discount.coupon && subscription.discount.coupon.id) {
      throw new Error('invalid-subscription')
    }
    if ((subscription.status !== 'active' && subscription.status !== 'trialing') || subscription.cancel_at_period_end) {
      throw new Error('invalid-subscription')
    }
    req.query.couponid = req.body.couponid
    const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
    if (!coupon) {
      throw new Error('invalid-couponid')
    }
    if (!coupon.metadata.published || coupon.metadata.unpublished) {
      throw new Error('invalid-coupon')
    }
    const subscriptionInfo = {
      coupon: req.body.couponid
    }
    const subscriptionNow = await stripeCache.execute('subscriptions', 'update', req.query.subscriptionid, subscriptionInfo, req.stripeKey)
    await stripeCache.update(subscriptionNow)
    await subscriptions.StorageList.addMany({
      [`${req.appid}/subscription/coupons/${req.query.subscriptionid}`]: req.body.couponid,
      [`${req.appid}/coupon/subscriptions/${req.body.couponid}`]: req.query.subscriptionid,
      [`${req.appid}/account/coupons/${subscription.metadata.accountid}`]: req.body.couponid,
      [`${req.appid}/coupon/accounts/${req.body.couponid}`]: subscription.metadata.accountid
    })
    return subscriptionNow
  }
}

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')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')

describe('/api/administrator/subscriptions/set-subscription-coupon', function () {
  this.timeout(60 * 60 * 1000)
  afterEach(TestHelper.deleteOldWebhooks)
  beforeEach(TestHelper.setupWebhook)
  describe('exceptions', () => {
    describe('invalid-subscriptionid', () => {
      it('missing querystring subscriptionid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/set-subscription-coupon')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          couponid: 'fake'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-subscriptionid')
      })

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

    describe('invalid-subscription', () => {
      it('ineligible subscription is canceling', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        await TestHelper.cancelSubscription(user)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          couponid: administrator.coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-subscription')
      })

      it('ineligible subscription has coupon', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        await TestHelper.createCoupon(administrator, {
          published: 'true',
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        await TestHelper.createSubscriptionDiscount(administrator, user.subscription, administrator.coupon)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          couponid: administrator.coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-subscription')
      })
    })

    describe('invalid-couponid', () => {
      it('missing posted couponid', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
        req.account = administrator.account
        req.session = administrator.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 administrator = await TestStripeAccounts.createOwnerWithPlan()
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          couponid: 'fake'
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-couponid')
      })
    })

    describe('invalid-coupon', () => {
      it('ineligible posted coupon is not published', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        await TestHelper.createCoupon(administrator, {
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          couponid: administrator.coupon.id
        }
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-coupon')
      })

      it('ineligible posted coupon is unpublished', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        await TestHelper.createCoupon(administrator, {
          published: 'true',
          unpublished: 'true',
          percent_off: '25',
          duration: 'repeating',
          duration_in_months: '3'
        })
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          couponid: administrator.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 TestStripeAccounts.createOwnerWithPlan()
      await TestHelper.createCoupon(administrator, {
        published: 'true',
        percent_off: '25',
        duration: 'repeating',
        duration_in_months: '3'
      })
      const user = await TestStripeAccounts.createUserWithPaymentMethod()
      await TestHelper.createSubscription(user, administrator.plan.id)
      await TestHelper.requestRefund(user, user.charge.id)
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-coupon?subscriptionid=${user.subscription.id}`)
      req.account = administrator.account
      req.session = administrator.session
      req.body = {
        couponid: administrator.coupon.id
      }
      req.filename = __filename
      req.saveResponse = true
      const subscriptionNow = await req.patch()
      assert.strictEqual(subscriptionNow.discount.coupon.id, administrator.coupon.id)
    })
  })
})