/api/user/subscriptions/payment-methods (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.PaymentMethods.get(req)Returns array
[
{
"paymentmethodid": "pm_1LEOhvHHqepMFuCXnSNuZivQ",
"object": "paymentmethod",
"accountid": "acct_eec0cfa8a0753ec0",
"customerid": "cus_LwHGn5lfgZH15S",
"stripeObject": {
"id": "pm_1LEOhvHHqepMFuCXnSNuZivQ",
"object": "payment_method",
"billing_details": {
"address": {
"city": "New York",
"country": "US",
"line1": "285 Fulton St",
"line2": "Apt 893",
"postal_code": "90120",
"state": "NY"
},
"email": null,
"name": "Natalie Stark",
"phone": null
},
"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",
"generated_from": null,
"last4": "1111",
"networks": {
"available": [
"visa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1656124007,
"customer": "cus_LwHGn5lfgZH15S",
"livemode": false,
"metadata": {},
"type": "card"
},
"appid": "tests_1656123998",
"createdAt": "2022-06-25T02:26:48.887Z",
"updatedAt": "2022-06-25T02:26:49.896Z"
},
{
"paymentmethodid": "pm_1LEOhsHHqepMFuCXVhJIxEPA",
"object": "paymentmethod",
"accountid": "acct_eec0cfa8a0753ec0",
"customerid": "cus_LwHGn5lfgZH15S",
"stripeObject": {
"id": "pm_1LEOhsHHqepMFuCXVhJIxEPA",
"object": "payment_method",
"billing_details": {
"address": {
"city": "New York",
"country": "US",
"line1": "285 Fulton St",
"line2": "Apt 893",
"postal_code": "90120",
"state": "NY"
},
"email": null,
"name": "Natalie Stark",
"phone": null
},
"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",
"generated_from": null,
"last4": "1111",
"networks": {
"available": [
"visa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1656124005,
"customer": "cus_LwHGn5lfgZH15S",
"livemode": false,
"metadata": {},
"type": "card"
},
"appid": "tests_1656123998",
"createdAt": "2022-06-25T02:26:46.115Z",
"updatedAt": "2022-06-25T02:26:47.336Z"
},
{
"paymentmethodid": "pm_1LEOhqHHqepMFuCXci74ctlG",
"object": "paymentmethod",
"accountid": "acct_eec0cfa8a0753ec0",
"customerid": "cus_LwHGn5lfgZH15S",
"stripeObject": {
"id": "pm_1LEOhqHHqepMFuCXci74ctlG",
"object": "payment_method",
"billing_details": {
"address": {
"city": "New York",
"country": "US",
"line1": "285 Fulton St",
"line2": "Apt 893",
"postal_code": "90120",
"state": "NY"
},
"email": null,
"name": "Natalie Stark",
"phone": null
},
"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",
"generated_from": null,
"last4": "1111",
"networks": {
"available": [
"visa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1656124002,
"customer": "cus_LwHGn5lfgZH15S",
"livemode": false,
"metadata": {},
"type": "card"
},
"appid": "tests_1656123998",
"createdAt": "2022-06-25T02:26:43.447Z",
"updatedAt": "2022-06-25T02:26:44.909Z"
}
]
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
all | boolean | optional | URL |
limit | integer | optional | URL |
offset | integer | optional | URL |
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-accountid | missing querystring accountid |
invalid querystring accountid |
NodeJS source (view on github)
const subscriptions = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.accountid) {
throw new Error('invalid-accountid')
}
const where = {
appid: req.appid || global.appid
}
if (req.query.customerid) {
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-customer')
}
where.customerid = req.query.customerid
} else {
const account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-account')
}
where.accountid = req.query.accountid
}
let paymentmethodids
if (req.query.all) {
paymentmethodids = await subscriptions.Storage.PaymentMethod.findAll({
where,
attributes: ['paymentmethodid'],
order: [
['createdAt', 'DESC']
]
})
} else {
const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0
const limit = req.query.limit ? parseInt(req.query.limit, 10) : global.pageSize
paymentmethodids = await subscriptions.Storage.PaymentMethod.findAll({
where,
attributes: ['paymentmethodid'],
offset,
limit,
order: [
['createdAt', 'DESC']
]
})
}
if (!paymentmethodids || !paymentmethodids.length) {
return null
}
const items = []
for (const paymentmethodInfo of paymentmethodids) {
req.query.paymentmethodid = paymentmethodInfo.dataValues.paymentmethodid
const paymentmethod = await global.api.user.subscriptions.PaymentMethod.get(req)
items.push(paymentmethod)
}
return items
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/subscriptions/payment-methods', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
let cachedResponses, cachedPaymentMethods
async function bundledData (retryNumber) {
if (retryNumber > 0) {
cachedResponses = {}
}
if (cachedResponses && cachedResponses.finished) {
return
}
cachedResponses = {}
cachedPaymentMethods = []
await TestHelper.setupBefore()
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
active: 'true'
})
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail
})
for (let i = 0, len = global.pageSize + 2; i < len; i++) {
await TestHelper.createPaymentMethod(user, {
cvc: '111',
number: '4111111111111111',
exp_month: '1',
exp_year: (new Date().getFullYear() + 1).toString().substring(2),
name: user.profile.fullName,
line1: '285 Fulton St',
line2: 'Apt 893',
city: 'New York',
state: 'NY',
postal_code: '90120',
country: 'US',
default: 'true'
})
cachedPaymentMethods.unshift(user.paymentMethod.paymentmethodid)
}
const req1 = TestHelper.createRequest(`/api/user/subscriptions/payment-methods?accountid=${user.account.accountid}&offset=1`)
req1.account = user.account
req1.session = user.session
cachedResponses.offset = await req1.get()
const req2 = TestHelper.createRequest(`/api/user/subscriptions/payment-methods?accountid=${user.account.accountid}&limit=1`)
req2.account = user.account
req2.session = user.session
cachedResponses.limit = await req2.get()
const req3 = TestHelper.createRequest(`/api/user/subscriptions/payment-methods?accountid=${user.account.accountid}&all=true`)
req3.account = user.account
req3.session = user.session
cachedResponses.all = await req3.get()
const req4 = TestHelper.createRequest(`/api/user/subscriptions/payment-methods?accountid=${user.account.accountid}`)
req4.account = user.account
req4.session = user.session
req4.filename = __filename
req4.saveResponse = true
cachedResponses.returns = await req4.get()
global.pageSize = 3
cachedResponses.pageSize = await req4.get()
global.pageSize = 2
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/payment-methods')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/payment-methods?accountid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/payment-methods?accountid=${user.account.accountid}`)
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('receives', () => {
it('optional querystring offset (integer)', async function () {
await bundledData(this.test.currentRetry())
const offset = 1
const paymentMethodsNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(paymentMethodsNow[i].paymentmethodid, cachedPaymentMethods[offset + i])
}
})
it('optional querystring limit (integer)', async function () {
await bundledData(this.test.currentRetry())
const limit = 1
const paymentMethodsNow = cachedResponses.limit
assert.strictEqual(paymentMethodsNow.length, limit)
})
it('optional querystring all (boolean)', async () => {
const paymentMethodsNow = cachedResponses.all
assert.strictEqual(paymentMethodsNow.length, cachedPaymentMethods.length)
})
})
describe('returns', () => {
it('array', async () => {
const paymentMethodsNow = cachedResponses.returns
assert.strictEqual(paymentMethodsNow.length, global.pageSize)
})
})
describe('configuration', () => {
it('environment PAGE_SIZE', async () => {
const paymentMethodsNow = cachedResponses.pageSize
assert.strictEqual(paymentMethodsNow.length, global.pageSize + 1)
})
})
})