/api/user/subscriptions/create-refund (POST)
await global.api.user.subscriptions.CreateRefund.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 chargeid | |
invalid-chargeid | missing querystring chargeid |
ineligible querystring charge is out of refund period | |
invalid-charge | ineligible querystring charge is refunded |
invalid-account | ineligible accessing account |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const dashboard = require('@userdashboard/dashboard')
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
post: async (req) => {
if (!req.query || !req.query.chargeid) {
throw new Error('invalid-chargeid')
}
const charge = await global.api.user.subscriptions.Charge.get(req)
if (!charge.amount || !charge.paid || charge.refunded || !global.subscriptionRefundPeriod ||
charge.created <= dashboard.Timestamp.now - global.subscriptionRefundPeriod) {
throw new Error('invalid-charge')
}
req.query.invoiceid = charge.invoice
const invoice = await global.api.user.subscriptions.Invoice.get(req)
const refundInfo = {
charge: req.query.chargeid,
amount: charge.amount - (charge.amount_refunded || 0),
reason: 'requested_by_customer',
metadata: {
appid: req.appid,
accountid: req.account.accountid
}
}
req.query.subscriptionid = invoice.subscription
const subscription = await global.api.user.subscriptions.Subscription.get(req)
if (subscription.status === 'active') {
const subscriptionNow = await stripeCache.execute('subscriptions', 'del', subscription.id, { prorate: false }, req.stripeKey)
await stripeCache.update(subscriptionNow)
await subscriptions.StorageList.remove(`${req.appid}/subscriptions`, subscription.id)
await subscriptions.StorageList.remove(`${req.appid}/account/subscriptions/${req.account.accountid}`, req.query.subscriptionid)
await subscriptions.StorageList.remove(`${req.appid}/plan/subscriptions/${subscription.plan.id}`, req.query.subscriptionid)
await subscriptions.StorageList.remove(`${req.appid}/product/subscriptions/${subscription.plan.product}`, req.query.subscriptionid)
}
const refund = await stripeCache.execute('refunds', 'create', refundInfo, req.stripeKey)
await stripeCache.update(refund)
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-refund', function () {
this.timeout(60 * 60 * 1000)
beforeEach(TestHelper.setupWebhook)
afterEach(TestHelper.deleteOldWebhooks)
describe('exceptions', () => {
describe('invalid-chargeid', () => {
it('missing querystring chargeid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-refund')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
it('invalid querystring chargeid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/create-refund?chargeid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
})
describe('invalid-charge', () => {
it('ineligible querystring charge is refunded', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const user = await TestStripeAccounts.createUserWithPaymentMethod()
await TestHelper.createSubscription(user, administrator.plan.id)
await TestHelper.createRefund(administrator, user.charge.id)
const req = TestHelper.createRequest(`/api/user/subscriptions/create-refund?chargeid=${user.charge.id}`)
req.account = user.account
req.session = user.session
req.body = {
amount: '10000'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-charge')
})
it('ineligible querystring charge is out of refund period', async () => {
global.subscriptionRefundPeriod = 0
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-refund?chargeid=${user.charge.id}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-charge')
})
})
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()
await TestHelper.createCustomer(user2, {
email: user.profile.contactEmail,
description: user.profile.firstName,
country: 'US'
})
const req = TestHelper.createRequest(`/api/user/subscriptions/create-refund?chargeid=${user.charge.id}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
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-refund?chargeid=${user.charge.id}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const refund = await req.post()
assert.strictEqual(refund.object, 'refund')
})
})
describe('configuration', () => {
it('environment SUBSCRIPTION_REFUND_PERIOD', async () => {
global.subscriptionRefundPeriod = 7 * 24 * 60 * 60
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-refund?chargeid=${user.charge.id}`)
req.account = user.account
req.session = user.session
const refund = await req.post()
assert.strictEqual(refund.object, 'refund')
})
})
})