/api/user/subscriptions/payment-intent (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.PaymentIntent.get(req)Returns object
{
"paymentintentid": "pi_3LEOggHHqepMFuCX0FrrVzWM",
"object": "paymentintent",
"stripeObject": {
"id": "pi_3LEOggHHqepMFuCX0FrrVzWM",
"object": "payment_intent",
"amount": 10000,
"amount_capturable": 0,
"amount_details": {
"tip": {}
},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges?payment_intent=pi_3LEOggHHqepMFuCX0FrrVzWM"
},
"client_secret": "pi_3LEOggHHqepMFuCX0FrrVzWM_secret_9NL3fLFAWg2ybG19AEtK6QFii",
"confirmation_method": "automatic",
"created": 1656123930,
"currency": "usd",
"customer": "cus_LwHFPAils8cZI1",
"description": null,
"invoice": null,
"last_payment_error": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"card": {
"installments": null,
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"source": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "requires_payment_method",
"transfer_data": null,
"transfer_group": null
},
"accountid": "acct_95b746ef23594964",
"customerid": "cus_LwHFPAils8cZI1",
"paymentmethodid": "pm_1LEOgeHHqepMFuCXW9101kYw",
"subscriptionid": null,
"invoiceid": null,
"status": "requires_payment_method",
"appid": "tests_1656123927",
"createdAt": "2022-06-25T02:25:31.044Z",
"updatedAt": "2022-06-25T02:25:31.573Z"
}
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-paymentintentid | missing querystring invalid |
invalid querystring invalid |
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.paymentintentid) {
throw new Error('invalid-paymentintentid')
}
let paymentIntent = await dashboard.StorageCache.get(req.query.paymentintentid)
if (!paymentIntent) {
const paymentIntentInfo = await subscriptions.Storage.PaymentIntent.findOne({
where: {
paymentintentid: req.query.paymentintentid,
appid: req.appid || global.appid
}
})
if (!paymentIntentInfo) {
throw new Error('invalid-paymentintentid')
}
if (paymentIntentInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
paymentIntent = {}
for (const field of paymentIntentInfo._options.attributes) {
paymentIntent[field] = paymentIntentInfo.get(field)
}
await dashboard.StorageCache.set(req.query.paymentintentid, paymentIntent)
}
return paymentIntent
}
}
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/payment-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()
await TestHelper.createPaymentIntent(user, {
amount: '10000',
currency: 'usd',
paymentmethodid: user.paymentMethod.paymentmethodid
})
const user2 = await TestHelper.createUser()
// invalid account
const req = TestHelper.createRequest(`/api/user/subscriptions/payment-intent?paymentintentid=${user.paymentIntent.paymentintentid}`)
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/payment-intent?paymentintentid=${user.paymentIntent.paymentintentid}`)
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-paymentintentid', () => {
it('missing querystring invalid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/payment-intent')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentintentid')
})
it('invalid querystring invalid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/payment-intent?paymentintentid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentintentid')
})
})
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 paymentIntent = cachedResponses.returns
assert.strictEqual(paymentIntent.object, 'paymentintent')
})
})
})