/api/administrator/subscriptions/charge (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.Charge.get(req)Returns object
{
"chargeid": "ch_3LEODlHHqepMFuCX0ns0PfCL",
"accountid": "acct_b8668466cb407f64",
"subscriptionid": "sub_1LEODlHHqepMFuCXSnVItOhl",
"customerid": "cus_LwGlwRC1ZNW5Nj",
"paymentmethodid": null,
"invoiceid": "in_1LEODlHHqepMFuCX69jqA8LL",
"refundRequested": null,
"refundReason": null,
"refundDenied": null,
"refundDeniedReason": null,
"object": "charge",
"stripeObject": {
"id": "ch_3LEODlHHqepMFuCX0ns0PfCL",
"object": "charge",
"amount": 1000,
"amount_captured": 1000,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_3LEODlHHqepMFuCX0sm25aMS",
"billing_details": {
"address": {
"city": "New York",
"country": "US",
"line1": "285 Fulton St",
"line2": "Apt 893",
"postal_code": "10007",
"state": "NY"
},
"email": null,
"name": "Tracy Orn",
"phone": null
},
"calculated_statement_descriptor": "PRODUCT133 DESCRIPTION",
"captured": true,
"created": 1656122138,
"currency": "usd",
"customer": "cus_LwGlwRC1ZNW5Nj",
"description": "Subscription creation",
"destination": null,
"dispute": null,
"disputed": false,
"failure_balance_transaction": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {},
"invoice": "in_1LEODlHHqepMFuCX69jqA8LL",
"livemode": false,
"metadata": {},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "approved_by_network",
"reason": null,
"risk_level": "normal",
"risk_score": 40,
"seller_message": "Payment complete.",
"type": "authorized"
},
"paid": true,
"payment_intent": "pi_3LEODlHHqepMFuCX0IoSP2Y8",
"payment_method": "pm_1LEODiHHqepMFuCXTzW9jG1d",
"payment_method_details": {
"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",
"installments": null,
"last4": "1111",
"mandate": null,
"network": "visa",
"three_d_secure": null,
"wallet": null
},
"type": "card"
},
"receipt_email": null,
"receipt_number": null,
"receipt_url": "https://pay.stripe.com/receipts/invoices/CAcaFwoVYWNjdF8xS2hpUDhISHFlcE1GdUNYKJzW2ZUGMgYyq88dj7w6LBZ5JN3IqQ9lNYgdoAuvDAq2cspwRaBfNBSDGRPywSJ2COQprxxAgp3unY0o?s=ap",
"refunded": false,
"refunds": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_3LEODlHHqepMFuCX0ns0PfCL/refunds"
},
"review": null,
"shipping": null,
"source": null,
"source_transfer": null,
"statement_descriptor": "product133 description",
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
},
"appid": "tests_1656122132",
"createdAt": "2022-06-25T01:55:40.649Z",
"updatedAt": "2022-06-25T01:55:40.895Z"
}
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-chargeid | missing querystring chargeid |
invalid querystring chargeid |
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.chargeid) {
throw new Error('invalid-chargeid')
}
let charge = await dashboard.StorageCache.get(req.query.chargeid)
if (!charge) {
const chargeInfo = await subscriptions.Storage.Charge.findOne({
where: {
chargeid: req.query.chargeid,
appid: req.appid || global.appid
}
})
if (!chargeInfo) {
throw new Error('invalid-chargeid')
}
charge = {}
for (const field of chargeInfo._options.attributes) {
charge[field] = chargeInfo.get(field)
}
await dashboard.StorageCache.set(req.query.chargeid, charge)
}
return charge
}
}
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/administrator/subscriptions/charge', 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 administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
// missing and invalid id
const req = TestHelper.createRequest('/api/administrator/subscriptions/charge')
req.account = administrator.account
req.session = administrator.session
try {
await req.get()
} catch (error) {
cachedResponses.missing = error.message
}
const req2 = TestHelper.createRequest('/api/administrator/subscriptions/charge?chargeid=invalid')
req2.account = administrator.account
req2.session = administrator.session
try {
await req2.get()
} catch (error) {
cachedResponses.invalid = error.message
}
// returns
const req3 = TestHelper.createRequest(`/api/administrator/subscriptions/charge?chargeid=${user.charge.chargeid}`)
req3.account = administrator.account
req3.session = administrator.session
req3.filename = __filename
req3.saveResponse = true
cachedResponses.returns = await req3.get()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-chargeid', () => {
it('missing querystring chargeid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
it('invalid querystring chargeid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
})
})
describe('returns', () => {
it('object', async () => {
const charge = cachedResponses.returns
assert.strictEqual(charge.object, 'charge')
})
})
})