Skip to content

Commit 888a2e3

Browse files
committed
change stripe init to be behind function
1 parent 6234967 commit 888a2e3

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

.github/workflows/staging-ghcr-public.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ jobs:
7070
build-args: |
7171
SOURCEBOT_VERSION=${{ github.ref_name }}
7272
POSTHOG_PAPIK=${{ secrets.POSTHOG_PAPIK }}
73-
SOURCEBOT_ENCRYPTION_KEY=${{ secrets.STAGING_SOURCEBOT_ENCRYPTION_KEY }}
7473
7574
- name: Export digest
7675
run: |

packages/web/src/actions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { encrypt } from "@sourcebot/crypto"
1414
import { getConnection } from "./data/connection";
1515
import { ConnectionSyncStatus, Prisma, Invite } from "@sourcebot/db";
1616
import { headers } from "next/headers"
17-
import { stripe } from "@/lib/stripe"
17+
import { getStripe } from "@/lib/stripe"
1818
import { getUser } from "@/data/user";
1919
import { Session } from "next-auth";
2020
import { STRIPE_PRODUCT_ID } from "@/lib/environment";
@@ -339,6 +339,7 @@ export const redeemInvite = async (invite: Invite, userId: string): Promise<{ su
339339
const existingSeatCount = subscription.items.data[0].quantity;
340340
const newSeatCount = (existingSeatCount || 1) + 1
341341

342+
const stripe = getStripe();
342343
await stripe.subscriptionItems.update(
343344
subscription.items.data[0].id,
344345
{
@@ -424,6 +425,7 @@ export const setupInitialStripeCustomer = async (name: string, domain: string) =
424425
return "";
425426
}
426427

428+
const stripe = getStripe();
427429
const origin = (await headers()).get('origin')
428430

429431
// @nocheckin
@@ -489,6 +491,7 @@ export const getSubscriptionCheckoutRedirect = async (domain: string) =>
489491
});
490492
const numOrgMembers = orgMembers.length;
491493

494+
const stripe = getStripe();
492495
const origin = (await headers()).get('origin')
493496
const prices = await stripe.prices.list({
494497
product: STRIPE_PRODUCT_ID,
@@ -520,6 +523,7 @@ export const getSubscriptionCheckoutRedirect = async (domain: string) =>
520523
)
521524

522525
export async function fetchStripeSession(sessionId: string) {
526+
const stripe = getStripe();
523527
const stripeSession = await stripe.checkout.sessions.retrieve(sessionId);
524528
return stripeSession;
525529
}
@@ -537,6 +541,7 @@ export const getCustomerPortalSessionLink = async (domain: string): Promise<stri
537541
return notFound();
538542
}
539543

544+
const stripe = getStripe();
540545
const origin = (await headers()).get('origin')
541546
const portalSession = await stripe.billingPortal.sessions.create({
542547
customer: org.stripeCustomerId as string,
@@ -558,6 +563,7 @@ export const fetchSubscription = (domain: string): Promise<Stripe.Subscription |
558563
return notFound();
559564
}
560565

566+
const stripe = getStripe();
561567
const subscriptions = await stripe.subscriptions.list({
562568
customer: org.stripeCustomerId
563569
});
@@ -624,6 +630,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{
624630
const existingSeatCount = subscription.items.data[0].quantity;
625631
const newSeatCount = (existingSeatCount || 1) - 1;
626632

633+
const stripe = getStripe();
627634
await stripe.subscriptionItems.update(
628635
subscription.items.data[0].id,
629636
{

packages/web/src/lib/stripe.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ import 'server-only';
33
import Stripe from 'stripe'
44
import { STRIPE_SECRET_KEY } from './environment'
55

6-
export const stripe = new Stripe(STRIPE_SECRET_KEY!)
6+
let stripeInstance: Stripe | null = null;
7+
export const getStripe = () => {
8+
if (!stripeInstance) {
9+
stripeInstance = new Stripe(STRIPE_SECRET_KEY!);
10+
}
11+
return stripeInstance;
12+
}

0 commit comments

Comments
 (0)