Skip to content

Website: Add support for promotional codes on the self-service license dispenser #26280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = {
],
mode: 'subscription',
billing_address_collection: 'required',// eslint-disable-line camelcase
allow_promotion_codes: true,// eslint-disable-line camelcase
tax_id_collection: {// eslint-disable-line camelcase
enabled: true,
required: 'if_supported'
Expand Down
24 changes: 20 additions & 4 deletions website/api/controllers/webhooks/receive-from-stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = {
// If stripe thinks this subscription renews in 7 days, we'll send the user an subscription reminder email.
if(type === 'invoice.upcoming' && stripeEventData.billing_reason === 'upcoming') {
// Get the subscription cost per host for the Subscription renewal notification email.
let subscriptionCostPerHost = Math.floor(subscriptionForThisEvent.subscriptionPrice / subscriptionForThisEvent.numberOfHosts / 12);
let subscriptionCostPerHost = (subscriptionForThisEvent.subscriptionPrice / subscriptionForThisEvent.numberOfHosts / 12).toFixed(2);
let upcomingBillingAt = stripeEventData.next_payment_attempt * 1000;
// Send a upcoming subscription renewal email.
await sails.helpers.sendTemplateEmail.with({
Expand Down Expand Up @@ -188,6 +188,21 @@ module.exports = {
// Get the updated number of hosts from the quantity of the invoice.
let newNumberOfHosts = updatedSubscriptionInfo.quantity;

let subscriptionPrice = Math.floor(pricePerHost * newNumberOfHosts);

// (Optionally) adjust the price of this subscription if a coupon was applied.
if(stripeEventData.discount){
if(stripeEventData.discount.coupon){
if(stripeEventData.discount.coupon.amount_off){
// If the coupon applied takes a fixed dollar amount off of the total price, subtact the amoutn fro mthe subscriptionPrice
subscriptionPrice = _.round(subscriptionPrice - (stripeEventData.discount.coupon.amount_off / 100), 2); // Note: coupon.amount_off contains the discounted amount in cents.
} else if(stripeEventData.discount.coupon.percent_off){
// Otherwise if it is a percent discount,
let discountAmount = subscriptionPrice * (stripeEventData.discount.coupon.percent_off / 100);
subscriptionPrice = _.round(subscriptionPrice - discountAmount, 2);
}
}
}
// Generate a new license key for this subscription
let newLicenseKeyForThisSubscription = await sails.helpers.createLicenseKey.with({
numberOfHosts: newNumberOfHosts,
Expand All @@ -198,7 +213,7 @@ module.exports = {
// Update the subscription record
await Subscription.updateOne({id: subscriptionForThisEvent.id}).set({
numberOfHosts: newNumberOfHosts,
subscriptionPrice: Math.floor(pricePerHost * newNumberOfHosts),
subscriptionPrice,
fleetLicenseKey: newLicenseKeyForThisSubscription,
nextBillingAt: nextBillingAt
});
Expand All @@ -214,10 +229,11 @@ module.exports = {
let nextBillingAt = newSubscriptionDetails.current_period_end * 1000;
// Get the number of Hosts.
let numberOfHosts = newSubscriptionDetails.quantity;
// Get the whole dollar price per host.
// Get the whole dollar price per host by subtracting the discount amount from the plan amount.
// [?]: https://docs.stripe.com/api/checkout/sessions/object#checkout_session_object-total_details
let subscriptionPricePerHost = newSubscriptionDetails.plan.amount / 100;
// Determine the annual cost of this user's subscription
let subscriptionPrice = subscriptionPricePerHost * numberOfHosts;
let subscriptionPrice = (subscriptionPricePerHost * numberOfHosts) - (stripeEventData.total_details.amount_discount / 100);
// Generate a new license key.
let newLicenseKey = await sails.helpers.createLicenseKey.with({
numberOfHosts,
Expand Down
6 changes: 3 additions & 3 deletions website/views/pages/customers/dashboard.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</div>
<div class="col-12 col-md-6 col-lg-2 pt-3 pt-md-0">
<strong>Cost</strong>
<p>${{thisSubscription.subscriptionPrice}}.00/year</p>
<p>${{thisSubscription.subscriptionPrice.toFixed(2)}}/year</p>
</div>
<div class="col-12 col-md-6 col-lg-2 pt-3 pt-lg-0">
<strong>No. of devices</strong>
Expand Down Expand Up @@ -89,8 +89,8 @@
<div class="row pb-3 mx-0">
<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>
<div class="col pl-3">
<p>{{thisSubscription.numberOfHosts}} devices @ ${{thisSubscription.subscriptionPrice / thisSubscription.numberOfHosts / 12}}.00/device/month</p>
<p>Billed annually at ${{thisSubscription.subscriptionPrice}}.00/yr</p>
<p>{{thisSubscription.numberOfHosts}} devices @ ${{(thisSubscription.subscriptionPrice / thisSubscription.numberOfHosts / 12).toFixed(2)}}/device/month</p>
<p>Billed annually at ${{thisSubscription.subscriptionPrice.toFixed(2)}}/yr</p>
<p v-if="willSubscriptionRenew">Next payment on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
<p v-else>Your subscription will expire on <js-timestamp :at="thisSubscription.nextBillingAt" always-show-year format="billing"></js-timestamp></p>
</div>
Expand Down
Loading