/api/administrator/subscriptions/set-subscription-canceled (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.SetSubscriptionCanceled.patch(req)Returns object
{
"subscriptionid": "sub_1LEOSsHHqepMFuCX0vFNk0l2",
"object": "subscription",
"stripeObject": {
"id": "sub_1LEOSsHHqepMFuCX0vFNk0l2",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": {
"enabled": false
},
"billing_cycle_anchor": 1656123073,
"billing_thresholds": null,
"cancel_at": 1658715073,
"cancel_at_period_end": true,
"canceled_at": 1656123078,
"collection_method": "charge_automatically",
"created": 1656123073,
"current_period_end": 1658715073,
"current_period_start": 1656123073,
"customer": "cus_LwH0AoIyTPw8Sj",
"days_until_due": null,
"default_payment_method": "pm_1LEOSkHHqepMFuCXIy0G5M66",
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_LwH0uzVLsiatqO",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656123074,
"metadata": {},
"plan": {
"id": "price_1LEOSjHHqepMFuCXd47X9tRG",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656123065,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwH0BkFFkvEMQv",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOSjHHqepMFuCXd47X9tRG",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656123065,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwH0BkFFkvEMQv",
"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_1LEOSsHHqepMFuCX0vFNk0l2",
"tax_rates": []
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_1LEOSsHHqepMFuCX0vFNk0l2"
},
"latest_invoice": "in_1LEOSsHHqepMFuCXTI4cuzPV",
"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_1LEOSjHHqepMFuCXd47X9tRG",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656123065,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwH0BkFFkvEMQv",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start_date": 1656123073,
"status": "active",
"test_clock": null,
"transfer_data": null,
"trial_end": null,
"trial_start": null
},
"customerid": "cus_LwH0AoIyTPw8Sj",
"accountid": "acct_159b14548614cb0d",
"paymentmethodid": null,
"productid": null,
"priceids": [
"price_1LEOSjHHqepMFuCXd47X9tRG"
],
"couponid": null,
"appid": "tests_1656123064",
"createdAt": "2022-06-25T02:11:16.446Z",
"updatedAt": "2022-06-25T02:11:18.330Z"
}
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-subscription | ineligible querystring subscription is not active |
invalid-subscriptionid | missing querystring subscriptionid |
invalid querystring subscriptionid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.subscriptionid) {
throw new Error('invalid-subscriptionid')
}
const subscription = await global.api.administrator.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionid')
}
if ((subscription.stripeObject.status !== 'active' && subscription.stripeObject.status !== 'trialing') || subscription.stripeObject.cancel_at_period_end) {
throw new Error('invalid-subscription')
}
const delayed = {
cancel_at_period_end: true
}
const subscriptionNow = await stripeCache.execute('subscriptions', 'update', req.query.subscriptionid, delayed, req.stripeKey)
await subscriptions.Storage.Subscription.update({
stripeObject: subscriptionNow
}, {
where: {
subscriptionid: req.query.subscriptionid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.subscriptionid)
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/set-subscription-canceled', 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()
// missing and invalid id
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-subscription-canceled')
req.account = administrator.account
req.session = administrator.session
try {
await req.patch()
} catch (error) {
cachedResponses.missing = error.message
}
const req2 = TestHelper.createRequest('/api/administrator/subscriptions/set-subscription-canceled?subscriptionid=invalid')
req2.account = administrator.account
req2.session = administrator.session
try {
await req2.patch()
} catch (error) {
cachedResponses.invalid = error.message
}
// invalid subscription
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
await TestHelper.cancelSubscription(user)
const req3 = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-canceled?subscriptionid=${user.subscription.subscriptionid}`)
req3.account = administrator.account
req3.session = administrator.session
req3.body = {
refund: 'credit'
}
try {
await req3.patch()
} catch (error) {
cachedResponses.invalidSubscription = error.message
}
// returns
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
const req4 = TestHelper.createRequest(`/api/administrator/subscriptions/set-subscription-canceled?subscriptionid=${user.subscription.subscriptionid}`)
req4.account = administrator.account
req4.session = administrator.session
req4.body = {
refund: 'refund'
}
req4.filename = __filename
req4.saveResponse = true
cachedResponses.returns = await req4.patch()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-subscriptionid', () => {
it('missing querystring subscriptionid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
it('invalid querystring subscriptionid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
})
describe('invalid-subscription', () => {
it('ineligible querystring subscription is not active', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidSubscription
assert.strictEqual(errorMessage, 'invalid-subscription')
})
})
})
describe('returns', () => {
it('object', async () => {
const subscriptionNow = cachedResponses.returns
assert.strictEqual(subscriptionNow.stripeObject.cancel_at_period_end, true)
})
})
})