-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added working billing with stripe
Took 5 hours 10 minutes
- Loading branch information
Showing
18 changed files
with
408 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ jobs: | |
port: ${{ secrets.SSH_SERVER_PORT }} | ||
script: | | ||
cd ${{ secrets.SSH_SERVER_BACKEND_PATH }} | ||
echo -e 'OAUTH_CLIENT_ID="${{ secrets.OAUTH_CLIENT_ID }}"\nOAUTH_CLIENT_SECRET="${{ secrets.OAUTH_CLIENT_SECRET }}"\nOAUTH_REDIRECT_URI="${{ secrets.OAUTH_REDIRECT_URI }}"\nDB_URL="${{ secrets.DB_URL }}"' > .env | ||
echo -e 'OAUTH_CLIENT_ID="${{ secrets.OAUTH_CLIENT_ID }}"\nOAUTH_CLIENT_SECRET="${{ secrets.OAUTH_CLIENT_SECRET }}"\nOAUTH_REDIRECT_URI="${{ secrets.OAUTH_REDIRECT_URI }}"\nDB_URL="${{ secrets.DB_URL }}"\nSTRIPE_PUBLISHABLE_KEY="${{ secrets.STRIPE_PUBLISHABLE_KEY }}"\nSTRIPE_SECRET_KEY="${{ secrets.STRIPE_SECRET_KEY }}"\nSTRIPE_SUCCESS_KEY="${{ secrets.STRIPE_SUCCESS_KEY }}"' > .env | ||
- name: Run backend | ||
uses: appleboy/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { ElysiaApp } from '@/app' | ||
import { db } from '@/utils/db' | ||
import { eq } from 'drizzle-orm' | ||
import { sessions, users } from '@/db/schema' | ||
import { validateRequest } from '@/utils/validateRequest' | ||
import Stripe from 'stripe' | ||
import dayjs from 'dayjs' | ||
import utc from 'dayjs/plugin/utc' | ||
import { t } from 'elysia' | ||
|
||
dayjs.extend(utc) | ||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string) | ||
|
||
export default (app: ElysiaApp) => | ||
app | ||
.get('/config', async ({ set, cookie: { auth_session } }) => { | ||
const userSession = await db.query.sessions.findFirst({ where: eq(sessions.id, auth_session.value) }) | ||
const { user, session } = await validateRequest(auth_session) | ||
|
||
if (user && session && userSession) { | ||
const userData = await db.query.users.findFirst({ where: eq(users.id, userSession.userId) }) | ||
|
||
if (userData?.accessToken) { | ||
set.status = 200 | ||
return { | ||
publishableKey: process.env.STRIPE_PUBLISHABLE_KEY as string, | ||
} | ||
} | ||
|
||
if (!userData) { | ||
set.status = 401 | ||
return { message: 'unauthorized' } | ||
} | ||
} else { | ||
set.status = 401 | ||
return { message: 'unauthorized' } | ||
} | ||
}) | ||
.post('/', async ({ set, cookie: { auth_session } }) => { | ||
const userSession = await db.query.sessions.findFirst({ where: eq(sessions.id, auth_session.value) }) | ||
const { user, session } = await validateRequest(auth_session) | ||
|
||
if (user && session && userSession) { | ||
const userData = await db.query.users.findFirst({ where: eq(users.id, userSession.userId) }) | ||
|
||
if (userData?.accessToken) { | ||
try { | ||
const paymentIntent: Stripe.PaymentIntent = await stripe.paymentIntents.create({ | ||
currency: 'usd', | ||
amount: 199, | ||
}) | ||
set.status = 200 | ||
return { clientSecret: paymentIntent.client_secret } | ||
} catch (error) { | ||
set.status = 403 | ||
return error | ||
} | ||
} | ||
|
||
if (!userData) { | ||
set.status = 401 | ||
return { message: 'unauthorized' } | ||
} | ||
} else { | ||
set.status = 401 | ||
return { message: 'unauthorized' } | ||
} | ||
}) | ||
.get('/success', async ({ set, query: { key }, cookie: { auth_session } }) => { | ||
const userSession = await db.query.sessions.findFirst({ where: eq(sessions.id, auth_session.value) }) | ||
const { user, session } = await validateRequest(auth_session) | ||
|
||
console.log(123, key) | ||
|
||
if (user && session && userSession) { | ||
const userData = await db.query.users.findFirst({ where: eq(users.id, userSession.userId) }) | ||
|
||
if (userData?.accessToken) { | ||
if (key !== process.env.STRIPE_SUCCESS_KEY) { | ||
set.status = 403 | ||
return { message: 'forbidden' } | ||
} | ||
|
||
const subscriptionExpirationDate = dayjs() | ||
.utc() | ||
.add(1, 'year') | ||
.set('hours', 0) | ||
.set('minutes', 0) | ||
.set('seconds', 0) | ||
.set('milliseconds', 0) | ||
.toISOString() | ||
|
||
console.log(123, userData.accessToken, subscriptionExpirationDate) | ||
|
||
const updatedUser = await db | ||
.update(users) | ||
.set({ subscriptionExpirationDate }) | ||
.where(eq(users.accessToken, userData.accessToken)) | ||
.returning({ id: users.id }) | ||
|
||
console.log([updatedUser]) | ||
|
||
if (updatedUser.length === 0) { | ||
set.status = 403 | ||
return { message: 'forbidden' } | ||
} else { | ||
set.status = 301 | ||
set.redirect = '/app/billing/complete' | ||
} | ||
} | ||
} else { | ||
set.status = 401 | ||
return { message: 'unauthorized' } | ||
} | ||
}, { | ||
query: t.Object({ | ||
key: t.String(), | ||
}), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { SEO } from '@/components/seo.tsx' | ||
import { Overlay } from '@/components/overlay/overlay.tsx' | ||
import { AnimateWrapper } from '@/components/animateWrapper/animateWrapper.tsx' | ||
import { Container } from '@/components/container/container.tsx' | ||
import styles from '@/styles/notFound.module.scss' | ||
import { Link } from 'react-router-dom' | ||
|
||
const metaData = { | ||
title: 'Thank you', | ||
path: '/app/billing/complete', | ||
} | ||
|
||
export default function BillingCancel() { | ||
return ( | ||
<> | ||
<SEO title={metaData.title} path={metaData.path} /> | ||
|
||
<Overlay> | ||
<AnimateWrapper> | ||
<Container> | ||
<div className={styles.notFoundMainContainer}> | ||
<h2>Your transaction has been cancelled</h2> | ||
<i>Please try again</i> | ||
<Link to={'/app/billing'}> | ||
Go to billing | ||
</Link> | ||
</div> | ||
</Container> | ||
</AnimateWrapper> | ||
</Overlay> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { SEO } from '@/components/seo.tsx' | ||
import { Overlay } from '@/components/overlay/overlay.tsx' | ||
import { AnimateWrapper } from '@/components/animateWrapper/animateWrapper.tsx' | ||
import { Container } from '@/components/container/container.tsx' | ||
import styles from '@/styles/notFound.module.scss' | ||
import { Link } from 'react-router-dom' | ||
|
||
const metaData = { | ||
title: 'Thank you', | ||
path: '/app/billing/complete', | ||
} | ||
|
||
export default function ThankYouPage() { | ||
return ( | ||
<> | ||
<SEO title={metaData.title} path={metaData.path} /> | ||
|
||
<Overlay> | ||
<AnimateWrapper> | ||
<Container> | ||
<div className={styles.notFoundMainContainer}> | ||
<h2>Thank you for your purchase</h2> | ||
<i>Your transaction has been successfully completed</i> | ||
<Link to={'/app/calendar'}> | ||
Go to your calendar | ||
</Link> | ||
</div> | ||
</Container> | ||
</AnimateWrapper> | ||
</Overlay> | ||
</> | ||
); | ||
} |
Oops, something went wrong.