/api/user/subscriptions/setup-intent (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.SetupIntent.get(req)Returns object
{
"setupintentid": "seti_1LEOmUHHqepMFuCX7ZWphTZ0",
"accountid": "acct_7730484b9b4a8c82",
"customerid": "cus_LwHLFbAyyTLhOK",
"paymentmethodid": null,
"object": "setupintent",
"stripeObject": {
"id": "seti_1LEOmUHHqepMFuCX7ZWphTZ0",
"object": "setup_intent",
"application": null,
"cancellation_reason": null,
"client_secret": "seti_1LEOmUHHqepMFuCX7ZWphTZ0_secret_LwHLUIYOItRmbv1XOb9sSmJWZolTL90",
"created": 1656124290,
"customer": "cus_LwHLFbAyyTLhOK",
"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_1LEOmTHHqepMFuCXgU0v2UXd",
"payment_method_options": {
"card": {
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"single_use_mandate": null,
"status": "requires_confirmation",
"usage": "off_session"
},
"appid": "tests_1656124288",
"createdAt": "2022-06-25T02:31:31.027Z",
"updatedAt": "2022-06-25T02:31:31.266Z"
}
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')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.setupintentid) {
throw new Error('invalid-setupintentid')
}
let setupIntent = await dashboard.StorageCache.get(req.query.setupintentid)
if (!setupIntent) {
const setupIntentInfo = await subscriptions.Storage.SetupIntent.findOne({
where: {
setupintentid: req.query.setupintentid,
appid: req.appid || global.appid
}
})
if (!setupIntentInfo) {
throw new Error('invalid-setupintentid')
}
if (setupIntentInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
setupIntent = {}
for (const field of setupIntentInfo._options.attributes) {
setupIntent[field] = setupIntentInfo.get(field)
}
await dashboard.StorageCache.set(req.query.setupintentid, setupIntent)
}
return setupIntent
}
}
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/setup-intent', 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()
const user2 = await TestHelper.createUser()
// invalid account
const req = TestHelper.createRequest(`/api/user/subscriptions/setup-intent?setupintentid=${user.setupIntent.setupintentid}`)
req.account = user2.account
req.session = user2.session
try {
await req.get()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// response
const req2 = TestHelper.createRequest(`/api/user/subscriptions/setup-intent?setupintentid=${user.setupIntent.setupintentid}`)
req2.account = user.account
req2.session = user.session
req2.filename = __filename
req2.saveResponse = true
cachedResponses.returns = await req2.get()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-setupintentid', () => {
it('missing querystring setupintentid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/setup-intent')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-setupintentid')
})
it('invalid querystring setupintentid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/setup-intent?setupintentid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
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 setupIntent = cachedResponses.returns
assert.strictEqual(setupIntent.object, 'setupintent')
})
})
})