mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 12:25:22 +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 { getUser } from "@/data/user";
|
||||
import { Session } from "next-auth";
|
||||
import Stripe from "stripe";
|
||||
import { STRIPE_PRODUCT_ID } from "@/lib/environment";
|
||||
|
||||
const ajv = new Ajv({
|
||||
validateFormats: false,
|
||||
|
|
@ -413,56 +413,53 @@ const parseConnectionConfig = (connectionType: string, config: string) => {
|
|||
return parsedConfig;
|
||||
}
|
||||
|
||||
export async function fetchStripeClientSecret(name: string, domain: string) {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
return "";
|
||||
}
|
||||
const user = await getUser(session.user.id);
|
||||
if (!user) {
|
||||
return "";
|
||||
}
|
||||
export const setupInitialStripeCustomer = async (name: string, domain: string) =>
|
||||
withAuth(async (session) => {
|
||||
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({
|
||||
frozen_time: Math.floor(Date.now() / 1000)
|
||||
})
|
||||
const test_clock = await stripe.testHelpers.testClocks.create({
|
||||
frozen_time: Math.floor(Date.now() / 1000)
|
||||
})
|
||||
|
||||
const customer = await stripe.customers.create({
|
||||
name: user.name!,
|
||||
email: user.email!,
|
||||
test_clock: test_clock.id
|
||||
})
|
||||
const customer = await stripe.customers.create({
|
||||
name: user.name!,
|
||||
email: user.email!,
|
||||
test_clock: test_clock.id
|
||||
})
|
||||
|
||||
const prices = await stripe.prices.list({
|
||||
product: 'prod_RkeYDKNFsZJROd',
|
||||
expand: ['data.product'],
|
||||
});
|
||||
const stripeSession = await stripe.checkout.sessions.create({
|
||||
ui_mode: 'embedded',
|
||||
customer: customer.id,
|
||||
line_items: [
|
||||
{
|
||||
price: prices.data[0].id,
|
||||
quantity: 1
|
||||
}
|
||||
],
|
||||
mode: 'subscription',
|
||||
subscription_data: {
|
||||
trial_period_days: 7,
|
||||
trial_settings: {
|
||||
end_behavior: {
|
||||
missing_payment_method: 'cancel',
|
||||
const prices = await stripe.prices.list({
|
||||
product: STRIPE_PRODUCT_ID,
|
||||
expand: ['data.product'],
|
||||
});
|
||||
const stripeSession = await stripe.checkout.sessions.create({
|
||||
ui_mode: 'embedded',
|
||||
customer: customer.id,
|
||||
line_items: [
|
||||
{
|
||||
price: prices.data[0].id,
|
||||
quantity: 1
|
||||
}
|
||||
],
|
||||
mode: 'subscription',
|
||||
subscription_data: {
|
||||
trial_period_days: 7,
|
||||
trial_settings: {
|
||||
end_behavior: {
|
||||
missing_payment_method: 'cancel',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
payment_method_collection: 'if_required',
|
||||
return_url: `${origin}/onboard/complete?session_id={CHECKOUT_SESSION_ID}&org_name=${name}&org_domain=${domain}`,
|
||||
})
|
||||
payment_method_collection: 'if_required',
|
||||
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) =>
|
||||
withAuth((session) =>
|
||||
|
|
@ -489,7 +486,7 @@ export const getSubscriptionCheckoutRedirect = async (domain: string) =>
|
|||
|
||||
const origin = (await headers()).get('origin')
|
||||
const prices = await stripe.prices.list({
|
||||
product: 'prod_RkeYDKNFsZJROd',
|
||||
product: STRIPE_PRODUCT_ID,
|
||||
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 Image from "next/image";
|
||||
|
||||
import { fetchStripeClientSecret } from "../../../actions"
|
||||
import { setupInitialStripeCustomer } from "../../../actions"
|
||||
import {
|
||||
EmbeddedCheckout,
|
||||
EmbeddedCheckoutProvider
|
||||
|
|
@ -21,6 +21,7 @@ import {
|
|||
import { loadStripe } from '@stripe/stripe-js'
|
||||
import { useState } from "react";
|
||||
import { OnboardingFormValues } from "./orgCreateForm";
|
||||
import { isServiceError } from "@/lib/utils";
|
||||
|
||||
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!)
|
||||
|
||||
|
|
@ -33,7 +34,13 @@ export function TrialCard({ orgCreateInfo }: { orgCreateInfo: OnboardingFormValu
|
|||
<div id="checkout">
|
||||
<EmbeddedCheckoutProvider
|
||||
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 />
|
||||
</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 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