Skip to content

Commit

Permalink
feat: stripe boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 16, 2023
1 parent 5ad21cf commit c89aaf0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ SUPABASE_URL=
DEEPGRAM_API_KEY=
OPENAI_API_KEY=
SLACK_POSTMAN_WEBHOOK_URL=

STRIPE_SECRET_KEY=
STRIPE_PUBLISHABLE_KEY=
4 changes: 4 additions & 0 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const env = createEnv({
PODCAST_INDEX_API_KEY: process.env.PODCAST_INDEX_API_KEY,
PODCAST_INDEX_SECRET: process.env.PODCAST_INDEX_SECRET,
SLACK_POSTMAN_WEBHOOK_URL: process.env.SLACK_POSTMAN_WEBHOOK_URL,
STRIPE_PUBLISHABLE_KEY: process.env.STRIPE_PUBLISHABLE_KEY,
STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
SUPABASE_SERVICE_ROLE_KEY: process.env.SUPABASE_SERVICE_ROLE_KEY,
SUPABASE_URL: process.env.SUPABASE_URL,
},
Expand All @@ -29,6 +31,8 @@ export const env = createEnv({
PODCAST_INDEX_API_KEY: z.string().min(1),
PODCAST_INDEX_SECRET: z.string().min(1),
SLACK_POSTMAN_WEBHOOK_URL: z.string().min(1),
STRIPE_PUBLISHABLE_KEY: z.string().min(1),
STRIPE_SECRET_KEY: z.string().min(1),
SUPABASE_SERVICE_ROLE_KEY: z.string().min(1),
SUPABASE_URL: z.string().min(1),
},
Expand Down
11 changes: 11 additions & 0 deletions lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ export class DatabaseError extends ExtendableError {
this.underlyingError = error;
}
}

export const createExternalServiceError = (serviceName: string) =>
class ExternalServiceError extends ExtendableError {
serviceName = serviceName;
underlyingError: unknown;

constructor(error: unknown) {
super(`Error from ${serviceName}`);
this.underlyingError = error;
}
};
25 changes: 25 additions & 0 deletions lib/services/stripe/checkout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use server';

import { stripe } from './client';

export const createCheckoutSession = async ({
baseUrl,
priceId,
}: {
baseUrl: string;
priceId: string;
}) => {
const session = await stripe.checkout.sessions.create({
cancel_url: `${baseUrl}?status=cancel`,
line_items: [
{
price: priceId,
quantity: 1,
},
],
mode: 'payment',
success_url: `${baseUrl}?status=success`,
});

return session;
};
7 changes: 7 additions & 0 deletions lib/services/stripe/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { env } from '@/env.mjs';
import { createExternalServiceError } from '@/lib/errors';
import { Stripe } from 'stripe';

export const stripe = new Stripe(env.STRIPE_SECRET_KEY);

export const StripeError = createExternalServiceError('Stripe');
22 changes: 22 additions & 0 deletions lib/services/stripe/prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use server';

import { StripeError, stripe } from './client';

const getProduct = async () => {
const products = await stripe.products.list();
const product = products.data.find((p) => p.metadata.project === 'beecast');

if (!product) {
throw new StripeError('Product not found!');
}

return product;
};

export const getPrices = async () => {
const product = await getProduct();
const prices = await stripe.prices.list({ product: product.id });
return prices.data
.filter((p) => p.active)
.sort((a, b) => a.unit_amount! - b.unit_amount!);
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
"es6-error": "^4.1.1",
"format-duration": "^3.0.2",
"html-react-parser": "^5.0.7",
"jotai": "^2.6.0",
"next": "14.0.4",
"next-themes": "1.0.0-beta.0",
"ofetch": "^1.3.3",
"openai": "^4.20.1",
"react": "^18",
"react-dom": "^18",
"react-icons": "^4.12.0",
"stripe": "^14.9.0",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
43 changes: 30 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c89aaf0

Please sign in to comment.