Stripe Subscriptions module API explorer

/api/administrator/subscriptions/setup-intent (GET)

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

await global.api.administrator.subscriptions.SetupIntent.get(req)

Returns object

{
  "setupintentid": "seti_1LEOULHHqepMFuCXGLyIAzRn",
  "accountid": "acct_1d6438199be52087",
  "customerid": "cus_LwH2T8GVBEUGFJ",
  "paymentmethodid": null,
  "object": "setupintent",
  "stripeObject": {
    "id": "seti_1LEOULHHqepMFuCXGLyIAzRn",
    "object": "setup_intent",
    "application": null,
    "cancellation_reason": null,
    "client_secret": "seti_1LEOULHHqepMFuCXGLyIAzRn_secret_LwH2aPGlNohcEGOYGmDCdKljdidDPty",
    "created": 1656123165,
    "customer": "cus_LwH2T8GVBEUGFJ",
    "description": null,
    "flow_directions": null,
    "last_setup_error": null,
    "latest_attempt": null,
    "livemode": false,
    "mandate": null,
    "metadata": {},
    "next_action": null,
    "on_behalf_of": null,
    "payment_method": "pm_1LEOUJHHqepMFuCXtoVLD1Gc",
    "payment_method_options": {
      "card": {
        "mandate_options": null,
        "network": null,
        "request_three_d_secure": "automatic"
      }
    },
    "payment_method_types": [
      "card"
    ],
    "single_use_mandate": null,
    "status": "requires_confirmation",
    "usage": "off_session"
  },
  "appid": "tests_1656123163",
  "createdAt": "2022-06-25T02:12:45.996Z",
  "updatedAt": "2022-06-25T02:12:46.675Z"
}

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-setupintentid missing querystring setupintentid
invalid querystring setupintentid

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.setupintentid) {
      throw new Error('invalid-setupintentid')
    }
    let setupIntent = await dashboard.StorageCache.get(req.query.setupintentid)
    if (!setupIntent) {
      const setupIntentInfo = await subscriptions.Storage.SetupIntent.findOne({
        where: {
          setupintentid: req.query.setupintentid,
          appid: req.appid || global.appid
        }
      })
      if (!setupIntentInfo) {
        throw new Error('invalid-setupintentid')
      }
      setupIntent = {}
      for (const field of setupIntentInfo._options.attributes) {
        setupIntent[field] = setupIntentInfo.get(field)
      }
      await dashboard.StorageCache.set(req.query.setupintentid, setupIntent)
    }
    return setupIntent
  }
}

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')

describe('/api/administrator/subscriptions/setup-intent', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-setupintentid', () => {
      it('missing querystring setupintentid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/setup-intent')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-setupintentid')
      })

      it('invalid querystring setupintentid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/setup-intent?setupintentid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-setupintentid')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestHelper.createOwner()
      const user = await TestStripeAccounts.createUserWithPaymentMethod()
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/setup-intent?setupintentid=${user.setupIntent.setupintentid}`)
      req.account = administrator.account
      req.session = administrator.session
      req.filename = __filename
      req.saveResponse = true
      const setupIntent = await req.get()
      assert.strictEqual(setupIntent.setupintentid, user.setupIntent.setupintentid)
    })
  })
})