mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-13 04:45:19 +00:00
move stripe product id to env var
This commit is contained in:
parent
84e8ec2102
commit
0b4140f81c
3 changed files with 52 additions and 47 deletions
|
|
@ -17,7 +17,7 @@ import { headers } from "next/headers"
|
||||||
import { stripe } from "@/lib/stripe"
|
import { stripe } from "@/lib/stripe"
|
||||||
import { getUser } from "@/data/user";
|
import { getUser } from "@/data/user";
|
||||||
import { Session } from "next-auth";
|
import { Session } from "next-auth";
|
||||||
import Stripe from "stripe";
|
import { STRIPE_PRODUCT_ID } from "@/lib/environment";
|
||||||
|
|
||||||
const ajv = new Ajv({
|
const ajv = new Ajv({
|
||||||
validateFormats: false,
|
validateFormats: false,
|
||||||
|
|
@ -413,56 +413,53 @@ const parseConnectionConfig = (connectionType: string, config: string) => {
|
||||||
return parsedConfig;
|
return parsedConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchStripeClientSecret(name: string, domain: string) {
|
export const setupInitialStripeCustomer = async (name: string, domain: string) =>
|
||||||
const session = await auth();
|
withAuth(async (session) => {
|
||||||
if (!session) {
|
const user = await getUser(session.user.id);
|
||||||
return "";
|
if (!user) {
|
||||||
}
|
return "";
|
||||||
const user = await getUser(session.user.id);
|
}
|
||||||
if (!user) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
const origin = (await headers()).get('origin')
|
const origin = (await headers()).get('origin')
|
||||||
|
|
||||||
const test_clock = await stripe.testHelpers.testClocks.create({
|
const test_clock = await stripe.testHelpers.testClocks.create({
|
||||||
frozen_time: Math.floor(Date.now() / 1000)
|
frozen_time: Math.floor(Date.now() / 1000)
|
||||||
})
|
})
|
||||||
|
|
||||||
const customer = await stripe.customers.create({
|
const customer = await stripe.customers.create({
|
||||||
name: user.name!,
|
name: user.name!,
|
||||||
email: user.email!,
|
email: user.email!,
|
||||||
test_clock: test_clock.id
|
test_clock: test_clock.id
|
||||||
})
|
})
|
||||||
|
|
||||||
const prices = await stripe.prices.list({
|
const prices = await stripe.prices.list({
|
||||||
product: 'prod_RkeYDKNFsZJROd',
|
product: STRIPE_PRODUCT_ID,
|
||||||
expand: ['data.product'],
|
expand: ['data.product'],
|
||||||
});
|
});
|
||||||
const stripeSession = await stripe.checkout.sessions.create({
|
const stripeSession = await stripe.checkout.sessions.create({
|
||||||
ui_mode: 'embedded',
|
ui_mode: 'embedded',
|
||||||
customer: customer.id,
|
customer: customer.id,
|
||||||
line_items: [
|
line_items: [
|
||||||
{
|
{
|
||||||
price: prices.data[0].id,
|
price: prices.data[0].id,
|
||||||
quantity: 1
|
quantity: 1
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
mode: 'subscription',
|
mode: 'subscription',
|
||||||
subscription_data: {
|
subscription_data: {
|
||||||
trial_period_days: 7,
|
trial_period_days: 7,
|
||||||
trial_settings: {
|
trial_settings: {
|
||||||
end_behavior: {
|
end_behavior: {
|
||||||
missing_payment_method: 'cancel',
|
missing_payment_method: 'cancel',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
payment_method_collection: 'if_required',
|
||||||
payment_method_collection: 'if_required',
|
return_url: `${origin}/onboard/complete?session_id={CHECKOUT_SESSION_ID}&org_name=${name}&org_domain=${domain}`,
|
||||||
return_url: `${origin}/onboard/complete?session_id={CHECKOUT_SESSION_ID}&org_name=${name}&org_domain=${domain}`,
|
})
|
||||||
})
|
|
||||||
|
|
||||||
return stripeSession.client_secret!;
|
return stripeSession.client_secret!;
|
||||||
}
|
});
|
||||||
|
|
||||||
export const getSubscriptionCheckoutRedirect = async (domain: string) =>
|
export const getSubscriptionCheckoutRedirect = async (domain: string) =>
|
||||||
withAuth((session) =>
|
withAuth((session) =>
|
||||||
|
|
@ -489,7 +486,7 @@ export const getSubscriptionCheckoutRedirect = async (domain: string) =>
|
||||||
|
|
||||||
const origin = (await headers()).get('origin')
|
const origin = (await headers()).get('origin')
|
||||||
const prices = await stripe.prices.list({
|
const prices = await stripe.prices.list({
|
||||||
product: 'prod_RkeYDKNFsZJROd',
|
product: STRIPE_PRODUCT_ID,
|
||||||
expand: ['data.product'],
|
expand: ['data.product'],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ import logoDark from "@/public/sb_logo_dark_large.png";
|
||||||
import logoLight from "@/public/sb_logo_light_large.png";
|
import logoLight from "@/public/sb_logo_light_large.png";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
|
|
||||||
import { fetchStripeClientSecret } from "../../../actions"
|
import { setupInitialStripeCustomer } from "../../../actions"
|
||||||
import {
|
import {
|
||||||
EmbeddedCheckout,
|
EmbeddedCheckout,
|
||||||
EmbeddedCheckoutProvider
|
EmbeddedCheckoutProvider
|
||||||
|
|
@ -21,6 +21,7 @@ import {
|
||||||
import { loadStripe } from '@stripe/stripe-js'
|
import { loadStripe } from '@stripe/stripe-js'
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { OnboardingFormValues } from "./orgCreateForm";
|
import { OnboardingFormValues } from "./orgCreateForm";
|
||||||
|
import { isServiceError } from "@/lib/utils";
|
||||||
|
|
||||||
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!)
|
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!)
|
||||||
|
|
||||||
|
|
@ -33,7 +34,13 @@ export function TrialCard({ orgCreateInfo }: { orgCreateInfo: OnboardingFormValu
|
||||||
<div id="checkout">
|
<div id="checkout">
|
||||||
<EmbeddedCheckoutProvider
|
<EmbeddedCheckoutProvider
|
||||||
stripe={stripePromise}
|
stripe={stripePromise}
|
||||||
options={{ fetchClientSecret: () => fetchStripeClientSecret(orgCreateInfo.name, orgCreateInfo.domain) }}
|
options={{ fetchClientSecret: async () => {
|
||||||
|
const clientSecret = await setupInitialStripeCustomer(orgCreateInfo.name, orgCreateInfo.domain);
|
||||||
|
if (isServiceError(clientSecret)) {
|
||||||
|
throw clientSecret;
|
||||||
|
}
|
||||||
|
return clientSecret;
|
||||||
|
} }}
|
||||||
>
|
>
|
||||||
<EmbeddedCheckout />
|
<EmbeddedCheckout />
|
||||||
</EmbeddedCheckoutProvider>
|
</EmbeddedCheckoutProvider>
|
||||||
|
|
|
||||||
|
|
@ -15,3 +15,4 @@ export const AUTH_GOOGLE_CLIENT_SECRET = getEnv(process.env.AUTH_GOOGLE_CLIENT_S
|
||||||
export const AUTH_URL = getEnv(process.env.AUTH_URL)!;
|
export const AUTH_URL = getEnv(process.env.AUTH_URL)!;
|
||||||
|
|
||||||
export const STRIPE_SECRET_KEY = getEnv(process.env.STRIPE_SECRET_KEY);
|
export const STRIPE_SECRET_KEY = getEnv(process.env.STRIPE_SECRET_KEY);
|
||||||
|
export const STRIPE_PRODUCT_ID = getEnv(process.env.STRIPE_PRODUCT_ID);
|
||||||
Loading…
Reference in a new issue