/api/user/subscriptions/create-refund-request (POST)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.CreateRefundRequest.post(req)Returns object
{
"chargeid": "ch_3LEOcHHHqepMFuCX19Y9lFZA",
"accountid": "acct_697741f7bc0161ea",
"subscriptionid": "sub_1LEOcGHHqepMFuCX71QsL36e",
"customerid": "cus_LwHA6vW0yPyvpS",
"paymentmethodid": null,
"invoiceid": "in_1LEOcHHHqepMFuCX0TvcmqTE",
"refundRequested": "2022-06-25T02:21:00.488Z",
"refundReason": "refund plz",
"refundDenied": null,
"refundDeniedReason": null,
"object": "charge",
"stripeObject": {
"id": "ch_3LEOcHHHqepMFuCX19Y9lFZA",
"object": "charge",
"amount": 1000,
"amount_captured": 1000,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_3LEOcHHHqepMFuCX1g1pVWuB",
"billing_details": {
"address": {
"city": "New York",
"country": "US",
"line1": "285 Fulton St",
"line2": "Apt 893",
"postal_code": "10007",
"state": "NY"
},
"email": null,
"name": "Jaime Hills",
"phone": null
},
"calculated_statement_descriptor": "PRODUCT257 DESCRIPTION",
"captured": true,
"created": 1656123657,
"currency": "usd",
"customer": "cus_LwHA6vW0yPyvpS",
"description": "Subscription creation",
"destination": null,
"dispute": null,
"disputed": false,
"failure_balance_transaction": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {},
"invoice": "in_1LEOcHHHqepMFuCX0TvcmqTE",
"livemode": false,
"metadata": {},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "approved_by_network",
"reason": null,
"risk_level": "normal",
"risk_score": 36,
"seller_message": "Payment complete.",
"type": "authorized"
},
"paid": true,
"payment_intent": "pi_3LEOcHHHqepMFuCX1SHGl1V9",
"payment_method": "pm_1LEOc2HHqepMFuCXxLrK6FqU",
"payment_method_details": {
"card": {
"brand": "visa",
"checks": {
"address_line1_check": "pass",
"address_postal_code_check": "pass",
"cvc_check": "pass"
},
"country": "US",
"exp_month": 1,
"exp_year": 2023,
"fingerprint": "IRcdqfBUCskmPkNV",
"funding": "credit",
"installments": null,
"last4": "1111",
"mandate": null,
"network": "visa",
"three_d_secure": null,
"wallet": null
},
"type": "card"
},
"receipt_email": null,
"receipt_number": null,
"receipt_url": "https://pay.stripe.com/receipts/invoices/CAcaFwoVYWNjdF8xS2hpUDhISHFlcE1GdUNYKIvi2ZUGMgY7c2JRZx86LBaAS-V2QauUBoym-w7pyZJVPJZ44qKmpMdkh3X2iXbAfYpOHs4jFHhhj9H3?s=ap",
"refunded": false,
"refunds": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_3LEOcHHHqepMFuCX19Y9lFZA/refunds"
},
"review": null,
"shipping": null,
"source": null,
"source_transfer": null,
"statement_descriptor": "product257 description",
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
},
"appid": "tests_1656123640",
"createdAt": "2022-06-25T02:20:59.850Z",
"updatedAt": "2022-06-25T02:21:00.488Z"
}
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-account | ineligible accessing account |
invalid-charge | ineligible querystring charge is refunded |
ineligible querystring charge has refund request | |
invalid-chargeid | missing querystring chargeid |
invalid querystring chargeid |
NodeJS source (view on github)
const subscriptions = require('../../../../../index.js')
module.exports = {
post: async (req) => {
if (!req.query || !req.query.chargeid) {
throw new Error('invalid-chargeid')
}
if (!req.body || !req.body.reason) {
throw new Error('invalid-reason')
}
if (!req.body.reason.length || req.body.reason.length > 200) {
throw new Error('invalid-reason-length')
}
const charge = await global.api.user.subscriptions.Charge.get(req)
if (!charge.stripeObject.amount || !charge.stripeObject.paid || charge.stripeObject.refunded) {
throw new Error('invalid-charge')
}
if (charge.refundRequested) {
throw new Error('invalid-charge')
}
await subscriptions.Storage.Charge.update({
refundRequested: new Date(),
refundReason: req.body.reason
}, {
where: {
chargeid: req.query.chargeid,
appid: req.appid || global.appid
}
})
return global.api.user.subscriptions.Charge.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/user/subscriptions/create-refund-request', 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()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
// missing or invalid id
const req = TestHelper.createRequest('/api/user/subscriptions/create-refund-request')
req.account = user.account
req.session = user.session
req.body = {
reason: 'refund plz'
}
try {
await req.post()
} catch (error) {
cachedResponses.missing = error.message
}
const req2 = TestHelper.createRequest('/api/user/subscriptions/create-refund-request?chargeid=invalid')
req2.account = user.account
req2.session = user.session
req2.body = {
reason: 'refund plz'
}
try {
await req2.post()
} catch (error) {
cachedResponses.invalid = error.message
}
// invalid charge
await TestHelper.createRefund(administrator, user.charge.chargeid)
const req3 = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.chargeid}`)
req3.account = user.account
req3.session = user.session
req3.body = {
reason: 'refund plz'
}
try {
await req3.post()
} catch (error) {
cachedResponses.invalidCharge = error.message
}
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
await TestHelper.requestRefund(user, user.charge.chargeid)
const req4 = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.chargeid}`)
req4.account = user.account
req4.session = user.session
req4.body = {
reason: 'refund plz'
}
try {
await req4.post()
} catch (error) {
cachedResponses.invalidCharge2 = error.message
}
// invalid account
await TestStripeAccounts.createUserWithPaidSubscription(administrator.price, user)
const user2 = await TestHelper.createUser()
const req5 = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.chargeid}`)
req5.account = user2.account
req5.session = user2.session
req5.body = {
reason: 'refund plz'
}
try {
await req5.post()
} catch (error) {
cachedResponses.invalidAccount = error.message
}
// returns
const req6 = TestHelper.createRequest(`/api/user/subscriptions/create-refund-request?chargeid=${user.charge.chargeid}`)
req6.account = user.account
req6.session = user.session
req6.body = {
reason: 'refund plz'
}
req6.filename = __filename
req6.saveResponse = true
cachedResponses.returns = await req6.post()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-chargeid', () => {
it('missing querystring chargeid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
it('invalid querystring chargeid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-chargeid')
})
})
describe('invalid-charge', () => {
it('ineligible querystring charge is refunded', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidCharge
assert.strictEqual(errorMessage, 'invalid-charge')
})
it('ineligible querystring charge has refund request', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidCharge2
assert.strictEqual(errorMessage, 'invalid-charge')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidAccount
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const charge = cachedResponses.returns
assert.strictEqual(charge.object, 'charge')
})
})
})