Stripe Subscriptions module API explorer

/api/administrator/subscriptions/create-refund (POST)

await global.api.administrator.subscriptions.CreateRefund.post(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 chargeid
invalid-chargeid missing querystring chargeid
invalid posted amount
invalid posted amount exceeds charge
invalid posted amount is negative
invalid-amount missing posted amount

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 = {
  post: async (req) => {
    if (!req.query || !req.query.chargeid) {
      throw new Error('invalid-chargeid')
    }
    if (!req.body || !req.body.amount) {
      throw new Error('invalid-amount')
    }
    const charge = await global.api.administrator.subscriptions.Charge.get(req)
    if (!charge) {
      throw new Error('invalid-chargeid')
    }
    const balance = charge.amount - (charge.amount_refunded || 0)
    if (charge.refunded || !charge.paid || charge.amount === 0 || balance === 0) {
      throw new Error('invalid-charge')
    }
    try {
      req.body.amount = parseInt(req.body.amount, 10)
      if (!req.body.amount || req.body.amount < 0 || req.body.amount > balance) {
        throw new Error('invalid-amount')
      }
    } catch (error) {
      throw new Error('invalid-amount')
    }
    const refundInfo = {
      charge: req.query.chargeid,
      amount: req.body.amount,
      reason: 'requested_by_customer',
      metadata: {
        appid: req.appid
      }
    }
    const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
    await stripeCache.delete(req.query.chargeid, req.stripeKey)
    return refund
  }
}

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')
const DashboardTestHelper = require('@userdashboard/dashboard/test-helper.js')

describe('/api/administrator/subscriptions/create-refund', function () {
  this.timeout(60 * 60 * 1000)
  const cachedResponses = {}
  after(TestHelper.deleteOldWebhooks)
  before(async () => {
    await DashboardTestHelper.setupBeforeEach()
    await TestHelper.setupBeforeEach()
    await TestHelper.setupWebhook()
    const administrator = await TestStripeAccounts.createOwnerWithPlan()
    const user = await TestStripeAccounts.createUserWithPaymentMethod()
    await TestHelper.createSubscription(user, administrator.plan.id)
    const req = TestHelper.createRequest(`/api/administrator/subscriptions/create-refund?chargeid=${user.charge.id}`)
    req.account = administrator.account
    req.session = administrator.session
    req.body = {
      amount: ''
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.missingAmount = error.message
    }
    req.body.amount = 'invalid'
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidAmount = error.message
    }
    req.body.amount = '-7'
    try {
      await req.post()
    } catch (error) {
      cachedResponses.negativeAmount = error.message
    }
    req.body.amount = administrator.plan.amount * 2
    try {
      await req.post()
    } catch (error) {
      cachedResponses.excessiveAmount = error.message
    }
    req.body.amount = administrator.plan.amount
    req.filename = __filename
    req.saveResponse = true
    cachedResponses.returns = await req.post()
  })
  describe('exceptions', () => {
    describe('invalid-chargeid', () => {
      it('missing querystring chargeid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-refund')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          amount: '1000'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-chargeid')
      })

      it('invalid querystring chargeid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/create-refund?chargeid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        req.body = {
          amount: '1000'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-chargeid')
      })
    })

    describe('invalid-amount', () => {
      it('missing posted amount', async () => {
        const errorMessage = cachedResponses.missingAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount', async () => {
        const errorMessage = cachedResponses.invalidAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount is negative', async () => {
        const errorMessage = cachedResponses.negativeAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount exceeds charge', async () => {
        const errorMessage = cachedResponses.excessiveAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })
    })
  })

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