Stripe Subscriptions module API explorer

/api/user/subscriptions/refund (GET)

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

await global.api.user.subscriptions.Refund.get(req)

Returns object

{
  "refundid": "re_3LEOi9HHqepMFuCX1lIKcKkk",
  "object": "refund",
  "stripeObject": {
    "id": "re_3LEOi9HHqepMFuCX1lIKcKkk",
    "object": "refund",
    "amount": 1000,
    "balance_transaction": "txn_3LEOi9HHqepMFuCX16ZK0H2z",
    "charge": "ch_3LEOi9HHqepMFuCX17KUCLdv",
    "created": 1656124025,
    "currency": "usd",
    "metadata": {},
    "payment_intent": "pi_3LEOi9HHqepMFuCX1nudTLjU",
    "reason": "requested_by_customer",
    "receipt_number": null,
    "source_transfer_reversal": null,
    "status": "succeeded",
    "transfer_reversal": null
  },
  "accountid": "acct_37b1f288ba80fa70",
  "subscriptionid": null,
  "customerid": null,
  "invoiceid": "in_1LEOi9HHqepMFuCXMllnM5VZ",
  "productid": null,
  "paymentmethodid": null,
  "appid": "tests_1656124016",
  "createdAt": "2022-06-25T02:27:06.395Z",
  "updatedAt": "2022-06-25T02:27:06.396Z"
}

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-account ineligible accessing account
invalid-refundid missing querystring refundid
invalid querystring refundid

NodeJS source (view on github)

const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')

module.exports = {
  get: async (req) => {
    if (!req.query || !req.query.refundid) {
      throw new Error('invalid-refundid')
    }
    let refund = await dashboard.StorageCache.get(req.query.refundid)
    if (!refund) {
      const refundInfo = await subscriptions.Storage.Refund.findOne({
        where: {
          refundid: req.query.refundid,
          appid: req.appid || global.appid
        }
      })
      if (!refundInfo) {
        throw new Error('invalid-refundid')
      }
      if (refundInfo.dataValues.accountid !== req.account.accountid) {
        throw new Error('invalid-account')
      }
      refund = {}
      for (const field of refundInfo._options.attributes) {
        refund[field] = refundInfo.get(field)
      }
      await dashboard.StorageCache.set(req.query.refundid, refund)
    }
    return refund
  }
}

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/user/subscriptions/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)
    await TestHelper.createRefund(administrator, user.charge.chargeid)
    const user2 = await TestHelper.createUser()
    // invalid account
    const req = TestHelper.createRequest(`/api/user/subscriptions/refund?refundid=${administrator.refund.refundid}`)
    req.account = user2.account
    req.session = user2.session
    try {
      await req.get()
    } catch (error) {
      cachedResponses.invalidAccount = error.message
    }
    // returns
    const req2 = TestHelper.createRequest(`/api/user/subscriptions/refund?refundid=${administrator.refund.refundid}`)
    req2.account = user.account
    req2.session = user.session
    req2.filename = __filename
    req2.saveResponse = true
    cachedResponses.returns = await req2.get()
    cachedResponses.finished = true
  }

  describe('exceptions', () => {
    describe('invalid-refundid', () => {
      it('missing querystring refundid', async function () {
        await bundledData(this.test.currentRetry())
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/refund?refundid=invalid')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-refundid')
      })

      it('invalid querystring refundid', async function () {
        await bundledData(this.test.currentRetry())
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/refund?refundid=invalid')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-refundid')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidAccount
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

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