Stripe Subscriptions module API explorer

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

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

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

Returns

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
ineligible posted payment method
invalid-customerid missing querystring customerid
invalid querystring customerid
invalid-paymentmethodid missing posted paymentmethodid
invalid posted paymentmethodid
object undefined

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 paymentIntent = await stripeCache.execute('paymentIntents', 'create', {
      amount: req.body.amount,
      currency: req.body.currency,
      payment_method_types: ['card'],
      customer: req.query.customerid
    }, req.stripeKey)
    req.query.paymentintentid = paymentIntent.id
    await subscriptions.Storage.PaymentIntent.create({
      appid: req.appid || global.appid,
      paymentintentid: paymentIntent.id,
      accountid: customer.accountid,
      customerid: customer.customerid,
      paymentmethodid: req.body.paymentmethodid,
      status: paymentIntent.status,
      stripeObject: paymentIntent
    })
    return global.api.user.subscriptions.PaymentIntent.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-payment-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()
    const user = await TestStripeAccounts.createUserWithPaymentMethod()
    const user2 = await TestHelper.createUser()
    // invalid account
    const req = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.customerid}`)
    req.account = user2.account
    req.session = user2.session
    req.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid,
      amount: '10000',
      currency: 'usd'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidAccount = error.message
    }
    const user3 = await TestStripeAccounts.createUserWithPaymentMethod()
    const req2 = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.customerid}`)
    req2.account = user3.account
    req2.session = user3.session
    req2.body = {
      paymentmethodid: user3.paymentMethod.id,
      amount: '10000',
      currency: 'usd'
    }
    try {
      await req2.post()
    } catch (error) {
      cachedResponses.invalidPaymentMethodAccount = error.message
    }
    // invalid payment method
    const req3 = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.customerid}`)
    req3.account = user.account
    req3.session = user.session
    req3.body = {
      paymentmethodid: ''
    }
    try {
      await req3.post()
    } catch (error) {
      cachedResponses.missingPaymentMethod = error.message
    }
    req3.body = {
      paymentmethodid: 'invalid'
    }
    try {
      await req3.post()
    } catch (error) {
      cachedResponses.invalidPaymentMethod = error.message
    }
    // returns
    req3.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid,
      amount: '10000',
      currency: 'usd'
    }
    cachedResponses.returns = await req3.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-payment-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-payment-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')
      })

      it('ineligible posted payment method', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidPaymentMethodAccount
        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('object', () => {
    it('object', async () => {
      const paymentIntent = cachedResponses.returns
      assert.strictEqual(paymentIntent.object, 'paymentintent')
    })
  })
})