Stripe Subscriptions module API explorer

/api/user/subscriptions/create-subscription (POST)

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

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

Returns object

{
  "subscriptionid": "sub_1LEOdDHHqepMFuCXbw2ID8aD",
  "object": "subscription",
  "stripeObject": {
    "id": "sub_1LEOdDHHqepMFuCXbw2ID8aD",
    "object": "subscription",
    "application": null,
    "application_fee_percent": null,
    "automatic_tax": {
      "enabled": false
    },
    "billing_cycle_anchor": 1656123715,
    "billing_thresholds": null,
    "cancel_at": null,
    "cancel_at_period_end": false,
    "canceled_at": null,
    "collection_method": "charge_automatically",
    "created": 1656123715,
    "current_period_end": 1658715715,
    "current_period_start": 1656123715,
    "customer": "cus_LwHB81LQK5UWH2",
    "days_until_due": null,
    "default_payment_method": "pm_1LEOd8HHqepMFuCXw2KdP7oq",
    "default_source": null,
    "default_tax_rates": [
      {
        "id": "txr_1LEOdCHHqepMFuCX8XgMWfVz",
        "object": "tax_rate",
        "active": true,
        "country": "US",
        "created": 1656123714,
        "description": "Sales tax in NY",
        "display_name": "NY Sales Tax",
        "inclusive": true,
        "jurisdiction": "US",
        "livemode": false,
        "metadata": {},
        "percentage": 17.5,
        "state": "NY",
        "tax_type": "sales_tax"
      },
      {
        "id": "txr_1LEOdDHHqepMFuCXhW38FRaS",
        "object": "tax_rate",
        "active": true,
        "country": "US",
        "created": 1656123715,
        "description": "Sales tax in NY",
        "display_name": "NY Sales Tax",
        "inclusive": true,
        "jurisdiction": "US",
        "livemode": false,
        "metadata": {},
        "percentage": 17.5,
        "state": "NY",
        "tax_type": "sales_tax"
      }
    ],
    "description": null,
    "discount": null,
    "ended_at": null,
    "items": {
      "object": "list",
      "data": [
        {
          "id": "si_LwHByExBocp8dH",
          "object": "subscription_item",
          "billing_thresholds": null,
          "created": 1656123715,
          "metadata": {},
          "plan": {
            "id": "price_1LEOd5HHqepMFuCXuIn5HDUC",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 1000,
            "amount_decimal": "1000",
            "billing_scheme": "per_unit",
            "created": 1656123707,
            "currency": "usd",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {},
            "nickname": null,
            "product": "prod_LwHBvhOe6NvV3g",
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "price": {
            "id": "price_1LEOd5HHqepMFuCXuIn5HDUC",
            "object": "price",
            "active": true,
            "billing_scheme": "per_unit",
            "created": 1656123707,
            "currency": "usd",
            "custom_unit_amount": null,
            "livemode": false,
            "lookup_key": null,
            "metadata": {},
            "nickname": null,
            "product": "prod_LwHBvhOe6NvV3g",
            "recurring": {
              "aggregate_usage": null,
              "interval": "month",
              "interval_count": 1,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "tax_behavior": "inclusive",
            "tiers_mode": null,
            "transform_quantity": null,
            "type": "recurring",
            "unit_amount": 1000,
            "unit_amount_decimal": "1000"
          },
          "quantity": 1,
          "subscription": "sub_1LEOdDHHqepMFuCXbw2ID8aD",
          "tax_rates": []
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/subscription_items?subscription=sub_1LEOdDHHqepMFuCXbw2ID8aD"
    },
    "latest_invoice": "in_1LEOdDHHqepMFuCX8Hvpp5Oh",
    "livemode": false,
    "metadata": {},
    "next_pending_invoice_item_invoice": null,
    "pause_collection": null,
    "payment_settings": {
      "payment_method_options": null,
      "payment_method_types": null,
      "save_default_payment_method": "off"
    },
    "pending_invoice_item_interval": null,
    "pending_setup_intent": null,
    "pending_update": null,
    "plan": {
      "id": "price_1LEOd5HHqepMFuCXuIn5HDUC",
      "object": "plan",
      "active": true,
      "aggregate_usage": null,
      "amount": 1000,
      "amount_decimal": "1000",
      "billing_scheme": "per_unit",
      "created": 1656123707,
      "currency": "usd",
      "interval": "month",
      "interval_count": 1,
      "livemode": false,
      "metadata": {},
      "nickname": null,
      "product": "prod_LwHBvhOe6NvV3g",
      "tiers_mode": null,
      "transform_usage": null,
      "trial_period_days": null,
      "usage_type": "licensed"
    },
    "quantity": 1,
    "schedule": null,
    "start_date": 1656123715,
    "status": "active",
    "test_clock": null,
    "transfer_data": null,
    "trial_end": null,
    "trial_start": null
  },
  "customerid": "cus_LwHB81LQK5UWH2",
  "accountid": "acct_895cf9d6afd6426f",
  "paymentmethodid": null,
  "productid": null,
  "priceids": [
    "price_1LEOd5HHqepMFuCXuIn5HDUC"
  ],
  "couponid": null,
  "appid": "tests_1656123707",
  "createdAt": "2022-06-25T02:21:57.680Z",
  "updatedAt": "2022-06-25T02:21:57.681Z"
}

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-customer ineligible querystring customer requires payment method
invalid-customerid missing querystring customerid
invalid querystring customerid
invalid-price invalid posted priceids contains not active price
invalid-priceid invalid posted priceids contains invalid price
invalid-priceids missing posted priceids
invalid-tax-rate invalid posted taxrateids contains inactive tax rate
invalid-taxrateid invalid posted taxrateids contains invalid tax rate

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')
    }
    if (!req.body || !req.body.priceids) {
      throw new Error('invalid-priceids')
    }
    if (!req.body || !req.body.quantities) {
      throw new Error('invalid-quantities')
    }
    const priceids = req.body.priceids.split(',')
    let metered = false
    for (const priceid of priceids) {
      req.query.priceid = priceid
      const price = await global.api.user.subscriptions.ActivePrice.get(req)
      if (!price) {
        throw new Error('invalid-priceid')
      }
      if (!price.stripeObject.active) {
        throw new Error('invalid-price')
      }
      if (price.stripeObject.recurring.usage_type === 'metered') {
        metered = true
      }
    }
    let quantities
    if (!metered) {
      quantities = req.body.quantities.split(',')
      for (const value of quantities) {
        try {
          const quantity = parseInt(value, 10)
          if (quantity < 1 || quantity.toString() !== value) {
            throw new Error('invalid-quantity')
          }
        } catch (error) {
          throw new Error('invalid-quantity')
        }
      }
      if (quantities.length !== priceids.length) {
        throw new Error('invalid-price-quantity-mismatch')
      }
    }
    if (req.body.couponid) {
      req.query.couponid = req.body.couponid
      const coupon = await global.api.user.subscriptions.Coupon.get(req)
      if (!coupon) {
        throw new Error('invalid-couponid')
      }
    }
    if (req.body.taxrateids) {
      const taxRates = req.body.taxrateids.split(',')
      for (const taxrateid of taxRates) {
        req.query.taxrateid = taxrateid
        const taxRate = await global.api.user.subscriptions.TaxRate.get(req)
        if (!taxRate) {
          throw new Error('invalid-taxrateid')
        }
        if (!taxRate.stripeObject.active) {
          throw new Error('invalid-tax-rate')
        }
      }
    }
    const customer = await global.api.user.subscriptions.Customer.get(req)
    if (customer.stripeObject.default_source) {
      req.body.paymentmethodid = customer.stripeObject.default_source
    } else if (customer.stripeObject.invoice_settings && customer.stripeObject.invoice_settings.default_payment_method) {
      req.body.paymentmethodid = customer.stripeObject.invoice_settings.default_payment_method
    }
    const subscriptionInfo = {
      customer: req.query.customerid,
      items: [],
      enable_incomplete_payments: true
    }
    for (let i = 0; i < priceids.length; i++) {
      subscriptionInfo.items.push({
        price: priceids[i],
        quantity: metered ? undefined : quantities[i]
      })
    }
    if (req.body.paymentmethodid) {
      subscriptionInfo.default_payment_method = req.body.paymentmethodid
    }
    if (req.body.trial_period_days) {
      subscriptionInfo.trial_period_days = req.body.trial_period_days
    }
    if (req.body.billing_cycle_anchor) {
      subscriptionInfo.billing_cycle_anchor = req.body.billing_cycle_anchor
    }
    if (req.body.couponid) {
      subscriptionInfo.coupon = req.body.couponid
    }
    if (req.body.taxrateids) {
      subscriptionInfo.default_tax_rates = req.body.taxrateids.split(',')
    }
    let subscription
    try {
      subscription = await stripeCache.execute('subscriptions', 'create', subscriptionInfo, req.stripeKey)
    } catch (error) {
      if (error.message === 'invalid-paymentmethod') {
        throw new Error('invalid-customer')
      }
    }
    if (!subscription) {
      throw new Error('unknown-error')
    }
    await subscriptions.Storage.Subscription.create({
      appid: req.appid || global.appid,
      subscriptionid: subscription.id,
      customerid: req.query.customerid,
      accountid: req.account.accountid,
      priceids,
      stripeObject: subscription
    })
    for (const item of subscription.items.data) {
      await subscriptions.Storage.SubscriptionItem.create({
        accountid: req.account.accountid,
        subscriptionitemid: item.id,
        customerid: req.query.customerid,
        appid: req.appid || global.appid,
        subscriptionid: subscription.id,
        stripeObject: item
      })
    }
    req.query.subscriptionid = subscription.id
    return global.api.user.subscriptions.Subscription.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-subscription', 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 administrator = await TestStripeAccounts.createOwnerWithPrice()
    const user = await TestHelper.createUser()
    // invalid customerid
    let req = TestHelper.createRequest('/api/user/subscriptions/create-subscription')
    req.account = user.account
    req.session = user.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.missingCustomer = error.message
    }
    req = TestHelper.createRequest('/api/user/subscriptions/create-subscription?customerid=invalid')
    req.account = user.account
    req.session = user.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidCustomer = error.message
    }
    // missing payment method
    await TestHelper.createCustomer(user, {
      email: user.profile.contactEmail,
      country: 'US'
    })
    req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.customerid}`)
    req.account = user.account
    req.session = user.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidPaymentMethod = error.message
    }
    // invalid account
    const user2 = await TestHelper.createUser()
    req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user.customer.customerid}`)
    req.account = user2.account
    req.session = user2.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidAccount = error.message
    }
    // invalid price
    const user3 = await TestStripeAccounts.createUserWithPaymentMethod()
    req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user3.customer.customerid}`)
    req.account = user3.account
    req.session = user3.session
    req.body = {
      priceids: '',
      paymentmethodid: user3.paymentMethod.paymentmethodid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.missingPrice = error.message
    }
    req.body = {
      priceids: 'invalid',
      paymentmethodid: user3.paymentMethod.paymentmethodid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidPrice = error.message
    }
    // not active price
    const administrator2 = await TestStripeAccounts.createOwnerWithInactivePrice()
    req.body = {
      priceids: administrator2.price.priceid,
      quantities: '1'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.notActivePrice = error.message
    }
    // default tax rates
    const taxRate1 = await TestHelper.createTaxRate(administrator, {
      active: 'false'
    })
    req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user3.customer.customerid}`)
    req.account = user3.account
    req.session = user3.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1',
      taxrateids: taxRate1.taxrateid
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidTaxRate = error.message
    }
    req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user3.customer.customerid}`)
    req.account = user3.account
    req.session = user3.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1',
      taxrateids: 'invalid'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.invalidTaxRateId = error.message
    }
    // response
    const taxRate2 = await TestHelper.createTaxRate(administrator, {
      active: true
    })
    const taxRate3 = await TestHelper.createTaxRate(administrator, {
      active: true
    })
    req = TestHelper.createRequest(`/api/user/subscriptions/create-subscription?customerid=${user3.customer.customerid}`)
    req.account = user3.account
    req.session = user3.session
    req.body = {
      priceids: administrator.price.priceid,
      quantities: '1',
      paymentmethodid: user3.paymentMethod.paymentmethodid,
      taxrateids: `${taxRate2.taxrateid},${taxRate3.taxrateid}`
    }
    req.filename = __filename
    req.saveResponse = true
    cachedResponses.returns = await req.post()
    cachedResponses.finished = true
  }

  describe('exceptions', () => {
    describe('invalid-customerid', () => {
      it('missing querystring customerid', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.missingCustomer
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })

      it('invalid querystring customerid', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidCustomer
        assert.strictEqual(errorMessage, 'invalid-customerid')
      })
    })

    describe('invalid-customer', () => {
      it('ineligible querystring customer requires payment method', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidPaymentMethod
        assert.strictEqual(errorMessage, 'invalid-customer')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidAccount
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })

    describe('invalid-priceids', () => {
      it('missing posted priceids', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.missingPrice
        assert.strictEqual(errorMessage, 'invalid-priceids')
      })
    })

    describe('invalid-priceid', () => {
      it('invalid posted priceids contains invalid price', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidPrice
        assert.strictEqual(errorMessage, 'invalid-priceid')
      })
    })

    describe('invalid-price', () => {
      it('invalid posted priceids contains not active price', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.notActivePrice
        assert.strictEqual(errorMessage, 'invalid-price')
      })
    })

    describe('invalid-taxrateid', () => {
      it('invalid posted taxrateids contains invalid tax rate', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidTaxRateId
        assert.strictEqual(errorMessage, 'invalid-taxrateid')
      })
    })

    describe('invalid-tax-rate', () => {
      it('invalid posted taxrateids contains inactive tax rate', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidTaxRate
        assert.strictEqual(errorMessage, 'invalid-tax-rate')
      })
    })
  })

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