Stripe Subscriptions module API explorer

/api/administrator/subscriptions/set-refund-request-denied (PATCH)

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

await global.api.administrator.subscriptions.SetRefundRequestDenied.patch(req)

Returns object

{
  "chargeid": "ch_3LEOSYHHqepMFuCX19xfC0Jg",
  "accountid": "acct_665957300028ad6e",
  "subscriptionid": "sub_1LEOSYHHqepMFuCXd9atvVsW",
  "customerid": "cus_LwH0R9a86peFp2",
  "paymentmethodid": null,
  "invoiceid": "in_1LEOSYHHqepMFuCXTMvlXQZr",
  "refundRequested": "2022-06-25T02:10:57.758Z",
  "refundReason": "unused subscription",
  "refundDenied": "1970-01-20T04:02:03.057Z",
  "refundDeniedReason": "no",
  "object": "charge",
  "stripeObject": {
    "id": "ch_3LEOSYHHqepMFuCX19xfC0Jg",
    "object": "charge",
    "amount": 1000,
    "amount_captured": 1000,
    "amount_refunded": 0,
    "application": null,
    "application_fee": null,
    "application_fee_amount": null,
    "balance_transaction": "txn_3LEOSYHHqepMFuCX1ogrOlfn",
    "billing_details": {
      "address": {
        "city": "New York",
        "country": "US",
        "line1": "285 Fulton St",
        "line2": "Apt 893",
        "postal_code": "10007",
        "state": "NY"
      },
      "email": null,
      "name": "Reginald Kutch",
      "phone": null
    },
    "calculated_statement_descriptor": "PRODUCT212 DESCRIPTION",
    "captured": true,
    "created": 1656123055,
    "currency": "usd",
    "customer": "cus_LwH0R9a86peFp2",
    "description": "Subscription creation",
    "destination": null,
    "dispute": null,
    "disputed": false,
    "failure_balance_transaction": null,
    "failure_code": null,
    "failure_message": null,
    "fraud_details": {},
    "invoice": "in_1LEOSYHHqepMFuCXTMvlXQZr",
    "livemode": false,
    "metadata": {},
    "on_behalf_of": null,
    "order": null,
    "outcome": {
      "network_status": "approved_by_network",
      "reason": null,
      "risk_level": "normal",
      "risk_score": 34,
      "seller_message": "Payment complete.",
      "type": "authorized"
    },
    "paid": true,
    "payment_intent": "pi_3LEOSYHHqepMFuCX1xmO1LHV",
    "payment_method": "pm_1LEOSDHHqepMFuCX3AdksdxR",
    "payment_method_details": {
      "card": {
        "brand": "visa",
        "checks": {
          "address_line1_check": "pass",
          "address_postal_code_check": "pass",
          "cvc_check": "pass"
        },
        "country": "US",
        "exp_month": 1,
        "exp_year": 2023,
        "fingerprint": "IRcdqfBUCskmPkNV",
        "funding": "credit",
        "installments": null,
        "last4": "1111",
        "mandate": null,
        "network": "visa",
        "three_d_secure": null,
        "wallet": null
      },
      "type": "card"
    },
    "receipt_email": null,
    "receipt_number": null,
    "receipt_url": "https://pay.stripe.com/receipts/invoices/CAcaFwoVYWNjdF8xS2hpUDhISHFlcE1GdUNYKLDd2ZUGMgbmc7BOPYU6LBaI5yNDxdYzARXXFhzCnGR6nHbNb0KlBxjLGd2l4-Uvjp5ttuzNlD6Sm4FY?s=ap",
    "refunded": false,
    "refunds": {
      "object": "list",
      "data": [],
      "has_more": false,
      "total_count": 0,
      "url": "/v1/charges/ch_3LEOSYHHqepMFuCX19xfC0Jg/refunds"
    },
    "review": null,
    "shipping": null,
    "source": null,
    "source_transfer": null,
    "statement_descriptor": "product212 description",
    "statement_descriptor_suffix": null,
    "status": "succeeded",
    "transfer_data": null,
    "transfer_group": null
  },
  "appid": "tests_1656123032",
  "createdAt": "2022-06-25T02:10:57.367Z",
  "updatedAt": "2022-06-25T02:10:57.762Z"
}

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-charge ineligible querystring charge has no refund request
ineligible querystring charge has denied request already
invalid-chargeid missing querystring chargeid
invalid querystring chargeid

