/api/administrator/subscriptions/create-cancelation-refund (POST)
await global.api.administrator.subscriptions.CreateCancelationRefund.post(req) Located in Stripe Subscriptions module API
Returns object
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 querystring subscriptionid | |
invalid-subscriptionid | missing querystring subscriptionid |
invalid-subscription | ineligible querystring subscription is not active |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request 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.latest_invoice
const invoice = await global.api.administrator.subscriptions.Invoice.get(req)
if (!invoice.charge) {
throw new Error('invalid-subscription')
}
if ((subscription.status !== 'active' && subscription.status !== 'trialing') || subscription.cancel_at_period_end) {
throw new Error('invalid-subscription')
}
req.query.chargeid = invoice.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.items.data) {
zeroed.push({ id: item.id, quantity: 0 })
}
const upcoming = await stripeCache.execute('invoices', 'retrieveUpcoming', {
customer: subscription.customer.id || subscription.customer,
subscription: req.query.subscriptionid,
subscription_items: zeroed
}, req.stripeKey)
if (upcoming.total === 0) {
throw new Error('invalid-subscription')
}
const refundInfo = {
charge: invoice.charge,
amount: -upcoming.total,
reason: 'requested_by_customer',
metadata: {
appid: req.appid,
accountid: req.account.accountid
}
}
const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
await stripeCache.update(refund)
await subscriptions.StorageList.addMany({
[`${req.appid}/refunds`]: refund.id,
[`${req.appid}/paymentMethod/refunds/${charge.payment_method}`]: refund.id,
[`${req.appid}/customer/refunds/${subscription.customer.id || subscription.customer}`]: refund.id,
[`${req.appid}/plan/refunds/${subscription.plan.id}`]: refund.id,
[`${req.appid}/product/refunds/${subscription.plan.product}`]: refund.id,
[`${req.appid}/account/refunds/${req.account.accountid}`]: refund.id
})
return refund
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
describe('/api/administrator/subscriptions/create-cancelation-refund', function () {
this.timeout(60 * 60 * 1000)
before(TestHelper.setupWebhook)
after(TestHelper.deleteOldWebhooks)
describe('exceptions', () => {
describe('invalid-subscriptionid', () => {
it('missing querystring subscriptionid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-cancelation-refund')
req.account = administrator.account
req.session = administrator.session
req.body = {
refund: 'at_period_end'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
it('invalid querystring subscriptionid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/create-cancelation-refund?subscriptionid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
})
describe('invalid-subscription', () => {
it('ineligible querystring subscription is not active', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
published: 'true'
})
await TestHelper.createPlan(administrator, {
productid: administrator.product.id,
usage_type: 'licensed',
published: 'true',
amount: '10000',
trial_period_days: '0'
})
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
await TestHelper.cancelSubscription(user)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.id}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscription')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.id}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
refund: 'credit'
}
req.filename = __filename
req.saveResponse = true
const refund = await req.post()
assert.strictEqual(refund.object, 'refund')
})
})
})