Stripe Subscriptions module API explorer

/api/user/subscriptions/set-invoice-paid (PATCH)

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

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

Returns object

{
  "invoiceid": "in_1LEOkDHHqepMFuCXVqmMipP6",
  "object": "invoice",
  "stripeObject": {
    "id": "in_1LEOkDHHqepMFuCXVqmMipP6",
    "object": "invoice",
    "account_country": "AU",
    "account_name": "test-subscriptions",
    "account_tax_ids": null,
    "amount_due": 2500,
    "amount_paid": 2500,
    "amount_remaining": 0,
    "application": null,
    "application_fee_amount": null,
    "attempt_count": 1,
    "attempted": true,
    "auto_advance": false,
    "automatic_tax": {
      "enabled": false,
      "status": null
    },
    "billing_reason": "manual",
    "charge": "ch_3LEOkEHHqepMFuCX0bY61d7z",
    "collection_method": "charge_automatically",
    "created": 1656124149,
    "currency": "usd",
    "custom_fields": null,
    "customer": "cus_LwHII6zIkBDuEU",
    "customer_address": null,
    "customer_email": "Dixie.Greenholt@yahoo.com",
    "customer_name": null,
    "customer_phone": null,
    "customer_shipping": null,
    "customer_tax_exempt": "none",
    "customer_tax_ids": [],
    "default_payment_method": null,
    "default_source": null,
    "default_tax_rates": [],
    "description": null,
    "discount": null,
    "discounts": [],
    "due_date": null,
    "ending_balance": 0,
    "footer": null,
    "hosted_invoice_url": "https://invoice.stripe.com/i/acct_1KhiP8HHqepMFuCX/test_YWNjdF8xS2hpUDhISHFlcE1GdUNYLF9Md0hJSmluZEJkR21tVWl2VU5lSVljZEFwaXJvalVQLDQ2NjY0OTUy0200jfrVOY8N?s=ap",
    "invoice_pdf": "https://pay.stripe.com/invoice/acct_1KhiP8HHqepMFuCX/test_YWNjdF8xS2hpUDhISHFlcE1GdUNYLF9Md0hJSmluZEJkR21tVWl2VU5lSVljZEFwaXJvalVQLDQ2NjY0OTUy0200jfrVOY8N/pdf?s=ap",
    "last_finalization_error": null,
    "lines": {
      "object": "list",
      "data": [
        {
          "id": "il_1LEOkCHHqepMFuCXKFayCIm7",
          "object": "line_item",
          "amount": 2500,
          "amount_excluding_tax": 2500,
          "currency": "usd",
          "description": "One-time setup fee",
          "discount_amounts": [],
          "discountable": true,
          "discounts": [],
          "invoice_item": "ii_1LEOkCHHqepMFuCXNXevGAsM",
          "livemode": false,
          "metadata": {},
          "period": {
            "end": 1656124148,
            "start": 1656124148
          },
          "plan": null,
          "price": {
            "id": "price_1KjGWWHHqepMFuCXfDloFG5a",
            "object": "price",
            "active": false,
            "billing_scheme": "per_unit",
            "created": 1648704380,
            "currency": "usd",
            "custom_unit_amount": null,
            "livemode": false,
            "lookup_key": null,
            "metadata": {},
            "nickname": null,
            "product": "prod_LOVmd6Ev4OAeb6",
            "recurring": null,
            "tax_behavior": "unspecified",
            "tiers_mode": null,
            "transform_quantity": null,
            "type": "one_time",
            "unit_amount": 2500,
            "unit_amount_decimal": "2500"
          },
          "proration": false,
          "proration_details": {
            "credited_items": null
          },
          "quantity": 1,
          "subscription": null,
          "tax_amounts": [],
          "tax_rates": [],
          "type": "invoiceitem",
          "unit_amount_excluding_tax": "2500"
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/invoices/in_1LEOkDHHqepMFuCXVqmMipP6/lines"
    },
    "livemode": false,
    "metadata": {},
    "next_payment_attempt": null,
    "number": "1C7947E2-0004",
    "on_behalf_of": null,
    "paid": true,
    "paid_out_of_band": false,
    "payment_intent": "pi_3LEOkEHHqepMFuCX0EZpM20O",
    "payment_settings": {
      "payment_method_options": null,
      "payment_method_types": null
    },
    "period_end": 1658716136,
    "period_start": 1656124136,
    "post_payment_credit_notes_amount": 0,
    "pre_payment_credit_notes_amount": 0,
    "quote": null,
    "receipt_number": null,
    "rendering_options": null,
    "starting_balance": 0,
    "statement_descriptor": null,
    "status": "paid",
    "status_transitions": {
      "finalized_at": 1656124149,
      "marked_uncollectible_at": null,
      "paid_at": 1656124150,
      "voided_at": null
    },
    "subscription": null,
    "subtotal": 2500,
    "subtotal_excluding_tax": 2500,
    "tax": null,
    "test_clock": null,
    "total": 2500,
    "total_discount_amounts": [],
    "total_excluding_tax": 2500,
    "total_tax_amounts": [],
    "transfer_data": null,
    "webhooks_delivered_at": null
  },
  "customerid": "cus_LwHII6zIkBDuEU",
  "subscriptionid": null,
  "accountid": "acct_91f06f0d6c6b78b3",
  "appid": "tests_1656124131",
  "createdAt": "2022-06-25T02:29:10.607Z",
  "updatedAt": "2022-06-25T02:29:13.086Z"
}

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-invoice invalid querystring invoice is paid
invalid querystring invoice is marked uncollectable
invalid-invoiceid missing querystring invoiceid
invalid querystring invoiceid
invalid-paymentmethodid missing posted paymentmethodid
invalid posted paymentmethodid

NodeJS source (view on github)

const dashboard = require('@layeredapps/dashboard')
const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.js')

module.exports = {
  patch: async (req) => {
    if (!req.query || !req.query.invoiceid) {
      throw new Error('invalid-invoiceid')
    }
    const invoice = await global.api.user.subscriptions.Invoice.get(req)
    if (!invoice) {
      throw new Error('invalid-invoiceid')
    }
    if (invoice.stripeObject.status !== 'draft' && invoice.stripeObject.status !== 'open') {
      throw new Error('invalid-invoice')
    }
    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 invoiceNow = await stripeCache.execute('invoices', 'pay', req.query.invoiceid, {
      payment_method: paymentMethod.id
    }, req.stripeKey)
    await subscriptions.Storage.Invoice.update({
      stripeObject: invoiceNow
    }, {
      where: {
        invoiceid: req.query.invoiceid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.StorageCache.remove(req.query.invoiceid)
    return global.api.user.subscriptions.Invoice.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/set-invoice-paid', 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 TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
    // invalid invoice is paid
    const req5 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req5.account = user.account
    req5.session = user.session
    req5.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid
    }
    try {
      await req5.patch()
    } catch (error) {
      cachedResponses.invalidInvoice = error.message
    }
    // missing and invalid id
    const req = TestHelper.createRequest('/api/user/subscriptions/set-invoice-paid')
    req.account = user.account
    req.session = user.session
    req.body = {
      paymentmethodid: 'invalid'
    }
    try {
      await req.patch()
    } catch (error) {
      cachedResponses.missing = error.message
    }
    const req2 = TestHelper.createRequest('/api/user/subscriptions/set-invoice-paid?invoiceid=invalid')
    req2.account = user.account
    req2.session = user.session
    req2.body = {
      paymentmethodid: 'invalid'
    }
    try {
      await req2.patch()
    } catch (error) {
      cachedResponses.invalid = error.message
    }
    // invalid account
    await TestHelper.createAmountOwed(user)
    const user2 = await TestStripeAccounts.createUserWithPaymentMethod()
    const req3 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req3.account = user2.account
    req3.session = user2.session
    req3.body = {
      paymentmethodid: 'invalid'
    }
    try {
      await req3.patch()
    } catch (error) {
      cachedResponses.invalidAccount = error.message
    }
    const req4 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req4.account = user.account
    req4.session = user.session
    req4.body = {
      paymentmethodid: user2.paymentMethod.paymentmethodid
    }
    try {
      await req4.patch()
    } catch (error) {
      cachedResponses.invalidAccount2 = error.message
    }
    // invalid invoice
    await TestHelper.createAmountOwed(user)
    await TestHelper.forgiveInvoice(administrator, user.invoice.invoiceid)
    const req6 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req6.account = user.account
    req6.session = user.session
    req6.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid
    }
    try {
      await req6.patch()
    } catch (error) {
      cachedResponses.invalidInvoice2 = error.message
    }
    // invalid payment method
    await TestHelper.createAmountOwed(user)
    const req7 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req7.account = user.account
    req7.session = user.session
    req7.body = {
      paymentmethodid: ''
    }
    try {
      await req7.patch()
    } catch (error) {
      cachedResponses.missingPaymentMethod = error.message
    }
    const req8 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req8.account = user.account
    req8.session = user.session
    req8.body = {
      paymentmethodid: 'invalid'
    }
    try {
      await req8.patch()
    } catch (error) {
      cachedResponses.invalidPaymentMethod = error.message
    }
    // returns
    const req9 = TestHelper.createRequest(`/api/user/subscriptions/set-invoice-paid?invoiceid=${user.invoice.invoiceid}`)
    req9.account = user.account
    req9.session = user.session
    req9.body = {
      paymentmethodid: user.paymentMethod.paymentmethodid
    }
    req9.filename = __filename
    req9.saveResponse = true
    cachedResponses.returns = await req9.patch()
    cachedResponses.finished = true
  }

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

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

    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.invalidAccount2
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })

    describe('invalid-invoice', () => {
      it('invalid querystring invoice is paid', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidInvoice
        assert.strictEqual(errorMessage, 'invalid-invoice')
      })

      it('invalid querystring invoice is marked uncollectable', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidInvoice2
        assert.strictEqual(errorMessage, 'invalid-invoice')
      })
    })

    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('returns', () => {
    it('object', async () => {
      const invoice = cachedResponses.returns
      assert.strictEqual(invoice.stripeObject.paid, true)
    })
  })
})