/api/user/subscriptions/create-cancelation-refund (POST)
await global.api.user.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-account | ineligible accessing account |
ineligible querystring subscription is free | |
ineligible querystring subscription is in free trial | |
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.user.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionid')
}
if ((subscription.status !== 'active' && subscription.status !== 'trialing') || subscription.cancel_at_period_end) {
throw new Error('invalid-subscription')
}
req.query.invoiceid = subscription.latest_invoice
const invoice = await global.api.user.subscriptions.Invoice.get(req)
if (!invoice.charge) {
throw new Error('invalid-subscription')
}
req.query.chargeid = invoice.charge
const charge = await global.api.user.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/user/subscriptions/create-cancelation-refund', function () {
this.timeout(60 * 60 * 1000)
beforeEach(TestHelper.setupWebhook)
afterEach(TestHelper.deleteOldWebhooks)
describe('exceptions', () => {
describe('invalid-subscriptionid', () => {
it('missing querystring subscriptionid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-cancelation-refund')
req.account = user.account
req.session = user.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 user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-cancelation-refund?subscriptionid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/set-subscription-canceled?subscriptionid=${user.subscription.id}`)
req.account = user2.account
req.session = user2.session
req.body = {
refund: 'at_period_end'
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-subscription', () => {
it('ineligible querystring subscription is not active', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
await TestHelper.cancelSubscription(user)
const req2 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.id}`)
req2.account = user.account
req2.session = user.session
let errorMessage
try {
await req2.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscription')
})
it('ineligible querystring subscription is free', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({ amount: '0' })
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
await TestHelper.cancelSubscription(user)
const req2 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.id}`)
req2.account = user.account
req2.session = user.session
let errorMessage
try {
await req2.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscription')
})
it('ineligible querystring subscription is in free trial', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan({ trial_period_days: '10' })
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
await TestHelper.cancelSubscription(user)
const req2 = TestHelper.createRequest(`/api/user/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.id}`)
req2.account = user.account
req2.session = user.session
let errorMessage
try {
await req2.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/user/subscriptions/create-cancelation-refund?subscriptionid=${user.subscription.id}`)
req.account = user.account
req.session = user.session
req.body = {
refund: 'credit'
}
req.filename = __filename
req.saveResponse = true
const refundNow = await req.post()
assert.strictEqual(refundNow.object, 'refund')
})
})
})