feat: dashboard billing (#193)
* feat: move plans into sharable json * remove unused code * update pnpm lock * update plans.json * implement early adopter pricing * docs: update meta title and description * Finished the UI for upgrading plans * Updated the pricing plans * Updated the cards ui * use relative import * improve styling of upgrade plan card * styling improvements, lemonsqueezy setup * trigger lemonsqueezy payment and add webhook * update pnpm lock * add logout route * feat: complete billing functionality * generate lock file * make crypto false on browser * feat: adding upgrade prompts to org * chore: add upgrade icon to upload video * feat: add upgrade prompt to certificate and settings * feat: implement onboarding setup page (#229) * working on the onboarding setup * working on the onboarding page * Almost done with the onboarding UI * finished the UI of the onBoarding setup * finished the UI of the onBoarding setup * Implementing the functions to carryout the logic of updating the UI onmount if a user has carried out a certain step or not * Implementing the functions to carryout the logic of updating the UI onmount if a user has carried out a certain step or not * making some chnages * finished the onBoarding task * updated some logic * Removed the uneeded svg from the static folder * fixed isCoursePublished not returning a boolean * improved the setup link protection * chore: make menuItems reactive * improve formatting * format supabase queries --------- Co-authored-by: Rotimi Best <best@classroomio.com> * revamp setup * improve the upgrade experience * migration * feat: add lemonsqueezy customer portal --------- Co-authored-by: emmanuel <chifez1@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Configuration, OpenAIApi } from 'openai-edge';
|
||||
import { OpenAIStream, StreamingTextResponse } from 'ai';
|
||||
import { OPENAI_API_KEY } from '$env/static/private';
|
||||
import { OpenAIStream, StreamingTextResponse } from 'ai';
|
||||
import { Configuration, OpenAIApi } from 'openai-edge';
|
||||
|
||||
// Create an OpenAI API client (that's edge friendly!)
|
||||
const openAIConfig = new Configuration({
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { createCheckout } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { LEMON_SQUEEZY_STORE_ID } from '$env/static/private';
|
||||
import { configureLemonSqueezy } from '$lib/utils/services/lemonsqueezy';
|
||||
|
||||
export async function POST({ request }) {
|
||||
const { email, name, orgId, productId, triggeredBy } = await request.json();
|
||||
|
||||
await configureLemonSqueezy();
|
||||
|
||||
if (!productId) {
|
||||
return json({ success: false, message: 'Product Id is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const checkoutData = {
|
||||
email: email,
|
||||
name: name,
|
||||
custom: {
|
||||
org_id: orgId,
|
||||
triggered_by: `${triggeredBy}`
|
||||
}
|
||||
};
|
||||
|
||||
console.log({ checkoutData });
|
||||
|
||||
const { data } = await createCheckout(LEMON_SQUEEZY_STORE_ID, productId, {
|
||||
checkoutData
|
||||
});
|
||||
|
||||
const checkoutURL = data?.data.attributes.url;
|
||||
|
||||
console.log({ checkoutURL });
|
||||
|
||||
return json({
|
||||
success: true,
|
||||
checkoutURL
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getSubscription } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { configureLemonSqueezy } from '$lib/utils/services/lemonsqueezy';
|
||||
|
||||
export async function GET({ url }) {
|
||||
const subscriptionId = url.searchParams.get('subscriptionId');
|
||||
|
||||
await configureLemonSqueezy();
|
||||
|
||||
if (!subscriptionId) {
|
||||
return json({ success: false, message: 'Subscription id is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const response = await getSubscription(subscriptionId);
|
||||
const customerPortal = response?.data?.data?.attributes?.urls?.customer_portal;
|
||||
|
||||
console.log({ customerPortal });
|
||||
|
||||
return json({
|
||||
success: true,
|
||||
customerPortal
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import crypto from 'crypto';
|
||||
import { LEMON_SQUEEZY_WEBHOOK_SECRET } from '$env/static/private';
|
||||
import { createOrgPlan, cancelOrgPlan } from '$lib/utils/services/org';
|
||||
import { PLAN } from 'shared-constants/src/plans/constants';
|
||||
|
||||
export async function POST({ request }) {
|
||||
try {
|
||||
// Catch the event type
|
||||
const clonedReq = request.clone();
|
||||
const eventType = request.headers.get('X-Event-Name');
|
||||
const body = await request.json();
|
||||
|
||||
// Check signature
|
||||
const secret = LEMON_SQUEEZY_WEBHOOK_SECRET;
|
||||
const hmac = crypto.createHmac('sha256', secret);
|
||||
const digest = Buffer.from(hmac.update(await clonedReq.text()).digest('hex'), 'utf8');
|
||||
const signature = Buffer.from(request.headers.get('X-Signature') || '', 'utf8');
|
||||
|
||||
if (!crypto.timingSafeEqual(digest, signature)) {
|
||||
throw new Error('Invalid signature.');
|
||||
}
|
||||
|
||||
console.log(body);
|
||||
|
||||
// Logic according to event
|
||||
if (eventType === 'subscription_created' || eventType === 'subscription_resumed') {
|
||||
const orgId = body.meta.custom_data.org_id;
|
||||
const triggeredBy = body.meta.custom_data.triggered_by;
|
||||
const isSuccessful = body.data.attributes.status === 'active';
|
||||
|
||||
console.log({
|
||||
orgId,
|
||||
triggeredBy,
|
||||
isSuccessful
|
||||
});
|
||||
|
||||
if (isSuccessful && orgId) {
|
||||
const { data, error } = await createOrgPlan({
|
||||
orgId,
|
||||
triggeredBy: parseInt(triggeredBy),
|
||||
planName: PLAN.EARLY_ADOPTER,
|
||||
data: body.data
|
||||
});
|
||||
|
||||
if (error) console.error('Error creating org plan', error);
|
||||
console.log('Create plan', data);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
['subscription_cancelled', 'subscription_expired', 'subscription_payment_refunded'].includes(
|
||||
`${eventType}`
|
||||
)
|
||||
) {
|
||||
const orgId = body.meta.custom_data.org_id;
|
||||
const { data, error } = await cancelOrgPlan({
|
||||
orgId,
|
||||
planName: PLAN.EARLY_ADOPTER
|
||||
});
|
||||
|
||||
if (error) console.error('Error canceling org plan', error);
|
||||
console.log('Cancel plan', data);
|
||||
}
|
||||
|
||||
return json({ message: 'Webhook received' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return json({ message: 'Server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export function GET() {
|
||||
return json({
|
||||
success: true
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
|
||||
export async function POST({ request }) {
|
||||
try {
|
||||
// Catch the event type
|
||||
// const clonedReq = request.clone();
|
||||
// const eventType = request.headers.get('X-Event-Name');
|
||||
const body = await request.json();
|
||||
|
||||
// // Check signature
|
||||
// const secret = LEMON_SQUEEZY_WEBHOOK_SECRET;
|
||||
// const hmac = crypto.createHmac('sha256', secret);
|
||||
// const digest = Buffer.from(hmac.update(await clonedReq.text()).digest('hex'), 'utf8');
|
||||
// const signature = Buffer.from(request.headers.get('X-Signature') || '', 'utf8');
|
||||
|
||||
// if (!crypto.timingSafeEqual(digest, signature)) {
|
||||
// throw new Error('Invalid signature.');
|
||||
// }
|
||||
|
||||
console.log(body);
|
||||
|
||||
// Logic according to event
|
||||
// if (eventType === 'order_created') {
|
||||
// const orgId = body.meta.custom_data.org_id;
|
||||
// const triggeredBy = body.meta.custom_data.triggered_by;
|
||||
// const isSuccessful = body.data.attributes.status === 'paid';
|
||||
|
||||
// console.log({
|
||||
// orgId,
|
||||
// triggeredBy,
|
||||
// isSuccessful
|
||||
// });
|
||||
|
||||
// if (isSuccessful) {
|
||||
// const { error } = await createOrgPlan({
|
||||
// orgId,
|
||||
// triggeredBy,
|
||||
// planName: PLAN.EARLY_ADOPTER,
|
||||
// data: body.data
|
||||
// });
|
||||
|
||||
// if (error) console.error('Error creating org plan', error);
|
||||
// }
|
||||
// }
|
||||
|
||||
return json({ message: 'Webhook received' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return json({ message: 'Server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export function GET() {
|
||||
return json({
|
||||
success: true
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user