Stripe Subscriptions module API explorer

/api/user/subscriptions/set-payment-intent-canceled (PATCH)

await global.api.user.subscriptions.SetPaymentIntentCanceled.patch(req)

Returns object

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 paymentintentid
invalid-paymentintentid missing querystring paymentintentid
invalid-account ineligible accessing account

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 = {
  patch: async (req) => {
    if (!req.query || !req.query.paymentintentid) {
      throw new Error('invalid-paymentintentid')
    }
    const paymentIntent = await global.api.user.subscriptions.PaymentIntent.get(req)
    if (!paymentIntent) {
      throw new Error('invalid-paymentintentid')
    }
    req.query.customerid = paymentIntent.customer
    const customer = await global.api.user.subscriptions.Customer.get(req)
    if (!customer) {
      throw new Error('invalid-paymentintentid')
    }
    const paymentIntentNow = await stripeCache.execute('paymentIntents', 'cancel', req.query.paymentintentid, req.stripeKey)
    await stripeCache.delete(req.query.paymentintentid, req.stripeKey)
    try {
      await subscriptions.StorageList.remove(`${req.appid}/paymentIntents`, req.query.paymentintentid)
    } catch (error) {
    }
    try {
      await subscriptions.StorageList.remove(`${req.appid}/account/paymentIntents/${req.account.accountid}`, req.query.paymentintentid)
    } catch (error) {
    }
    try {
      await subscriptions.Storage.delete(`${req.appid}/map/accountid/paymentintentid/${req.query.paymentintentid}`)
    } catch (error) {
    }
    return paymentIntentNow
  }
}

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/set-payment-intent-canceled', () => {
  describe('exceptions', () => {
    describe('invalid-paymentintentid', () => {
      it('missing querystring paymentintentid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/set-payment-intent-canceled')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-paymentintentid')
      })

      it('invalid querystring paymentintentid', async () => {
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest('/api/user/subscriptions/set-payment-intent-canceled?paymentintentid=invalid')
        req.account = user.account
        req.session = user.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-paymentintentid')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async () => {
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createPaymentIntent(user, {
          amount: '10000',
          currency: 'usd',
          paymentmethodid: user.paymentMethod.id
        })
        const user2 = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-intent-canceled?paymentintentid=${user.paymentIntent.id}`)
        req.account = user2.account
        req.session = user2.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const user = await TestStripeAccounts.createUserWithPaymentMethod()
      await TestHelper.createPaymentIntent(user, {
        amount: '10000',
        currency: 'usd',
        paymentmethodid: user.paymentMethod.id
      })
      const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-intent-canceled?paymentintentid=${user.paymentIntent.id}`)
      req.account = user.account
      req.session = user.session
      req.filename = __filename
      req.saveResponse = true
      const paymentIntentNow = await req.patch()
      assert.strictEqual(paymentIntentNow.status, 'canceled')
    })
  })
})