Stripe Subscriptions module API explorer

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

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

NodeJS source (edit on github)

If you see a problem with the source submit a pull request 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,
      metadata: {
        appid: req.appid,
        accountid: req.account.accountid
      }
    }, req.stripeKey)
    await stripeCache.update(paymentIntent)
    await subscriptions.StorageList.addMany({
      [`${req.appid}/paymentIntents`]: paymentIntent.id,
      [`${req.appid}/customer/paymentIntents/${req.query.customerid}`]: paymentIntent.id,
      [`${req.appid}/account/paymentIntents/${req.account.accountid}`]: paymentIntent.id
    })
    return paymentIntent
  }
}

Test source (edit on github)

Tests perform real HTTP requests against a running Dashboard server.

/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')

describe('/api/user/subscriptions/create-payment-intent', () => {
  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 () => {
        global.stripeJS = false
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        const user2 = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.id}`)
        req.account = user2.account
        req.session = user2.session
        req.body = {
          paymentmethodid: user.paymentMethod.id,
          amount: '10000',
          currency: 'usd'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })

      it('ineligible posted payment method', async () => {
        global.stripeJS = false
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const user2 = await TestStripeAccounts.createUserWithPaymentMethod()
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.id}`)
        req.account = user2.account
        req.session = user2.session
        req.body = {
          paymentmethodid: user2.paymentMethod.id,
          amount: '10000',
          currency: 'usd'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })

    describe('invalid-paymentmethodid', () => {
      it('missing posted paymentmethodid', async () => {
        global.stripeJS = false
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          paymentmethodid: ''
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
      })

      it('invalid posted paymentmethodid', async () => {
        global.stripeJS = false
        const user = await TestHelper.createUser()
        await TestHelper.createCustomer(user, {
          email: user.profile.contactEmail,
          description: user.profile.firstName,
          country: 'US'
        })
        const req = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.id}`)
        req.account = user.account
        req.session = user.session
        req.body = {
          paymentmethodid: 'invalid'
        }
        let errorMessage
        try {
          await req.post()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
      })
    })
  })

  describe('object', () => {
    it('object', async () => {
      global.stripeJS = false
      const user = await TestStripeAccounts.createUserWithPaymentMethod()
      const req = TestHelper.createRequest(`/api/user/subscriptions/create-payment-intent?customerid=${user.customer.id}`)
      req.account = user.account
      req.session = user.session
      req.body = {
        paymentmethodid: user.paymentMethod.id,
        amount: '10000',
        currency: 'usd'
      }
      const paymentIntent = await req.post()
      assert.strictEqual(paymentIntent.object, 'payment_intent')
    })
  })
})