/api/user/subscriptions/payment-intent (GET)
await global.api.user.subscriptions.PaymentIntent.get(req) Located in Stripe Subscriptions module API
Returns object
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 querystring invalid | |
invalid-paymentintentid | missing querystring invalid |
invalid-account | ineligible accessing account |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.paymentintentid) {
throw new Error('invalid-paymentintentid')
}
const exists = await subscriptions.StorageList.exists(`${req.appid}/paymentIntents`, req.query.paymentintentid)
if (!exists) {
throw new Error('invalid-paymentintentid')
}
const owned = await subscriptions.StorageList.exists(`${req.appid}/account/paymentIntents/${req.account.accountid}`, req.query.paymentintentid)
if (!owned) {
throw new Error('invalid-account')
}
let paymentintent
try {
paymentintent = await stripeCache.retrieve(req.query.paymentintentid, 'paymentIntents', req.stripeKey)
} catch (error) {
}
if (!paymentintent) {
throw new Error('invalid-paymentintentid')
}
return paymentintent
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
describe('/api/user/subscriptions/payment-intent', () => {
describe('exceptions', () => {
describe('invalid-paymentintentid', () => {
it('missing querystring invalid', async () => {
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 () => {
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 () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createPaymentIntent(user, {
amount: '10000',
currency: 'usd',
paymentmethodid: user.paymentMethod.id
})
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/payment-intent?paymentintentid=${user.paymentIntent.id}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createPaymentIntent(user, {
amount: '10000',
currency: 'usd',
paymentmethodid: user.paymentMethod.id
})
const req = TestHelper.createRequest(`/api/user/subscriptions/payment-intent?paymentintentid=${user.paymentIntent.id}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const paymentIntent = await req.get()
assert.strictEqual(paymentIntent.id, user.paymentIntent.id)
})
})
})