Skip to content

Commit

Permalink
Add option to disable billing
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed Jan 27, 2025
1 parent d63ab1b commit ce99dc9
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default defineNuxtConfig({
APP_LIMIT_APPS_TESTER_GROUPS: 100,
APP_LIMIT_UPLOAD_SIZE: 83886080, // 80 mb
APP_LIMIT_ARTIFACT: 5,
APP_DISABLE_BILLING: false,
LOCAL_AUTHS: '',
JWT_KEY: '',
DB_URL: '',
Expand Down
3 changes: 3 additions & 0 deletions pages/account-settings/billing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<ProgressSpinner style="width: 30px; height: 30px; margin: unset;" strokeWidth="6" class="self-center"
data-testid="menu-loading" />
</div>
<div v-else-if="data?.doNotShow">
Billing disabled
</div>
<div v-else class="flex max-w-full flex-col lg:max-w-5xl mx-auto my-10 gap-5">
<div class="flex-1 flex flex-col lg:flex-row gap-5 items-start">
<AppCard class="w-full">
Expand Down
40 changes: 21 additions & 19 deletions server/api/auth/sign-in-google.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ export default defineEventHandler(async event => {
const name = payload['name'] as string
const email = payload['email'] as string
const { token: userToken, appUserId, pUserId } = await generateUserToken(event, 'google', payload.sub!, email, name)
const userSubLms = await getUserSubscriptionLms(email)
if (userSubLms) {
const subAttrs = userSubLms.attributes
await syncUserSubscription(event, appUserId, pUserId, userSubLms.id, {
card_brand: subAttrs.card_brand || undefined,
created_at: subAttrs.created_at,
customer_id: subAttrs.customer_id,
ends_at: subAttrs.ends_at,
product_id: subAttrs.product_id,
product_name: subAttrs.product_name,
renews_at: subAttrs.renews_at,
status: subAttrs.status,
status_formatted: subAttrs.status_formatted,
updated_at: subAttrs.updated_at,
user_email: subAttrs.user_email,
user_name: subAttrs.user_name,
variant_id: subAttrs.variant_id,
variant_name: subAttrs.variant_name,
})
if (isBillingEnabled(event)) {
const userSubLms = await getUserSubscriptionLms(email)
if (userSubLms) {
const subAttrs = userSubLms.attributes
await syncUserSubscription(event, appUserId, pUserId, userSubLms.id, {
card_brand: subAttrs.card_brand || undefined,
created_at: subAttrs.created_at,
customer_id: subAttrs.customer_id,
ends_at: subAttrs.ends_at,
product_id: subAttrs.product_id,
product_name: subAttrs.product_name,
renews_at: subAttrs.renews_at,
status: subAttrs.status,
status_formatted: subAttrs.status_formatted,
updated_at: subAttrs.updated_at,
user_email: subAttrs.user_email,
user_name: subAttrs.user_name,
variant_id: subAttrs.variant_id,
variant_name: subAttrs.variant_name,
})
}
}
signInUser(event, userToken)
const param = new URLSearchParams({
Expand Down
6 changes: 6 additions & 0 deletions server/api/billing/checkout.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export type CustomerMetadata = {
}

export default defineEventHandler(async event => {
if (!isBillingEnabled(event)) {
throw createError({
message: 'Billing disabled',
})
}

const { checkoutOrigin } = getQuery(event)
const db = event.context.drizzle
const activeSub = await getUserSubFromDb(event)
Expand Down
14 changes: 14 additions & 0 deletions server/api/billing/current-billing.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { defineEventHandler } from 'h3'
import { syncUserSubscription } from './subscription-sync'

export default defineEventHandler(async event => {
if (!isBillingEnabled(event)) {
return {
plan: '',
planPrice: 0,
status: '',
statusFormatted: '',
endsAt: '',
renewsAt: '',
isCancelled: false,
doNotShow: true,
}
}

const userSubLms = await getUserSubscriptionLms(event.context.auth.email!)
if (userSubLms) {
const subAttrs = userSubLms.attributes
Expand Down Expand Up @@ -41,5 +54,6 @@ export default defineEventHandler(async event => {
endsAt: userSub?.endsAt,
renewsAt: userSub?.renewsAt,
isCancelled: isCancelled,
doNotShow: false,
}
})
6 changes: 6 additions & 0 deletions server/api/billing/toggle-subs.get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { updateSubscription } from "@lemonsqueezy/lemonsqueezy.js"

export default defineEventHandler(async event => {
if (!isBillingEnabled(event)) {
throw createError({
message: 'Billing disabled',
})
}

const { isCancel: isCancelStr } = getQuery(event)
const isCancel = isCancelStr === 'true'
const subs = await getUserSubFromDb(event)
Expand Down
6 changes: 6 additions & 0 deletions server/api/billing/update-payment.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default defineEventHandler(async event => {
if (!isBillingEnabled(event)) {
throw createError({
message: 'Billing disabled',
})
}

const subscription = await getUserSubFromDb(event)
const { updatePaymentMethod } = await getUrlsSubscription(subscription!.subscriptionId)
return updatePaymentMethod
Expand Down
5 changes: 5 additions & 0 deletions server/utils/user-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ export function checkIsExpire(gracePeriodHour: number, endsAt: Date) {
const now = new Date()
now.setHours(now.getHours() - gracePeriodHour)
return (endsAt.getTime() - now.getTime()) <= 0
}

export function isBillingEnabled(event: H3Event<EventHandlerRequest>) {
const { APP_DISABLE_BILLING } = useRuntimeConfig(event)
return !APP_DISABLE_BILLING
}

0 comments on commit ce99dc9

Please sign in to comment.