Skip to content

Commit 01c9537

Browse files
authored
Website: Add support for promotional codes on the self-service license dispenser (#26280)
Changes: - Added the ability to use coupons on the Fleet premium license dispenser - Updated the stripe webhook to support coupons on Fleet premium subscriptions - Updated the customer dashboard on the website to show decimal places in subscription/host prices.
1 parent 606df3f commit 01c9537

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

website/api/controllers/customers/get-stripe-checkout-session-url.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = {
5454
],
5555
mode: 'subscription',
5656
billing_address_collection: 'required',// eslint-disable-line camelcase
57+
allow_promotion_codes: true,// eslint-disable-line camelcase
5758
tax_id_collection: {// eslint-disable-line camelcase
5859
enabled: true,
5960
required: 'if_supported'

website/api/controllers/webhooks/receive-from-stripe.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = {
103103
// If stripe thinks this subscription renews in 7 days, we'll send the user an subscription reminder email.
104104
if(type === 'invoice.upcoming' && stripeEventData.billing_reason === 'upcoming') {
105105
// Get the subscription cost per host for the Subscription renewal notification email.
106-
let subscriptionCostPerHost = Math.floor(subscriptionForThisEvent.subscriptionPrice / subscriptionForThisEvent.numberOfHosts / 12);
106+
let subscriptionCostPerHost = (subscriptionForThisEvent.subscriptionPrice / subscriptionForThisEvent.numberOfHosts / 12).toFixed(2);
107107
let upcomingBillingAt = stripeEventData.next_payment_attempt * 1000;
108108
// Send a upcoming subscription renewal email.
109109
await sails.helpers.sendTemplateEmail.with({
@@ -188,6 +188,21 @@ module.exports = {
188188
// Get the updated number of hosts from the quantity of the invoice.
189189
let newNumberOfHosts = updatedSubscriptionInfo.quantity;
190190

191+
let subscriptionPrice = Math.floor(pricePerHost * newNumberOfHosts);
192+
193+
// (Optionally) adjust the price of this subscription if a coupon was applied.
194+
if(stripeEventData.discount){
195+
if(stripeEventData.discount.coupon){
196+
if(stripeEventData.discount.coupon.amount_off){
197+
// If the coupon applied takes a fixed dollar amount off of the total price, subtact the amoutn fro mthe subscriptionPrice
198+
subscriptionPrice = _.round(subscriptionPrice - (stripeEventData.discount.coupon.amount_off / 100), 2); // Note: coupon.amount_off contains the discounted amount in cents.
199+
} else if(stripeEventData.discount.coupon.percent_off){
200+
// Otherwise if it is a percent discount,
201+
let discountAmount = subscriptionPrice * (stripeEventData.discount.coupon.percent_off / 100);
202+
subscriptionPrice = _.round(subscriptionPrice - discountAmount, 2);
203+
}
204+
}
205+
}
191206
// Generate a new license key for this subscription
192207
let newLicenseKeyForThisSubscription = await sails.helpers.createLicenseKey.with({
193208
numberOfHosts: newNumberOfHosts,
@@ -198,7 +213,7 @@ module.exports = {
198213
// Update the subscription record
199214
await Subscription.updateOne({id: subscriptionForThisEvent.id}).set({
200215
numberOfHosts: newNumberOfHosts,
201-
subscriptionPrice: Math.floor(pricePerHost * newNumberOfHosts),
216+
subscriptionPrice,
202217
fleetLicenseKey: newLicenseKeyForThisSubscription,
203218
nextBillingAt: nextBillingAt
204219
});
@@ -214,10 +229,11 @@ module.exports = {
214229
let nextBillingAt = newSubscriptionDetails.current_period_end * 1000;
215230
// Get the number of Hosts.
216231
let numberOfHosts = newSubscriptionDetails.quantity;
217-
// Get the whole dollar price per host.
232+
// Get the whole dollar price per host by subtracting the discount amount from the plan amount.
233+
// [?]: https://docs.stripe.com/api/checkout/sessions/object#checkout_session_object-total_details
218234
let subscriptionPricePerHost = newSubscriptionDetails.plan.amount / 100;
219235
// Determine the annual cost of this user's subscription
220-
let subscriptionPrice = subscriptionPricePerHost * numberOfHosts;
236+
let subscriptionPrice = (subscriptionPricePerHost * numberOfHosts) - (stripeEventData.total_details.amount_discount / 100);
221237
// Generate a new license key.
222238
let newLicenseKey = await sails.helpers.createLicenseKey.with({
223239
numberOfHosts,

website/views/pages/customers/dashboard.ejs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</div>
3232
<div class="col-12 col-md-6 col-lg-2 pt-3 pt-md-0">
3333
<strong>Cost</strong>
34-
<p>${{thisSubscription.subscriptionPrice}}.00/year</p>
34+
<p>${{thisSubscription.subscriptionPrice.toFixed(2)}}/year</p>
3535
</div>
3636
<div class="col-12 col-md-6 col-lg-2 pt-3 pt-lg-0">
3737
<strong>No. of devices</strong>
@@ -89,8 +89,8 @@
8989
<div class="row pb-3 mx-0">
9090
<div style="max-width: 16px;" class="col-1 px-0"><img style="margin-top: 5px; height: 16px; width: 16px;" src="/images/[email protected]" alt="A calendar icon"></div>
9191
<div class="col pl-3">
92-
<p>{{thisSubscription.numberOfHosts}} devices @ ${{thisSubscription.subscriptionPrice / thisSubscription.numberOfHosts / 12}}.00/device/month</p>
93-
<p>Billed annually at ${{thisSubscription.subscriptionPrice}}.00/yr</p>
92+
<p>{{thisSubscription.numberOfHosts}} devices @ ${{(thisSubscription.subscriptionPrice / thisSubscription.numberOfHosts / 12).toFixed(2)}}/device/month</p>
93+
<p>Billed annually at ${{thisSubscription.subscriptionPrice.toFixed(2)}}/yr</p>
9494
<p v-if="willSubscriptionRenew">Next payment on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
9595
<p v-else>Your subscription will expire on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
9696
</div>

0 commit comments

Comments
 (0)