Stripe Subscriptions module API explorer

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

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

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

Returns object

{
  "paymentmethodid": "pm_1LEOhLHHqepMFuCX4Pyb93Yp",
  "object": "paymentmethod",
  "accountid": "acct_7ca5efe63e9ff18f",
  "customerid": "cus_LwHFubP27SB3w4",
  "stripeObject": {
    "id": "pm_1LEOhLHHqepMFuCX4Pyb93Yp",
    "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": "Dorothy Daniel",
      "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": 1656123971,
    "customer": "cus_LwHFubP27SB3w4",
    "livemode": false,
    "metadata": {},
    "type": "card"
  },
  "appid": "tests_1656123970",
  "createdAt": "2022-06-25T02:26:12.814Z",
  "updatedAt": "2022-06-25T02:26:13.997Z"
}

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-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')
      }
      if (paymentMethodInfo.dataValues.accountid !== req.account.accountid) {
        throw new Error('invalid-account')
      }
      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')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')

describe('/api/user/subscriptions/payment-method', 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 user = await TestStripeAccounts.createUserWithPaymentMethod()
    const user2 = await TestHelper.createUser()
    // invalid account
    const req = TestHelper.createRequest(`/api/user/subscriptions/payment-method?paymentmethodid=${user.paymentMethod.paymentmethodid}`)
    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/payment-method?paymentmethodid=${user.paymentMethod.paymentmethodid}`)
    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-paymentmethodid', () => {
      it('missing querystring paymentmethodid', async function () {
        await bundledData(this.test.currentRetry())
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/payment-method')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
      })

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

    describe('invalid-account', () => {
      it('ineligible accessing account', async function () {
        await bundledData(this.test.currentRetry())
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        const user2 = await TestHelper.createUser()
        await TestHelper.createCustomer(user2, {
          email: user.profile.contactEmail,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/payment-method?paymentmethodid=${user.paymentMethod.paymentmethodid}`)
        req.account = user2.account
        req.session = user2.session
        let errorMessage
        try {
          await req.get()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

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