Skip to content

Commit 1779a6f

Browse files
committed
add stripe
1 parent bf7e762 commit 1779a6f

5 files changed

Lines changed: 107 additions & 12 deletions

File tree

‎apps/codingcatdev/src/lib/client/firebase.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { toastStore } from '@codingcatdev/blackcatui';
2-
3-
42
import { initializeApp, getApps, FirebaseError } from 'firebase/app';
53
import { getAuth, setPersistence, browserSessionPersistence, signInWithEmailAndPassword, signInWithPopup, type AuthProvider, GoogleAuthProvider } from 'firebase/auth';
4+
import { getFirestore, collection, doc, addDoc, onSnapshot } from 'firebase/firestore';
65

76
import {
87
PUBLIC_FB_API_KEY,
@@ -28,11 +27,12 @@ export let app = getApps().at(0);
2827
if (!app) {
2928
app = initializeApp(firebaseConfig);
3029
}
30+
31+
/* AUTH */
3132
export const auth = getAuth(app);
3233
// As httpOnly cookies are to be used, do not persist any state client side.
3334
setPersistence(auth, browserSessionPersistence);
3435

35-
/* AUTH */
3636
const setCookie = (idToken: string) => {
3737
document.cookie = '__ccdlogin=' + idToken + ';max-age=3600';
3838
}
@@ -66,4 +66,29 @@ export const ccdSignInWithPopUp = async (provider: AuthProvider) => {
6666
}
6767
}
6868
}
69-
}
69+
}
70+
71+
/* DB */
72+
export const db = getFirestore(app);
73+
74+
export const addSubscription = async (price: string) => {
75+
const userDoc = doc(collection(db, 'stripe-customers'), auth.currentUser?.uid)
76+
const docRef = await addDoc(collection(userDoc, 'checkout_sessions'), {
77+
price,
78+
success_url: window.location.href,
79+
cancel_url: window.location.href,
80+
})
81+
onSnapshot(docRef, (snap) => {
82+
const { error, url } = snap.data() as { error: Error, url: string };
83+
if (error) {
84+
toastStore.trigger({
85+
message: error.message,
86+
background: 'variant-filled-error'
87+
})
88+
}
89+
if (url) {
90+
// We have a Stripe Checkout URL, let's redirect.
91+
window.location.assign(url);
92+
}
93+
});
94+
};

‎apps/codingcatdev/src/lib/server/firebase.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { initializeApp, getApps, cert } from 'firebase-admin/app';
22
import { getAuth } from 'firebase-admin/auth';
3+
import { getFirestore } from 'firebase-admin/firestore';
34

45
import {
56
PUBLIC_FB_PROJECT_ID,
@@ -22,6 +23,7 @@ if (!app) {
2223
}
2324

2425
export const auth = getAuth(app);
26+
export const db = getFirestore(app);
2527

2628
export const ccdCreateSessionCookie = async (idToken: string) => {
2729
// Set session expiration to 5 days.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { db } from '$lib/server/firebase';
2+
import type { PageServerLoad } from './$types';
3+
4+
export const load = (async () => {
5+
try {
6+
const snapshot = await db.collection('stripe-products').where('active', '==', true).get();
7+
const products = [];
8+
9+
for (const doc of snapshot.docs) {
10+
const priceSnapshot = await doc.ref.collection('prices').where('active', '==', true).get();
11+
12+
for (const price of priceSnapshot.docs) {
13+
products.push({
14+
id: doc.id,
15+
...doc.data(),
16+
price: price.id
17+
})
18+
}
19+
}
20+
return {
21+
products
22+
};
23+
} catch (error) {
24+
console.error(error)
25+
}
26+
}) satisfies PageServerLoad;

‎apps/codingcatdev/src/routes/(protected)/dashboard/+page.svelte‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script lang="ts">
2+
import type { PageData } from './$types';
3+
export let data: PageData;
24
import { Icon } from '@steeze-ui/svelte-icon';
35
import { RocketLaunch } from '@steeze-ui/heroicons';
46
@@ -11,6 +13,9 @@
1113
type: 'component',
1214
component: {
1315
ref: ProModal
16+
},
17+
meta: {
18+
products: data.products
1419
}
1520
};
1621
modalStore.trigger(d);

‎apps/codingcatdev/src/routes/(protected)/dashboard/ProModal.svelte‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@
66
import AjPrimary from '$lib/components/global/icons/AJPrimary.svelte';
77
88
let monthly = true;
9+
const onSelect = (setMonth: boolean) => {
10+
monthly = setMonth;
11+
};
12+
import { modalStore, ProgressCircle } from '@codingcatdev/blackcatui';
13+
import { addSubscription } from '$lib/client/firebase';
14+
import { toastStore } from '@codingcatdev/blackcatui';
915
10-
const onSelect = () => {
11-
monthly = !monthly;
16+
let redirecting = false;
17+
const onSubscribe = async (products: { role: string; price: string }[]) => {
18+
const price = products
19+
.filter((p) => (monthly ? p.role === 'monthly' : p.role === 'yearly'))
20+
.at(0)?.price;
21+
if (!price) {
22+
toastStore.trigger({
23+
message: 'Missing price data, contact Alex!',
24+
background: 'variant-filled-error'
25+
});
26+
} else {
27+
redirecting = true;
28+
addSubscription(price);
29+
}
1230
};
31+
1332
const base = 'bcu-card p-2 md:p-8 flex flex-col items-center relative w-full hover:bg-opacity-90';
1433
</script>
1534

1635
<div class="bcu-card p-2 md:p-8 flex flex-col gap-2 md:gap-8">
1736
<div class="flex flex-col md:grid md:grid-cols-2 gap-2 md:gap-8">
1837
<button
1938
class={`${base} ${monthly ? 'variant-filled-primary' : 'variant-soft-primary'}`}
20-
on:click={onSelect}
39+
on:click={() => onSelect(true)}
2140
>
2241
{#if monthly}
2342
<div class="absolute top-0 right-0 mr-2 mt-2 ">
@@ -38,7 +57,7 @@
3857
</button>
3958
<button
4059
class={`${base} ${!monthly ? 'variant-filled-primary' : 'variant-soft-primary'}`}
41-
on:click={onSelect}
60+
on:click={() => onSelect(false)}
4261
>
4362
{#if !monthly}
4463
<div class="absolute top-0 right-0 mr-2 mt-2 ">
@@ -82,8 +101,26 @@
82101
</div>
83102
</div>
84103

85-
<button class="bcu-button variant-filled-surface flex self-end gap-2">
86-
Continue
87-
<Icon src={ArrowRightCircle} theme="solid" class="w-8" />
88-
</button>
104+
{#if redirecting}
105+
<button
106+
class="bcu-button variant-filled-surface flex self-end gap-2"
107+
on:click|once={() => onSubscribe($modalStore[0].meta?.products)}
108+
>
109+
Redirecting
110+
<ProgressCircle
111+
stroke={100}
112+
meter="stroke-primary-50"
113+
track="stroke-primary-500/30"
114+
class="w-8"
115+
/>
116+
</button>
117+
{:else}
118+
<button
119+
class="bcu-button variant-filled-surface flex self-end gap-2"
120+
on:click|once={() => onSubscribe($modalStore[0].meta?.products)}
121+
>
122+
Continue
123+
<Icon src={ArrowRightCircle} theme="solid" class="w-8" />
124+
</button>
125+
{/if}
89126
</div>

0 commit comments

Comments
 (0)