Stripe Subscriptions module API explorer

/api/user/subscriptions/create-setup-intent (POST)

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

await global.api.user.subscriptions.CreateSetupIntent.post(req)

Returns object

{
  "setupintentid": "seti_1LEOczHHqepMFuCXtUpiKM2n",
  "accountid": "acct_23d3c1137d972161",
  "customerid": "cus_LwHBXcaskMY057",
  "paymentmethodid": "pm_1LEOcwHHqepMFuCXllyARE2o",
  "object": "setupintent",
  "stripeObject": {
    "id": "seti_1LEOczHHqepMFuCXtUpiKM2n",
    "object": "setup_intent",
    "application": null,
    "cancellation_reason": null,
    "client_secret": "seti_1LEOczHHqepMFuCXtUpiKM2n_secret_LwHBghHJ8tyRcxBVfNM2hAXnC8J79EV",
    "created": 1656123701,
    "customer": "cus_LwHBXcaskMY057",
    "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_1LEOcwHHqepMFuCXllyARE2o",
    "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_1656123697",
  "createdAt": "2022-06-25T02:21:41.623Z",
  "updatedAt": "2022-06-25T02:21:41.623Z"
}

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-customerid missing querystring customerid
invalid querystring customerid
invalid-paymentmethodid missing posted paymentmethodid
invalid posted paymentmethodid

NodeJS source (view on github)

const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')

module.exports = {
  post: async (req) => {
    if (!req.query || !req.query.customerid) {
      throw new Error('invalid-customerid')
    }
    const customer = await global.api.user.subscriptions.Customer.get(req)
    if (!customer) {
      throw new Error('invalid-customerid')
    }
    if (!req.body || !req.body.paymentmethodid) {
      throw new Error('invalid-paymentmethodid')
    }
    req.query.paymentmethodid = req.body.paymentmethodid
    const paymentMethod = await global.api.user.subscriptions.PaymentMethod.get(req)
    if (!paymentMethod) {
      throw new Error('invalid-paymentmethodid')
    }
    const setupIntent = await stripeCache.execute('setupIntents', 'create', {
      payment_method_types: ['card'],
      customer: req.query.customerid,
      payment_method: req.body.paymentmethodid
    }, req.stripeKey)
    req.query.setupintentid = setupIntent.id
    await subscriptions.Storage.SetupIntent.create({
      appid: req.appid || global.appid,
      setupintentid: setupIntent.id,
      accountid: req.account.accountid,
      customerid: req.query.customerid,
      paymentmethodid: req.body.paymentmethodid,
      stripeObject: setupIntent
    })
    return global.api.user.subscriptions.SetupIntent.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/user/subscriptions/create-setup-intent', 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()
    global.stripeJS = false
    const user = await TestStripeAccounts.createUserWithPaymentMethod()
    const user2 = await TestHelper.createUser()
    // invalid account
    const req = TestHelper.createRequest(`/api/user/subscriptions/create-setup-intent?customerid=${user.customer.customerid}`)
    req.account = user2.account
    req.session = user2.session
    req.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidAccount = error.message
    }
    // invalid payment method
    const req2 = TestHelper.createRequest(`/api/user/subscriptions/create-setup-intent?customerid=${user.customer.customerid}`)
    req2.account = user.account
    req2.session = user.session
    req2.body = {
      paymentmethodid: ''
    }
    try {
      await req2.post()
    } catch (error) {
      cachedResponses.missingPaymentMethod = error.message
    }
    req2.body = {
      paymentmethodid: 'invalid'
    }
    try {
      await req2.post()
    } catch (error) {
      cachedResponses.invalidPaymentMethod = error.message
    }
    // returns
    req2.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid
    }
    req2.filename = __filename
    req2.saveResponse = true
    cachedResponses.returns = await req2.post()
    cachedResponses.finished = true
  }

  describe('exceptions', () => {
    describe('invalid-customerid', () => {
      it('missing querystring customerid', async () => {
        global.stripeJS = false
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-setup-intent')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })

      it('invalid querystring customerid', async () => {
        global.stripeJS = false
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/create-setup-intent?customerid=invalid')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })
    })

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

    describe('invalid-paymentmethodid', () => {
      it('missing posted paymentmethodid', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.missingPaymentMethod
        assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
      })

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

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