Stripe Subscriptions module API explorer

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

Account information like email addresses is generated with faker-js it is not real user information.

await global.api.administrator.subscriptions.CreateRefund.post(req)

Returns object

{
  "refundid": "re_3LEOI0HHqepMFuCX1MSmw5PT",
  "object": "refund",
  "stripeObject": {
    "id": "re_3LEOI0HHqepMFuCX1MSmw5PT",
    "object": "refund",
    "amount": 1000,
    "balance_transaction": "txn_3LEOI0HHqepMFuCX1j0ojQAi",
    "charge": "ch_3LEOI0HHqepMFuCX1JMwlglM",
    "created": 1656122405,
    "currency": "usd",
    "metadata": {},
    "payment_intent": "pi_3LEOI0HHqepMFuCX1dp4ji2O",
    "reason": "requested_by_customer",
    "receipt_number": null,
    "source_transfer_reversal": null,
    "status": "succeeded",
    "transfer_reversal": null
  },
  "accountid": "acct_f67c760a7c677fc1",
  "subscriptionid": null,
  "customerid": null,
  "invoiceid": "in_1LEOI0HHqepMFuCXi617k9eR",
  "productid": null,
  "paymentmethodid": null,
  "appid": "tests_1656122395",
  "createdAt": "2022-06-25T02:00:05.977Z",
  "updatedAt": "2022-06-25T02:00:05.977Z"
}

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-amount missing posted amount
invalid posted amount
invalid posted amount is negative
invalid posted amount exceeds charge
invalid-chargeid missing querystring chargeid
invalid querystring chargeid

NodeJS source (view on github)

const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.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.stripeObject.amount - (charge.stripeObject.amount_refunded || 0)
    if (charge.refunded || !charge.stripeObject.paid || charge.stripeObject.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')
    }
    req.query.invoiceid = charge.stripeObject.invoice
    const invoice = await global.api.administrator.subscriptions.Invoice.get(req)
    const refundInfo = {
      charge: req.query.chargeid,
      amount: req.body.amount,
      reason: 'requested_by_customer'
    }
    const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
    await subscriptions.Storage.Refund.create({
      appid: req.appid || global.appid,
      accountid: charge.accountid,
      refundid: refund.id,
      chargeid: charge.id,
      customerid: charge.stripeObject.customer.id,
      invoiceid: charge.stripeObject.invoice,
      subscriptionid: invoice.stripeObject.subscriptionid,
      productid: invoice.stripeObject.productid,
      stripeObject: refund
    })
    const chargeNow = await stripeCache.retrieve(req.query.chargeid, 'charges', req.stripeKey)
    await subscriptions.Storage.Charge.update({
      stripeObject: chargeNow
    }, {
      where: {
        chargeid: req.query.chargeid,
        appid: req.appid || global.appid
      }
    })
    req.query.refundid = refund.id
    return global.api.administrator.subscriptions.Refund.get(req)
  }
}

Test source (view on github)

/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')

describe('/api/administrator/subscriptions/create-refund', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  let cachedResponses
  async function bundledData (retryNumber) {
    if (retryNumber > 0) {
      cachedResponses = {}
    }
    if (cachedResponses && cachedResponses.finished) {
      return
    }
    cachedResponses = {}
    await TestHelper.setupBefore()
    await DashboardTestHelper.setupBeforeEach()
    await TestHelper.setupBeforeEach()
    const administrator = await TestStripeAccounts.createOwnerWithPrice()
    const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
    const req = TestHelper.createRequest(`/api/administrator/subscriptions/create-refund?chargeid=${user.charge.chargeid}`)
    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 = 10000000
    try {
      await req.post()
    } catch (error) {
      cachedResponses.excessiveAmount = error.message
    }
    req.body.amount = administrator.price.stripeObject.unit_amount
    req.filename = __filename
    req.saveResponse = true
    cachedResponses.returns = await req.post()
    cachedResponses.finished = true
  }

  describe('exceptions', () => {
    describe('invalid-chargeid', () => {
      it('missing querystring chargeid', async function () {
        await bundledData(this.test.currentRetry())
        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 function () {
        await bundledData(this.test.currentRetry())
        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 function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.missingAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount is negative', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.negativeAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })

      it('invalid posted amount exceeds charge', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.excessiveAmount
        assert.strictEqual(errorMessage, 'invalid-amount')
      })
    })
  })

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