/api/user/subscriptions/charge (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.Charge.get(req)Returns object
{
"chargeid": "ch_3LEOZuHHqepMFuCX0UTMt8ua",
"accountid": "acct_ea4aed84175aeda2",
"subscriptionid": "sub_1LEOZuHHqepMFuCXCkbr9KCh",
"customerid": "cus_LwH8phNrMdbEwo",
"paymentmethodid": null,
"invoiceid": "in_1LEOZuHHqepMFuCXp1bWwWjX",
"refundRequested": null,
"refundReason": null,
"refundDenied": null,
"refundDeniedReason": null,
"object": "charge",
"stripeObject": {
"id": "ch_3LEOZuHHqepMFuCX0UTMt8ua",
"object": "charge",
"amount": 1000,
"amount_captured": 1000,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_3LEOZuHHqepMFuCX0fa8olyG",
"billing_details": {
"address": {
"city": "New York",
"country": "US",
"line1": "285 Fulton St",
"line2": "Apt 893",
"postal_code": "10007",
"state": "NY"
},
"email": null,
"name": "Al Harvey",
"phone": null
},
"calculated_statement_descriptor": "PRODUCT253 DESCRIPTION",
"captured": true,
"created": 1656123511,
"currency": "usd",
"customer": "cus_LwH8phNrMdbEwo",
"description": "Subscription creation",
"destination": null,
"dispute": null,
"disputed": false,
"failure_balance_transaction": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {},
"invoice": "in_1LEOZuHHqepMFuCXp1bWwWjX",
"livemode": false,
"metadata": {},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "approved_by_network",
"reason": null,
"risk_level": "normal",
"risk_score": 28,
"seller_message": "Payment complete.",
"type": "authorized"
},
"paid": true,
"payment_intent": "pi_3LEOZuHHqepMFuCX0CbpYvzZ",
"payment_method": "pm_1LEOZrHHqepMFuCXtcclM4TD",
"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/CAcaFwoVYWNjdF8xS2hpUDhISHFlcE1GdUNYKPng2ZUGMgbbqS-sFRs6LBYtwQkzinIPVqSoLpqaGGIl7oJYbbArf41OUWILdQQt_tKVmi0qbZTAR19G?s=ap",
"refunded": false,
"refunds": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_3LEOZuHHqepMFuCX0UTMt8ua/refunds"
},
"review": null,
"shipping": null,
"source": null,
"source_transfer": null,
"statement_descriptor": "product253 description",
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
},
"appid": "tests_1656123505",
"createdAt": "2022-06-25T02:18:33.809Z",
"updatedAt": "2022-06-25T02:18:34.149Z"
}
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-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')
}
if (chargeInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
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/user/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)
const user2 = await TestHelper.createUser()
// invalid account
const req = TestHelper.createRequest(`/api/user/subscriptions/charge?chargeid=${user.charge.chargeid}`)
req.account = user2.account
req.session = user2.session
req.body = {
paymentmethodid: user.paymentMethod.paymentmethodid
}
try {
await req.get()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// returns
const req2 = TestHelper.createRequest(`/api/user/subscriptions/charge?chargeid=${user.charge.chargeid}`)
req2.account = user.account
req2.session = user.session
req2.body = {
paymentmethodid: user.paymentMethod.paymentmethodid
}
req2.filename = __filename
req2.saveResponse = true
cachedResponses.returns = await req2.get()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-chargeid', () => {
it('missing querystring chargeid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/charge')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
it('invalid querystring chargeid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/charge?chargeid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
})
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 charge = cachedResponses.returns
assert.strictEqual(charge.object, 'charge')
})
})
})