NodeJS source (view on github)

const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')

module.exports = {
  patch: async (req) => {
    if (!req.query || !req.query.chargeid) {
      throw new Error('invalid-chargeid')
    }
    if (!req.body || !req.body.reason) {
      throw new Error('invalid-reason')
    }
    if (!req.body.reason.length || req.body.reason.length > 200) {
      throw new Error('invalid-reason-length')
    }
    const charge = await global.api.administrator.subscriptions.Charge.get(req)
    if (!charge.stripeObject.amount || !charge.stripeObject.paid || charge.refunded) {
      throw new Error('invalid-charge')
    }
    if (!charge.refundRequested || charge.refundDenied) {
      throw new Error('invalid-charge')
    }
    await subscriptions.Storage.Charge.update({
      refundDenied: Math.floor(new Date().getTime() / 1000),
      refundDeniedReason: req.body.reason
    }, {
      where: {
        chargeid: req.query.chargeid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.StorageCache.remove(req.query.chargeid)
    return global.api.administrator.subscriptions.Charge.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/administrator/subscriptions/set-refund-request-denied', 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()
    // missing and invalid id
    const req = TestHelper.createRequest('/api/administrator/subscriptions/set-refund-request-denied')
    req.account = administrator.account
    req.session = administrator.session
    req.body = {
      reason: 'no'
    }
    try {
      await req.patch()
    } catch (error) {
      cachedResponses.missing = error.message
    }
    const req2 = TestHelper.createRequest('/api/administrator/subscriptions/set-refund-request-denied?chargeid=invalid')
    req2.account = administrator.account
    req2.session = administrator.session
    req2.body = {
      reason: 'no'
    }
    try {
      await req2.patch()
    } catch (error) {
      cachedResponses.invalid = error.message
    }
    // invalid charge
    const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
    const req3 = TestHelper.createRequest(`/api/administrator/subscriptions/set-refund-request-denied?chargeid=${user.charge.chargeid}`)
    req3.account = administrator.account
    req3.session = administrator.session
    req3.body = {
      reason: 'no'
    }
    try {
      await req3.patch()
    } catch (error) {
      cachedResponses
        .invalidCharge = error.message
    }
    await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
    await TestHelper.requestRefund(user, user.charge.chargeid)
    await TestHelper.denyRefund(administrator, user, user.charge.chargeid)
    const req4 = TestHelper.createRequest(`/api/administrator/subscriptions/set-refund-request-denied?chargeid=${user.charge.chargeid}`)
    req4.account = administrator.account
    req4.session = administrator.session
    req4.body = {
      reason: 'no'
    }
    try {
      await req4.patch()
    } catch (error) {
      cachedResponses.invalidCharge2 = error.message
    }
    // returns
    await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
    await TestHelper.requestRefund(user, user.charge.chargeid)
    const req5 = TestHelper.createRequest(`/api/administrator/subscriptions/set-refund-request-denied?chargeid=${user.charge.chargeid}`)
    req5.account = administrator.account
    req5.session = administrator.session
    req5.body = {
      reason: 'no'
    }
    req5.filename = __filename
    req5.saveResponse = true
    cachedResponses.returns = await req5.patch()
    cachedResponses.finished = true
  }

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

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

    describe('invalid-charge', () => {
      it('ineligible querystring charge has no refund request', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidCharge
        assert.strictEqual(errorMessage, 'invalid-charge')
      })

      it('ineligible querystring charge has denied request already', async function () {
        await bundledData(this.test.currentRetry())
        const errorMessage = cachedResponses.invalidCharge2
        assert.strictEqual(errorMessage, 'invalid-charge')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const charge = cachedResponses.returns
      assert.notStrictEqual(charge.refundDenied, undefined)
      assert.notStrictEqual(charge.refundDenied, null)
      assert.strictEqual(charge.refundDeniedReason, 'no')
    })
  })
})