Stripe Subscriptions module API explorer

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

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

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

Returns object

{
  "paymentintentid": "pi_3LEOkXHHqepMFuCX0lvNHCtd",
  "object": "paymentintent",
  "stripeObject": {
    "id": "pi_3LEOkXHHqepMFuCX0lvNHCtd",
    "object": "payment_intent",
    "amount": 10000,
    "amount_capturable": 0,
    "amount_details": {
      "tip": {}
    },
    "amount_received": 0,
    "application": null,
    "application_fee_amount": null,
    "automatic_payment_methods": null,
    "canceled_at": 1656124169,
    "cancellation_reason": null,
    "capture_method": "automatic",
    "charges": {
      "object": "list",
      "data": [],
      "has_more": false,
      "total_count": 0,
      "url": "/v1/charges?payment_intent=pi_3LEOkXHHqepMFuCX0lvNHCtd"
    },
    "client_secret": "pi_3LEOkXHHqepMFuCX0lvNHCtd_secret_30Dm5qjRUq4UgHF1YFlMY5vUU",
    "confirmation_method": "automatic",
    "created": 1656124169,
    "currency": "usd",
    "customer": "cus_LwHJLCub0iZTrg",
    "description": null,
    "invoice": null,
    "last_payment_error": null,
    "livemode": false,
    "metadata": {},
    "next_action": null,
    "on_behalf_of": null,
    "payment_method": null,
    "payment_method_options": {
      "card": {
        "installments": null,
        "mandate_options": null,
        "network": null,
        "request_three_d_secure": "automatic"
      }
    },
    "payment_method_types": [
      "card"
    ],
    "processing": null,
    "receipt_email": null,
    "review": null,
    "setup_future_usage": null,
    "shipping": null,
    "source": null,
    "statement_descriptor": null,
    "statement_descriptor_suffix": null,
    "status": "canceled",
    "transfer_data": null,
    "transfer_group": null
  },
  "accountid": "acct_be0662423ae77e09",
  "customerid": "cus_LwHJLCub0iZTrg",
  "paymentmethodid": "pm_1LEOkUHHqepMFuCXyggxcKOW",
  "subscriptionid": null,
  "invoiceid": null,
  "status": "requires_payment_method",
  "appid": "tests_1656124165",
  "createdAt": "2022-06-25T02:29:29.282Z",
  "updatedAt": "2022-06-25T02:29:30.085Z"
}

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

NodeJS source (view on github)

const dashboard = require('@layeredapps/dashboard')
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.customerid
    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 subscriptions.Storage.PaymentIntent.update({
      stripeObject: paymentIntentNow
    }, {
      where: {
        paymentintentid: req.query.paymentintentid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.StorageCache.remove(req.query.paymentintentid)
    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')

describe('/api/user/subscriptions/set-payment-intent-canceled', function () {
  before(TestHelper.disableMetrics)
  after(TestHelper.enableMetrics)
  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.paymentmethodid
        })
        const user2 = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-intent-canceled?paymentintentid=${user.paymentIntent.paymentintentid}`)
        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.paymentmethodid
      })
      const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-intent-canceled?paymentintentid=${user.paymentIntent.paymentintentid}`)
      req.account = user.account
      req.session = user.session
      req.filename = __filename
      req.saveResponse = true
      const paymentIntentNow = await req.patch()
      assert.strictEqual(paymentIntentNow.stripeObject.status, 'canceled')
    })
  })
})