Skip to content

Commit

Permalink
Use Price objects to show pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
danrowden committed Sep 26, 2023
1 parent 53f9033 commit 6c6e87e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
10 changes: 7 additions & 3 deletions app/(app)/billing/refresh-plans/page.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import prisma from "@/lib/prisma";
import LemonSqueezy from '@lemonsqueezy/lemonsqueezy.js';


const ls = new LemonSqueezy(process.env.LEMONSQUEEZY_API_KEY);


Expand Down Expand Up @@ -64,6 +63,11 @@ async function getPlans() {
product = variant['product']
productId = parseInt(variant['attributes']['product_id'])

// Get variant's Price objects
let prices = await ls.getPrices({ variantId: variantId, perPage: 100 })
// The first object is the latest/current price
let variant_price = prices['data'][0]['attributes']['unit_price']

variant = variant['attributes']

try {
Expand All @@ -79,7 +83,7 @@ async function getPlans() {
status: variant['status'],
sort: variant['sort'],
description: variant['description'],
price: variant['price'],
price: variant_price, // display price in the app matches current Price object in LS
interval: variant['interval'],
intervalCount: variant['interval_count'],
},
Expand All @@ -91,7 +95,7 @@ async function getPlans() {
status: variant['status'],
sort: variant['sort'],
description: variant['description'],
price: variant['price'],
price: variant_price, // display price in the app matches current Price object in LS
interval: variant['interval'],
intervalCount: variant['interval_count'],
}
Expand Down
10 changes: 9 additions & 1 deletion app/(app)/billing/webhook/route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import prisma from "@/lib/prisma";
import LemonSqueezy from '@lemonsqueezy/lemonsqueezy.js';

const ls = new LemonSqueezy(process.env.LEMONSQUEEZY_API_KEY);


async function processEvent(event) {
Expand Down Expand Up @@ -42,6 +45,11 @@ async function processEvent(event) {

const lemonSqueezyId = parseInt(obj['id'])

// Get subscription's Price object
// We save the Price value to the subscription so we can display it to the user
let resp = await ls.getPrice({ id: data['first_subscription_item']['price_id'] })
let subItemPrice = resp['data']['attributes']['unit_price']

const updateData = {
orderId: data['order_id'],
name: data['user_name'],
Expand All @@ -52,7 +60,7 @@ async function processEvent(event) {
trialEndsAt: data['trial_ends_at'],
planId: plan['id'],
userId: customData['user_id'],
price: event.eventName == 'subscription_created' ? plan['price'] : null
price: subItemPrice
}

const createData = updateData
Expand Down
7 changes: 6 additions & 1 deletion app/api/subscriptions/[id]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export async function POST(request, { params }) {
// Return values needed to refresh state in UI
// DB will be updated in the background with webhooks

// Get price
let resp = await ls.getPrice({ id: subscription['data']['attributes']['first_subscription_item']['price_id'] })
let subItemPrice = resp['data']['attributes']['unit_price']

// Filtered object
const sub = {
product_id: subscription['data']['attributes']['product_id'],
Expand All @@ -105,7 +109,8 @@ export async function POST(request, { params }) {
renews_at: subscription['data']['attributes']['renews_at'],
ends_at: subscription['data']['attributes']['ends_at'],
resumes_at: subscription['data']['attributes']['resumes_at'],
plan: {}
plan: {},
price: subItemPrice,
}

// Get new plan's data
Expand Down
1 change: 1 addition & 0 deletions components/plan-button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ For upgrades you will be charged a prorated amount.`)) {
planInterval: result['subscription']['plan']['interval'],
status: result['subscription']['status'],
renewalDate: result['subscription']['renews_at'],
price: result['subscription']['price']
})

toast.success('Your subscription plan has changed!')
Expand Down
11 changes: 6 additions & 5 deletions components/subscription-customer-portal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const PortalSubscriptionComponent = ({ sub, plans }) => {
trialEndDate: sub.trialEndsAt,
expiryDate: sub.endsAt,
unpauseDate: sub.resumesAt,
price: sub.price / 100,
}
} else {
return {}
Expand Down Expand Up @@ -99,7 +100,7 @@ const ActiveSubscription = ({ subscription }) => {
return (
<>
<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-8">Your next renewal will be on {formatDate(subscription.renewalDate)}.</p>
Expand All @@ -114,7 +115,7 @@ const CancelledSubscription = ({ subscription }) => {
return (
<>
<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-8">Your subscription has been cancelled and <b>will end on {formatDate(subscription.expiryDate)}</b>. After this date you will no longer have access to the app.</p>
Expand All @@ -129,7 +130,7 @@ const PausedSubscription = ({ subscription }) => {
return (
<>
<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

{subscription.unpauseDate ? (
Expand All @@ -148,7 +149,7 @@ const TrialSubscription = ({ subscription }) => {
return (
<>
<p className="mb-2">
You are currently on a free trial of the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on a free trial of the <b>{subscription.planName} {subscription.planInterval}ly</b> plan (${subscription.price}/{subscription.planInterval}).
</p>

<p className="mb-8">Your trial ends on {formatDate(subscription.trialEndDate)}. You can cancel your subscription before this date and you won&apos;t be charged.</p>
Expand All @@ -168,7 +169,7 @@ const PastDueSubscription = ({ subscription }) => {
</div>

<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-8">We will attempt a payment on {formatDate(subscription.renewalDate)}.</p>
Expand Down
11 changes: 6 additions & 5 deletions components/subscription.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const SubscriptionComponent = ({ sub, plans }) => {
trialEndDate: sub.trialEndsAt,
expiryDate: sub.endsAt,
unpauseDate: sub.resumesAt,
price: sub.price / 100,
}
} else {
return {}
Expand Down Expand Up @@ -77,7 +78,7 @@ const ActiveSubscription = ({ subscription, setSubscription }) => {
return (
<>
<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-2">Your next renewal will be on {formatDate(subscription.renewalDate)}.</p>
Expand All @@ -104,7 +105,7 @@ const CancelledSubscription = ({ subscription, setSubscription }) => {
return (
<>
<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-8">Your subscription has been cancelled and <b>will end on {formatDate(subscription.expiryDate)}</b>. After this date you will no longer have access to the app.</p>
Expand All @@ -119,7 +120,7 @@ const PausedSubscription = ({ subscription, setSubscription }) => {
return (
<>
<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

{subscription.unpauseDate ? (
Expand All @@ -138,7 +139,7 @@ const TrialSubscription = ({ subscription, setSubscription }) => {
return (
<>
<p className="mb-2">
You are currently on a free trial of the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on a free trial of the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-6">Your trial ends on {formatDate(subscription.trialEndDate)}. You can cancel your subscription before this date and you won&apos;t be charged.</p>
Expand Down Expand Up @@ -168,7 +169,7 @@ const PastDueSubscription = ({ subscription, setSubscription }) => {
</div>

<p className="mb-2">
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan.
You are currently on the <b>{subscription.planName} {subscription.planInterval}ly</b> plan, paying ${subscription.price}/{subscription.planInterval}.
</p>

<p className="mb-2">We will attempt a payment on {formatDate(subscription.renewalDate)}.</p>
Expand Down

0 comments on commit 6c6e87e

Please sign in to comment.