Stripe Subscriptions module API explorer

/api/user/subscriptions/create-refund-request (POST)

await global.api.user.subscriptions.CreateRefundRequest.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
ineligible querystring charge has refund request
invalid-charge ineligible querystring charge is refunded
invalid-account ineligible accessing account

NodeJS source (edit on github)

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

const dashboard = require('@userdashboard/dashboard')
const subscriptions = require('../../../../../index.js')
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.reason) {
      throw new Error('invalid-reason')
    }
    if (!req.body.reason.length || req.body.reason.length > 200) {
      throw new Error('invalid-reason-length')
    }
    const charge = await global.api.user.subscriptions.Charge.get(req)
    if (!charge.amount || !charge.paid || charge.refunded) {
      throw new Error('invalid-charge')
    }
    if (charge.metadata.refundRequested) {
      throw new Error('invalid-charge')
    }
    const chargeInfo = {
      metadata: {
        refundRequested: dashboard.Timestamp.now,
        refundReason: req.body.reason
      }
    }
    const chargeNow = await stripeCache.execute('charges', 'update', req.query.chargeid, chargeInfo, req.stripeKey)
    await subscriptions.StorageList.add(`${req.appid}/refundRequests`, charge.id)
    await stripeCache.update(chargeNow)
    return chargeNow
  }
}

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/user/subscriptions/create-refund-request', function () {
  this.timeout(60 * 60 * 1000)
  afterEach(TestHelper.deleteOldWebhooks)
  beforeEach(TestHelper.setupWebhook)
  describe('exceptions', () => {
    describe('invalid-chargeid', () => {
      it('missing querystring chargeid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-refund-request')
        req.account = user.account
        req.session = user.session
        req.body = {
          reason: 'refund plz'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-chargeid')
      })

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

    describe('invalid-charge', () => {
      it('ineligible querystring charge is refunded', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        await TestHelper.createRefund(administrator, user.charge.id)
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          reason: 'refund plz'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-charge')
      })

      it('ineligible querystring charge has refund request', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        await TestHelper.requestRefund(user, user.charge.id)
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          reason: 'refund plz'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-charge')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        const user2 = await TestHelper.createUser()
        await TestHelper.createCustomer(user2, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.id}`)
        req.account = user2.account
        req.session = user2.session
        req.body = {
          reason: 'refund plz'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestStripeAccounts.createOwnerWithPlan()
      const user = await TestStripeAccounts.createUserWithPaymentMethod()
      await TestHelper.createSubscription(user, administrator.plan.id)
      const req = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.id}`)
      req.account = user.account
      req.session = user.session
      req.body = {
        reason: 'refund plz'
      }
      req.filename = __filename
      req.saveResponse = true
      const charge = await req.post()
      assert.strictEqual(charge.object, 'charge')
    })
  })
})