/api/administrator/subscriptions/remove-subscription-item-tax-rate (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.RemoveSubscriptionItemTaxRate.get(req)Returns object
{
"subscriptionid": "sub_1LEOPhHHqepMFuCXVLZCRhkq",
"object": "subscription",
"stripeObject": {
"id": "sub_1LEOPhHHqepMFuCXVLZCRhkq",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": {
"enabled": false
},
"billing_cycle_anchor": 1656122877,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "charge_automatically",
"created": 1656122877,
"current_period_end": 1658714877,
"current_period_start": 1656122877,
"customer": "cus_LwGxMUnYXToePY",
"days_until_due": null,
"default_payment_method": "pm_1LEOPeHHqepMFuCXnAednTQz",
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_LwGxek3rPUZpSS",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656122878,
"metadata": {},
"plan": {
"id": "price_1LEOPdHHqepMFuCXuy2yUpFI",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656122873,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwGxhQPyzNvkvw",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOPdHHqepMFuCXuy2yUpFI",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656122873,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwGxhQPyzNvkvw",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 1000,
"unit_amount_decimal": "1000"
},
"quantity": 1,
"subscription": "sub_1LEOPhHHqepMFuCXVLZCRhkq",
"tax_rates": [
{
"id": "txr_1LEOPlHHqepMFuCXx4yezUu8",
"object": "tax_rate",
"active": true,
"country": "US",
"created": 1656122881,
"description": "Sales tax in NY",
"display_name": "NY Sales Tax",
"inclusive": true,
"jurisdiction": "US",
"livemode": false,
"metadata": {},
"percentage": 17.5,
"state": "NY",
"tax_type": "sales_tax"
}
]
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_1LEOPhHHqepMFuCXVLZCRhkq"
},
"latest_invoice": "in_1LEOPhHHqepMFuCX4QEIXuPc",
"livemode": false,
"metadata": {},
"next_pending_invoice_item_invoice": null,
"pause_collection": null,
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null,
"save_default_payment_method": "off"
},
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"plan": {
"id": "price_1LEOPdHHqepMFuCXuy2yUpFI",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656122873,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwGxhQPyzNvkvw",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start_date": 1656122877,
"status": "active",
"test_clock": null,
"transfer_data": null,
"trial_end": null,
"trial_start": null
},
"customerid": "cus_LwGxMUnYXToePY",
"accountid": "acct_83da82535d6d842b",
"paymentmethodid": null,
"productid": null,
"priceids": [
"price_1LEOPdHHqepMFuCXuy2yUpFI"
],
"couponid": null,
"appid": "tests_1656122873",
"createdAt": "2022-06-25T02:07:59.789Z",
"updatedAt": "2022-06-25T02:08:06.701Z"
}
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-subscriptionitemid | missing querystring subscriptionitemid |
invalid querystring subscriptionitemid | |
invalid-tax-rate | invalid posted tax-rate |
invalid-taxrateid | missing posted taxrateid |
invalid posted taxrateid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.subscriptionitemid) {
throw new Error('invalid-subscriptionitemid')
}
if (!req.body || !req.body.taxrateid) {
throw new Error('invalid-taxrateid')
}
req.query.taxrateid = req.body.taxrateid
const taxRate = await global.api.administrator.subscriptions.TaxRate.get(req)
if (!taxRate.stripeObject.active) {
throw new Error('invalid-tax-rate')
}
const subscriptionItemBefore = await stripeCache.execute('subscriptionItems', 'retrieve', req.query.subscriptionitemid, req.stripeKey)
const taxRatesRemaining = []
if (subscriptionItemBefore.tax_rates.length) {
for (const item of subscriptionItemBefore.tax_rates) {
if (item.id !== req.body.taxrateid) {
taxRatesRemaining.push(item.id)
}
}
}
// clear the taxes
const subscriptionItem = await stripeCache.execute('subscriptionItems', 'update', req.query.subscriptionitemid, {
tax_rates: ''
}, req.stripeKey)
// add any remaining taxes back
if (taxRatesRemaining.length) {
await stripeCache.execute('subscriptionItems', 'update', req.query.subscriptionitemid, {
tax_rates: taxRatesRemaining
}, req.stripeKey)
}
const subscriptionNow = await stripeCache.execute('subscriptions', 'retrieve', subscriptionItem.subscription, req.stripeKey)
await subscriptions.Storage.Subscription.update({
stripeObject: subscriptionNow
}, {
where: {
subscriptionid: subscriptionNow.id
}
})
await dashboard.StorageCache.remove(subscriptionNow.id)
req.query.subscriptionid = subscriptionNow.id
return global.api.administrator.subscriptions.Subscription.get(req)
}
}
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/remove-subscription-item-tax-rate', 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 taxRate1 = await TestHelper.createTaxRate(administrator)
const taxRate2 = await TestHelper.createTaxRate(administrator)
const taxRate3 = await TestHelper.createTaxRate(administrator, {
active: 'false'
})
// missing and invalid id
let req = TestHelper.createRequest('/api/administrator/subscriptions/remove-subscription-item-tax-rate')
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: taxRate1.taxrateid
}
try {
await req.patch()
} catch (error) {
cachedResponses.missing = error.message
}
req = TestHelper.createRequest('/api/administrator/subscriptions/remove-subscription-item-tax-rate?subscriptionitemid=invalid')
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: taxRate1.taxrateid
}
try {
await req.patch()
} catch (error) {
cachedResponses.invalid = error.message
}
// invalid taxrateid
req = TestHelper.createRequest(`/api/administrator/subscriptions/remove-subscription-item-tax-rate?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: ''
}
try {
await req.patch()
} catch (error) {
cachedResponses.missingTaxRateId = error.message
}
req = TestHelper.createRequest(`/api/administrator/subscriptions/remove-subscription-item-tax-rate?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: 'invalid'
}
try {
await req.patch()
} catch (error) {
cachedResponses.invalidTaxRateId = error.message
}
// invalid tax rate
req = TestHelper.createRequest(`/api/administrator/subscriptions/remove-subscription-item-tax-rate?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: taxRate3.taxrateid
}
try {
await req.patch()
} catch (error) {
cachedResponses.invalidTaxRate = error.message
}
// non-present tax rate
req = TestHelper.createRequest(`/api/administrator/subscriptions/remove-subscription-item-tax-rate?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: taxRate1.taxrateid
}
try {
await req.patch()
} catch (error) {
cachedResponses.invalidTaxRate = error.message
}
// returns
await TestHelper.addSubscriptionItemTaxRate(administrator, user.subscription.stripeObject.items.data[0].id, taxRate1.taxrateid)
await TestHelper.addSubscriptionItemTaxRate(administrator, user.subscription.stripeObject.items.data[0].id, taxRate2.taxrateid)
req = TestHelper.createRequest(`/api/administrator/subscriptions/remove-subscription-item-tax-rate?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
taxrateid: taxRate1.taxrateid
}
req.filename = __filename
req.saveResponse = true
cachedResponses.returns = await req.patch()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-subscriptionitemid', () => {
it('missing querystring subscriptionitemid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
it('invalid querystring subscriptionitemid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
})
describe('invalid-taxrateid', () => {
it('missing posted taxrateid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missingTaxRateId
assert.strictEqual(errorMessage, 'invalid-taxrateid')
})
it('invalid posted taxrateid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidTaxRateId
assert.strictEqual(errorMessage, 'invalid-taxrateid')
})
})
describe('invalid-tax-rate', () => {
it('invalid posted tax-rate', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidTaxRate
assert.strictEqual(errorMessage, 'invalid-tax-rate')
})
})
})
describe('returns', () => {
it('object', async () => {
const subscriptionNow = cachedResponses.returns
assert.strictEqual(subscriptionNow.stripeObject.items.data[0].tax_rates.length, 1)
})
})
})