Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stripe boilerplate #73

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!);

Check warning on line 21 in lib/services/stripe/prices.ts

View workflow job for this annotation

GitHub Actions / lint_and_typecheck

Forbidden non-null assertion

Check warning on line 21 in lib/services/stripe/prices.ts

View workflow job for this annotation

GitHub Actions / lint_and_typecheck

Forbidden non-null assertion
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"react": "^18",
"react-dom": "^18",
"react-icons": "^4.12.0",
"stripe": "^14.9.0",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
24 changes: 11 additions & 13 deletions pnpm-lock.yaml

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

Loading