Stripe Subscriptions module API explorer

/api/administrator/subscriptions/set-charge-flagged (PATCH)

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

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

Returns object

{
  "chargeid": "ch_3LEOQgHHqepMFuCX0HWt2NWj",
  "accountid": "acct_3ffa2a93c1c66c75",
  "subscriptionid": "sub_1LEOQfHHqepMFuCXf3B7mHOA",
  "customerid": "cus_LwGyl8e44PRfS1",
  "paymentmethodid": null,
  "invoiceid": "in_1LEOQfHHqepMFuCXFOfHellq",
  "refundRequested": null,
  "refundReason": null,
  "refundDenied": null,
  "refundDeniedReason": null,
  "object": "charge",
  "stripeObject": {
    "id": "ch_3LEOQgHHqepMFuCX0HWt2NWj",
    "object": "charge",
    "amount": 1000,
    "amount_captured": 1000,
    "amount_refunded": 0,
    "application": null,
    "application_fee": null,
    "application_fee_amount": null,
    "balance_transaction": "txn_3LEOQgHHqepMFuCX0ILDudf5",
    "billing_details": {
      "address": {
        "city": "New York",
        "country": "US",
        "line1": "285 Fulton St",
        "line2": "Apt 893",
        "postal_code": "10007",
        "state": "NY"
      },
      "email": null,
      "name": "Ida Considine",
      "phone": null
    },
    "calculated_statement_descriptor": "PRODUCT200 DESCRIPTION",
    "captured": true,
    "created": 1656122938,
    "currency": "usd",
    "customer": "cus_LwGyl8e44PRfS1",
    "description": "Subscription creation",
    "destination": null,
    "dispute": null,
    "disputed": false,
    "failure_balance_transaction": null,
    "failure_code": null,
    "failure_message": null,
    "fraud_details": {
      "user_report": "fraudulent"
    },
    "invoice": "in_1LEOQfHHqepMFuCXFOfHellq",
    "livemode": false,
    "metadata": {},
    "on_behalf_of": null,
    "order": null,
    "outcome": {
      "network_status": "approved_by_network",
      "reason": null,
      "risk_level": "normal",
      "risk_score": 44,
      "seller_message": "Payment complete.",
      "type": "authorized"
    },
    "paid": true,
    "payment_intent": "pi_3LEOQgHHqepMFuCX0kOmjf1U",
    "payment_method": "pm_1LEOQdHHqepMFuCXaRHSCEXv",
    "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/acct_1KhiP8HHqepMFuCX/ch_3LEOQgHHqepMFuCX0HWt2NWj/rcpt_LwGyrchq8f05Ng1hnszAJGQ8qElvPrG",
    "refunded": false,
    "refunds": {
      "object": "list",
      "data": [],
      "has_more": false,
      "total_count": 0,
      "url": "/v1/charges/ch_3LEOQgHHqepMFuCX0HWt2NWj/refunds"
    },
    "review": null,
    "shipping": null,
    "source": null,
    "source_transfer": null,
    "statement_descriptor": "product200 description",
    "statement_descriptor_suffix": null,
    "status": "succeeded",
    "transfer_data": null,
    "transfer_group": null
  },
  "appid": "tests_1656122925",
  "createdAt": "2022-06-25T02:09:01.117Z",
  "updatedAt": "2022-06-25T02:09:02.371Z"
}

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 is already flagged
invalid-chargeid missing querystring charge
invalid querystring charge

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.chargeid) {
      throw new Error('invalid-chargeid')
    }
    const charge = await global.api.administrator.subscriptions.Charge.get(req)
    if (!charge) {
      throw new Error('invalid-chargeid')
    }
    if (charge.stripeObject.fraud_details.user_report) {
      throw new Error('invalid-charge')
    }
    const chargeInfo = {
      fraud_details: {
        user_report: 'fraudulent'
      }
    }
    const chargeNow = await stripeCache.execute('charges', 'update', req.query.chargeid, chargeInfo, req.stripeKey)
    await subscriptions.Storage.Charge.update({
      stripeObject: chargeNow
    }, {
      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-charge-flagged', 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-charge-flagged')
    req.account = administrator.account
    req.session = administrator.session
    try {
      await req.patch()
    } catch (error) {
      cachedResponses.missing = error.message
    }
    const req2 = TestHelper.createRequest('/api/administrator/subscriptions/set-charge-flagged?chargeid=invalid')
    req2.account = administrator.account
    req2.session = administrator.session
    try {
      await req2.patch()
    } catch (error) {
      cachedResponses.invalid = error.message
    }
    // invalid charge
    const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
    await TestHelper.flagCharge(administrator, user.charge.chargeid)
    const req3 = TestHelper.createRequest(`/api/administrator/subscriptions/set-charge-flagged?chargeid=${user.charge.chargeid}`)
    req3.account = administrator.account
    req3.session = administrator.session
    try {
      await req3.patch()
    } catch (error) {
      cachedResponses.invalidCharge = error.message
    }
    // returns
    const user2 = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
    const req4 = TestHelper.createRequest(`/api/administrator/subscriptions/set-charge-flagged?chargeid=${user2.charge.chargeid}`)
    req4.account = administrator.account
    req4.session = administrator.session
    req4.filename = __filename
    req4.saveResponse = true
    cachedResponses.returns = await req4.patch()
    cachedResponses.finished = true
  }

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

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

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

  describe('returns', () => {
    it('object', async () => {
      const charge = cachedResponses.returns
      assert.strictEqual(charge.stripeObject.fraud_details.user_report, 'fraudulent')
    })
  })
})