/api/administrator/subscriptions/set-customer-coupon (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.SetCustomerCoupon.patch(req)Returns object
{
"customerid": "cus_LwGzy408zZqE4l",
"object": "customer",
"accountid": "acct_78f4cbe67af10669",
"couponid": "COUPON42",
"stripeObject": {
"id": "cus_LwGzy408zZqE4l",
"object": "customer",
"address": null,
"balance": 0,
"created": 1656122961,
"currency": "usd",
"default_source": null,
"delinquent": false,
"description": "Visa",
"discount": {
"id": "di_1LEORDHHqepMFuCXBrGZVj6o",
"object": "discount",
"checkout_session": null,
"coupon": {
"id": "COUPON42",
"object": "coupon",
"amount_off": null,
"created": 1656122951,
"currency": null,
"duration": "repeating",
"duration_in_months": 3,
"livemode": false,
"max_redemptions": null,
"metadata": {},
"name": "coupon name",
"percent_off": 41,
"redeem_by": null,
"times_redeemed": 2,
"valid": true
},
"customer": "cus_LwGzy408zZqE4l",
"end": 1664071771,
"invoice": null,
"invoice_item": null,
"promotion_code": null,
"start": 1656122971,
"subscription": null
},
"email": "Jeff_Windler@hotmail.com",
"invoice_prefix": "216AE3CE",
"invoice_settings": {
"custom_fields": null,
"default_payment_method": "pm_1LEOR3HHqepMFuCX04IgspqD",
"footer": null,
"rendering_options": null
},
"livemode": false,
"metadata": {},
"name": null,
"next_invoice_sequence": 2,
"phone": null,
"preferred_locales": [],
"shipping": null,
"tax_exempt": "none",
"test_clock": null
},
"appid": "tests_1656122950",
"createdAt": "2022-06-25T02:09:21.511Z",
"updatedAt": "2022-06-25T02:09:31.361Z"
}
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-couponid | missing posted couponid |
invalid posted couponid | |
invalid-customer | ineligible customer has coupon |
invalid-customerid | missing querystring customerid |
invalid querystring customerid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.customerid) {
throw new Error('invalid-customerid')
}
if (!req.body || !req.body.couponid) {
throw new Error('invalid-couponid')
}
const customer = await global.api.administrator.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-customerid')
}
if (customer.stripeObject.discount && customer.stripeObject.discount.coupon && customer.stripeObject.discount.coupon.id) {
throw new Error('invalid-customer')
}
req.query.couponid = req.body.couponid
const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
if (!coupon) {
throw new Error('invalid-couponid')
}
const customerInfo = {
coupon: req.body.couponid
}
const customerNow = await stripeCache.execute('customers', 'update', req.query.customerid, customerInfo, req.stripeKey)
await subscriptions.Storage.Customer.update({
stripeObject: customerNow,
couponid: req.body.couponid
}, {
where: {
customerid: req.query.customerid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.customerid)
return global.api.administrator.subscriptions.Customer.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/set-customer-coupon', 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 coupon = await TestHelper.createCoupon(administrator, {
duration: 'repeating',
duration_in_months: '3'
})
// missing and invalid customer id
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-customer-coupon')
req.account = administrator.account
req.session = administrator.session
req.body = {
couponid: 'fake'
}
try {
await req.patch()
} catch (error) {
cachedResponses.missing = error.message
}
const req2 = TestHelper.createRequest('/api/administrator/subscriptions/set-customer-coupon?customerid=invalid')
req2.account = administrator.account
req2.session = administrator.session
req2.body = {
couponid: 'fake'
}
try {
await req2.patch()
} catch (error) {
cachedResponses.invalid = error.message
}
// invalid customer
const user2 = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
await TestHelper.createCustomerDiscount(administrator, user2.customer, coupon)
const req3 = TestHelper.createRequest(`/api/administrator/subscriptions/set-customer-coupon?customerid=${user2.customer.customerid}`)
req3.account = administrator.account
req3.session = administrator.session
req3.body = {
couponid: coupon.couponid
}
try {
await req3.patch()
} catch (error) {
cachedResponses.invalidCustomer = error.message
}
// invalid coupon
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
const req4 = TestHelper.createRequest(`/api/administrator/subscriptions/set-customer-coupon?customerid=${user.customer.customerid}`)
req4.account = administrator.account
req4.session = administrator.session
req4.body = {
couponid: ''
}
try {
await req4.patch()
} catch (error) {
cachedResponses.missingCoupon = error.message
}
const req5 = TestHelper.createRequest(`/api/administrator/subscriptions/set-customer-coupon?customerid=${user.customer.customerid}`)
req5.account = administrator.account
req5.session = administrator.session
req5.body = {
couponid: 'fake'
}
try {
await req5.patch()
} catch (error) {
cachedResponses.invalidCoupon = error.message
}
// returns
const req8 = TestHelper.createRequest(`/api/administrator/subscriptions/set-customer-coupon?customerid=${user.customer.customerid}`)
req8.account = administrator.account
req8.session = administrator.session
req8.body = {
couponid: coupon.couponid
}
req8.filename = __filename
req8.saveResponse = true
cachedResponses.returns = await req8.patch()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missing
assert.strictEqual(errorMessage, 'invalid-customerid')
})
it('invalid querystring customerid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalid
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-customer', () => {
it('ineligible customer has coupon', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidCustomer
assert.strictEqual(errorMessage, 'invalid-customer')
})
})
describe('invalid-couponid', () => {
it('missing posted couponid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.missingCoupon
assert.strictEqual(errorMessage, 'invalid-couponid')
})
it('invalid posted couponid', async function () {
await bundledData(this.test.currentRetry())
const errorMessage = cachedResponses.invalidCoupon
assert.strictEqual(errorMessage, 'invalid-couponid')
})
})
})
describe('returns', () => {
it('object', async () => {
const customerNow = cachedResponses.returns
assert.strictEqual(customerNow.stripeObject.discount.coupon.object, 'coupon')
})
})
})