Stripe Subscriptions module API explorer

/api/administrator/subscriptions/payment-method (GET)

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

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

Returns object

{
  "paymentmethodid": "pm_1LEOLgHHqepMFuCXe2uKX8bg",
  "object": "paymentmethod",
  "accountid": "acct_3913d804a909a02d",
  "customerid": "cus_LwGt1AoKqjrb0G",
  "stripeObject": {
    "id": "pm_1LEOLgHHqepMFuCXe2uKX8bg",
    "object": "payment_method",
    "billing_details": {
      "address": {
        "city": "New York",
        "country": "US",
        "line1": "285 Fulton St",
        "line2": "Apt 893",
        "postal_code": "10007",
        "state": "NY"
      },
      "email": null,
      "name": "Joseph Rolfson",
      "phone": null
    },
    "card": {
      "brand": "visa",
      "checks": {
        "address_line1_check": "pass",
        "address_postal_code_check": "pass",
        "cvc_check": "pass"
      },
      "country": "US",
      "exp_month": 1,
      "exp_year": 2023,
      "fingerprint": "IRcdqfBUCskmPkNV",
      "funding": "credit",
      "generated_from": null,
      "last4": "1111",
      "networks": {
        "available": [
          "visa"
        ],
        "preferred": null
      },
      "three_d_secure_usage": {
        "supported": true
      },
      "wallet": null
    },
    "created": 1656122628,
    "customer": "cus_LwGt1AoKqjrb0G",
    "livemode": false,
    "metadata": {},
    "type": "card"
  },
  "appid": "tests_1656122627",
  "createdAt": "2022-06-25T02:03:49.278Z",
  "updatedAt": "2022-06-25T02:03:50.333Z"
}

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

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

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/payment-method', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  describe('exceptions', () => {
    describe('invalid-paymentmethodid', () => {
      it('missing querystring paymentmethodid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/payment-method')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
      })

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

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