/api/user/subscriptions/set-setup-intent-canceled (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.SetSetupIntentCanceled.patch(req)Returns object
{
"setupintentid": "seti_1LEOkrHHqepMFuCXSAEgRIk7",
"accountid": "acct_61c23ce6401c9f92",
"customerid": "cus_LwHJVyMv20rw4M",
"paymentmethodid": null,
"object": "setupintent",
"stripeObject": {
"id": "seti_1LEOkrHHqepMFuCXSAEgRIk7",
"object": "setup_intent",
"application": null,
"cancellation_reason": null,
"client_secret": "seti_1LEOkrHHqepMFuCXSAEgRIk7_secret_LwHJOumwIL0bIxQstO6DVTnvuCrXEjv",
"created": 1656124189,
"customer": "cus_LwHJVyMv20rw4M",
"description": null,
"flow_directions": null,
"last_setup_error": null,
"latest_attempt": null,
"livemode": false,
"mandate": null,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": "pm_1LEOkqHHqepMFuCXeD4wgDkj",
"payment_method_options": {
"card": {
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"single_use_mandate": null,
"status": "canceled",
"usage": "off_session"
},
"appid": "tests_1656124187",
"createdAt": "2022-06-25T02:29:50.275Z",
"updatedAt": "2022-06-25T02:29:51.358Z"
}
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-setupintentid | missing querystring setupintentid |
invalid querystring setupintentid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.setupintentid) {
throw new Error('invalid-setupintentid')
}
const setupIntent = await global.api.user.subscriptions.SetupIntent.get(req)
if (!setupIntent) {
throw new Error('invalid-setupintentid')
}
req.query.customerid = setupIntent.customerid
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-setupintentid')
}
const setupIntentNow = await stripeCache.execute('setupIntents', 'cancel', req.query.setupintentid, req.stripeKey)
await subscriptions.Storage.SetupIntent.update({
stripeObject: setupIntentNow
}, {
where: {
setupintentid: req.query.setupintentid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.setupintentid)
return global.api.user.subscriptions.SetupIntent.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-setup-intent-canceled', 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 user = await TestStripeAccounts.createUserWithPaymentMethod()
// missing or invalid id
const req2 = TestHelper.createRequest('/api/user/subscriptions/set-setup-intent-canceled')
req2.account = user.account
req2.session = user.session
try {
await req2.patch()
} catch (error) {
cachedResponses.missing = error.message
}
const req3 = TestHelper.createRequest('/api/user/subscriptions/set-setup-intent-canceled?setupintentid=invalid')
req3.account = user.account
req3.session = user.session
try {
await req3.patch()
} catch (error) {
cachedResponses.invalid = error.message
}
// invalid account
const user2 = await TestHelper.createUser()
const req4 = TestHelper.createRequest(`/api/user/subscriptions/set-setup-intent-canceled?setupintentid=${user.setupIntent.setupintentid}`)
req4.account = user2.account
req4.session = user2.session
try {
await req4.patch()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// returns
const req5 = TestHelper.createRequest(`/api/user/subscriptions/set-setup-intent-canceled?setupintentid=${user.setupIntent.setupintentid}`)
req5.account = user.account
req5.session = user.session
req5.filename = __filename
req5.saveResponse = true
cachedResponses.returns = await req5.patch()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-setupintentid', () => {
it('missing querystring setupintentid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-setupintentid')
})
it('invalid querystring setupintentid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-setupintentid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidAccount
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const setupIntentNow = cachedResponses.returns
assert.strictEqual(setupIntentNow.stripeObject.status, 'canceled')
})
})
})