Stripe Subscriptions module API explorer

/api/user/subscriptions/create-cancelation-refund (POST)

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

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

Returns object

{
  "refundid": "re_3LEOb2HHqepMFuCX1h6zv1xa",
  "object": "refund",
  "accountid": "acct_1e7c2ed38cb5a55b",
  "subscriptionid": "sub_1LEOb2HHqepMFuCXEyDxudDw",
  "customerid": "cus_LwH9BKit9cTEMW",
  "invoiceid": "in_1LEOb2HHqepMFuCXhFtWwu3a",
  "productid": null,
  "paymentmethodid": null,
  "appid": "tests_1656123576",
  "createdAt": "2022-06-25T02:19:56.255Z",
  "updatedAt": "2022-06-25T02:19:56.256Z"
}

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-subscription ineligible querystring subscription is not active
ineligible querystring subscription is free
invalid-subscriptionid missing querystring subscriptionid
invalid querystring subscriptionid

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.subscriptionid) {
      throw new Error('invalid-subscriptionid')
    }
    const subscription = await global.api.user.subscriptions.Subscription.get(req)
    if (!subscription) {
      throw new Error('invalid-subscriptionid')
    }
    if ((subscription.stripeObject.status !== 'active' && subscription.stripeObject.status !== 'trialing') || subscription.stripeObject.cancel_at_period_end) {
      throw new Error('invalid-subscription')
    }
    req.query.invoiceid = subscription.stripeObject.latest_invoice
    const invoice = await global.api.user.subscriptions.Invoice.get(req)
    if (!invoice.stripeObject.charge) {
      throw new Error('invalid-subscription')
    }
    req.query.chargeid = invoice.stripeObject.charge
    const charge = await global.api.user.subscriptions.Charge.get(req)
    if (!charge) {
      throw new Error('invalid-subscription')
    }
    const zeroed = []
    for (const item of subscription.stripeObject.items.data) {
      zeroed.push({ id: item.id, quantity: 0 })
    }
    const upcoming = await stripeCache.execute('invoices', 'retrieveUpcoming', {
      customer: subscription.customerid || subscription.customer,
      subscription: req.query.subscriptionid,
      subscription_items: zeroed
    }, req.stripeKey)
    if (upcoming.total === 0) {
      throw new Error('invalid-subscription')
    }
    const refundInfo = {
      charge: invoice.stripeObject.charge,
      amount: -upcoming.total,
      reason: 'requested_by_customer'
    }
    const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
    await subscriptions.Storage.Refund.create({
      appid: req.appid || global.appid,
      refundid: refund.id,
      accountid: subscription.accountid,
      subscriptionid: subscription.subscriptionid,
      customerid: subscription.customerid,
      invoiceid: invoice.invoiceid,
      productid: invoice.productid,
      paymentmethodid: invoice.paymentmethodid
    })
    req.query.refundid = refund.id
    return global.api.user.subscriptions.Refund.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-cancelation-refund', 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)
    // missing and invalid id
    const req = TestHelper.createRequest('/api/user/subscriptions/create-cancelation-refund')
    req.account = user.account
    req.session = user.session
    req.body = {
      refund: 'at_period_end'
    }
    try {
      await req.post()
    } catch (error) {
      cachedResponses.missing = error.message
    }
    const req2 = TestHelper.createRequest('/api/user/subscriptions/create-cancelation-refund?subscriptionid=invalid')
    req2.account = user.account
    req2.session = user.session
    try {
      await req2.post()
    } catch (error) {
      cachedResponses.invalid = error.message
    }
    // invalid account
    const user2 = await TestHelper.createUser()
    const req3 = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-canceled?subscriptionid=${user.subscription.subscriptionid}`)
    req3.account = user2.account
    req3.session = user2.session
    req3.body = {
      refund: 'at_period_end'
    }
    try {
      await req3.patch()
    } catch (error) {
      cachedResponses.account = error.message
    }
    // invalid subscription
    const user3 = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
    await TestHelper.cancelSubscription(user3)
    const req4 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user3.subscription.subscriptionid}`)
    req4.account = user3.account
    req4.session = user3.session
    try {
      await req4.post()
    } catch (error) {
      cachedResponses.inactiveSubscription = error.message
    }
    await TestHelper.createPrice(administrator, {
      productid: administrator.product.productid,
      active: 'true',
      unit_amount: 0,
      currency: 'usd',
      tax_behavior: 'inclusive',
      recurring_interval: 'month',
      recurring_interval_count: '1',
      recurring_usage_type: 'licensed'
    })
    const user4 = await TestStripeAccounts.createUserWithFreeSubscription(administrator.price)
    const req5 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user4.subscription.subscriptionid}`)
    req5.account = user4.account
    req5.session = user4.session
    try {
      await req5.post()
    } catch (error) {
      cachedResponses.freeSubscription = error.message
    }
    // returns
    const req6 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.subscriptionid}`)
    req6.account = user.account
    req6.session = user.session
    req6.body = {
      refund: 'credit'
    }
    req6.filename = __filename
    req6.saveResponse = true
    cachedResponses.returns = await req6.post()
    cachedResponses.finished = true
  }

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

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

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

    describe('invalid-subscription', () => {
      it('ineligible querystring subscription is not active', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.inactiveSubscription
        assert.strictEqual(errorMessage, 'invalid-subscription')
      })

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

      // TODO: requires modifying create-subscription to skip requiring payment method
      // it('ineligible querystring subscription is in free trial', async () => {
      //   const administrator = await TestStripeAccounts.createOwnerWithPrice()
      //   const user = await TestHelper.createUser()
      //   await TestHelper.createCustomer(user, {
      //     email: user.profile.contactEmail,
      //     description: user.profile.fullName
      //   })
      //   await TestHelper.createSubscription(user, administrator.price.priceid)
      //   const req2 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.subscriptionid}`)
      //   req2.account = user.account
      //   req2.session = user.session
      //   let errorMessage
      //   try {
      //     await req2.post()
      //   } catch (error) {
      //     errorMessage = error.message
      //   }
      //   assert.strictEqual(errorMessage, 'invalid-subscription')
      // })
    })
  })

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