/api/administrator/subscriptions/create-cancelation-refund (POST)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.CreateCancelationRefund.post(req)Returns object
{
"refundid": "re_3LEOFTHHqepMFuCX0F4TyAoZ",
"object": "refund",
"stripeObject": {
"id": "re_3LEOFTHHqepMFuCX0F4TyAoZ",
"object": "refund",
"amount": 1000,
"balance_transaction": "txn_3LEOFTHHqepMFuCX07IyCtDw",
"charge": "ch_3LEOFTHHqepMFuCX0KMFyrma",
"created": 1656122247,
"currency": "usd",
"metadata": {},
"payment_intent": "pi_3LEOFTHHqepMFuCX0wri3vxv",
"reason": "requested_by_customer",
"receipt_number": null,
"source_transfer_reversal": null,
"status": "succeeded",
"transfer_reversal": null
},
"accountid": "acct_c1df1d539a530127",
"subscriptionid": "sub_1LEOFSHHqepMFuCXPnL3Xh4B",
"customerid": "cus_LwGmr3tuGEOVyz",
"invoiceid": null,
"productid": null,
"paymentmethodid": null,
"appid": "tests_1656122233",
"createdAt": "2022-06-25T01:57:28.137Z",
"updatedAt": "2022-06-25T01:57:28.137Z"
}
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 subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
post: 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')
}
req.query.invoiceid = subscription.stripeObject.latest_invoice
const invoice = await global.api.administrator.subscriptions.Invoice.get(req)
if (!invoice.stripeObject.charge) {
throw new Error('invalid-subscription')
}
if ((subscription.stripeObject.status !== 'active' && subscription.stripeObject.status !== 'trialing') || subscription.stripeObject.cancel_at_period_end) {
throw new Error('invalid-subscription')
}
req.query.chargeid = invoice.stripeObject.charge
const charge = await global.api.administrator.subscriptions.Charge.get(req)
if (!charge) {
throw new Error('invalid-subscription')
}
const zeroed = []
for (const item of subscription.stripeObject.items.data) {
zeroed.push({ id: item.id, quantity: 0 })
}
const upcoming = await stripeCache.execute('invoices', 'retrieveUpcoming', {
customer: invoice.customerid,
subscription: req.query.subscriptionid,
subscription_items: zeroed
}, req.stripeKey)
if (upcoming.total === 0) {
throw new Error('invalid-subscription')
}
const refundInfo = {
charge: invoice.stripeObject.charge,
amount: -upcoming.total,
reason: 'requested_by_customer'
}
const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
await subscriptions.Storage.Refund.create({
appid: req.appid || global.appid,
refundid: refund.id,
accountid: subscription.accountid,
subscriptionid: subscription.subscriptionid,
customerid: subscription.customerid,
stripeObject: refund
})
req.query.refundid = refund.id
return global.api.administrator.subscriptions.Refund.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/create-cancelation-refund', 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/create-cancelation-refund')
req.account = administrator.account
req.session = administrator.session
req.body = {
refund: 'at_period_end'
}
try {
await req.post()
} catch (error) {
cachedResponses.missing = error.message
}
const req2 = TestHelper.createRequest('/api/administrator/subscriptions/create-cancelation-refund?subscriptionid=invalid')
req2.account = administrator.account
req2.session = administrator.session
try {
await req2.post()
} 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/create-cancelation-refund?subscriptionid=${user.subscription.subscriptionid}`)
req3.account = administrator.account
req3.session = administrator.session
try {
await req3.post()
} catch (error) {
cachedResponses.invalidSubscription = error.message
}
// returns
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
const req4 = TestHelper.createRequest(`/api/administrator/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.subscriptionid}`)
req4.account = administrator.account
req4.session = administrator.session
req4.body = {
refund: 'credit'
}
req4.filename = __filename
req4.saveResponse = true
cachedResponses.returns = await req4.post()
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 refund = cachedResponses.returns
assert.strictEqual(refund.object, 'refund')
})
})
})