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:
Rotimi Best
2024-04-07 06:08:34 +01:00
committed by GitHub
co-authored by emmanuel
parent 136e057954
commit fcd0bcc656
105 changed files with 1743 additions and 463 deletions
@@ -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
});
}