/api/user/subscriptions/refunds (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.Refunds.get(req)Returns array
[
{
"refundid": "re_3LEOjIHHqepMFuCX0VyMknov",
"object": "refund",
"stripeObject": {
"id": "re_3LEOjIHHqepMFuCX0VyMknov",
"object": "refund",
"amount": 3000,
"balance_transaction": "txn_3LEOjIHHqepMFuCX095iG990",
"charge": "ch_3LEOjIHHqepMFuCX0kO0aaLj",
"created": 1656124096,
"currency": "usd",
"metadata": {},
"payment_intent": "pi_3LEOjIHHqepMFuCX0T283yB6",
"reason": "requested_by_customer",
"receipt_number": null,
"source_transfer_reversal": null,
"status": "succeeded",
"transfer_reversal": null
},
"accountid": "acct_ca3e54b0059784df",
"subscriptionid": null,
"customerid": null,
"invoiceid": "in_1LEOjHHHqepMFuCXtAWn0J54",
"productid": null,
"paymentmethodid": null,
"appid": "tests_1656124067",
"createdAt": "2022-06-25T02:28:16.746Z",
"updatedAt": "2022-06-25T02:28:16.746Z"
},
{
"refundid": "re_3LEOjBHHqepMFuCX1KFiECsh",
"object": "refund",
"stripeObject": {
"id": "re_3LEOjBHHqepMFuCX1KFiECsh",
"object": "refund",
"amount": 3000,
"balance_transaction": "txn_3LEOjBHHqepMFuCX1mO9YE0m",
"charge": "ch_3LEOjBHHqepMFuCX1HyALB6y",
"created": 1656124089,
"currency": "usd",
"metadata": {},
"payment_intent": "pi_3LEOjBHHqepMFuCX1rIM28jR",
"reason": "requested_by_customer",
"receipt_number": null,
"source_transfer_reversal": null,
"status": "succeeded",
"transfer_reversal": null
},
"accountid": "acct_ca3e54b0059784df",
"subscriptionid": null,
"customerid": null,
"invoiceid": "in_1LEOjBHHqepMFuCX6VKfF0To",
"productid": null,
"paymentmethodid": null,
"appid": "tests_1656124067",
"createdAt": "2022-06-25T02:28:10.273Z",
"updatedAt": "2022-06-25T02:28:10.274Z"
},
{
"refundid": "re_3LEOj5HHqepMFuCX11eh4H21",
"object": "refund",
"stripeObject": {
"id": "re_3LEOj5HHqepMFuCX11eh4H21",
"object": "refund",
"amount": 3000,
"balance_transaction": "txn_3LEOj5HHqepMFuCX1Y5mRANO",
"charge": "ch_3LEOj5HHqepMFuCX1FtY6IBB",
"created": 1656124083,
"currency": "usd",
"metadata": {},
"payment_intent": "pi_3LEOj5HHqepMFuCX1cX6N2Lk",
"reason": "requested_by_customer",
"receipt_number": null,
"source_transfer_reversal": null,
"status": "succeeded",
"transfer_reversal": null
},
"accountid": "acct_ca3e54b0059784df",
"subscriptionid": null,
"customerid": null,
"invoiceid": "in_1LEOj4HHqepMFuCXKyx8PcYk",
"productid": null,
"paymentmethodid": null,
"appid": "tests_1656124067",
"createdAt": "2022-06-25T02:28:03.548Z",
"updatedAt": "2022-06-25T02:28:03.548Z"
}
]
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 account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-account')
}
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-customerid')
}
where.customerid = req.query.customerid
} else if (req.query.subscriptionid) {
const subscription = await global.api.user.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionid')
}
where.subscriptionid = req.query.subscriptionid
} else {
where.accountid = req.query.accountid
}
let refundids
if (req.query.all) {
refundids = await subscriptions.Storage.Refund.findAll({
where,
attributes: ['refundid'],
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
refundids = await subscriptions.Storage.Refund.findAll({
where,
attributes: ['refundid'],
offset,
limit,
order: [
['createdAt', 'DESC']
]
})
}
if (!refundids || !refundids.length) {
return null
}
const items = []
for (const refundInfo of refundids) {
req.query.refundid = refundInfo.dataValues.refundid
const item = await global.api.user.subscriptions.Refund.get(req)
items.push(item)
}
return items
}
}
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/refunds', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
let cachedResponses, cachedRefunds
async function bundledData (retryNumber) {
if (retryNumber > 0) {
cachedResponses = {}
}
if (cachedResponses && cachedResponses.finished) {
return
}
cachedResponses = {}
cachedRefunds = []
await TestHelper.setupBefore()
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
active: 'true'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
for (let i = 0, len = global.pageSize + 2; i < len; i++) {
await TestHelper.createPrice(administrator, {
productid: administrator.product.productid,
currency: 'usd',
unit_amount: 3000,
recurring_interval: 'month',
recurring_interval_count: '1',
recurring_usage_type: 'licensed',
active: 'true',
tax_behavior: 'inclusive'
})
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
await TestHelper.createRefund(administrator, user.charge.chargeid)
cachedRefunds.unshift(administrator.refund.refundid)
}
const req1 = TestHelper.createRequest(`/api/user/subscriptions/refunds?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/refunds?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/refunds?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/refunds?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/refunds')
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/refunds?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/refunds?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 refundsNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(refundsNow[i].refundid, cachedRefunds[offset + i])
}
})
it('optional querystring limit (integer)', async function () {
await bundledData(this.test.currentRetry())
const limit = 1
const refundsNow = cachedResponses.limit
assert.strictEqual(refundsNow.length, limit)
})
it('optional querystring all (boolean)', async () => {
const refundsNow = cachedResponses.all
assert.strictEqual(refundsNow.length, cachedRefunds.length)
})
})
describe('returns', () => {
it('array', async () => {
const refundsNow = cachedResponses.returns
assert.strictEqual(refundsNow.length, global.pageSize)
})
})
describe('configuration', () => {
it('environment PAGE_SIZE', async () => {
const refundsNow = cachedResponses.pageSize
assert.strictEqual(refundsNow.length, global.pageSize + 1)
})
})
})