56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { runtimeEnv } from "@/utils/envUtils.js";
|
|
import { type AppEnv, ErrCode } from "@autumn/shared";
|
|
import Stripe from "stripe";
|
|
import RecaseError from "@/utils/errorUtils.js";
|
|
import {
|
|
MAIN_STRIPE_EVENT_TYPES,
|
|
SYNC_STRIPE_EVENT_TYPES,
|
|
} from "./common/stripeConstants";
|
|
|
|
export const checkKeyValid = async (apiKey: string) => {
|
|
const stripe = new Stripe(apiKey);
|
|
|
|
// Call customers.list
|
|
await stripe.customers.list();
|
|
};
|
|
|
|
export const createWebhookEndpoint = async (
|
|
apiKey: string,
|
|
env: AppEnv,
|
|
orgId: string,
|
|
) => {
|
|
const stripe = new Stripe(apiKey);
|
|
|
|
const webhookBaseUrl = runtimeEnv.STRIPE_WEBHOOK_URL || runtimeEnv.SERVER_URL;
|
|
|
|
if (!webhookBaseUrl) {
|
|
throw new RecaseError({
|
|
message: "Stripe webhook baseURL not found",
|
|
code: ErrCode.StripeKeyInvalid,
|
|
statusCode: 500,
|
|
});
|
|
}
|
|
|
|
const endpoint = await stripe.webhookEndpoints.create({
|
|
url: `${webhookBaseUrl}/webhooks/stripe/${orgId}/${env}`,
|
|
enabled_events: [...MAIN_STRIPE_EVENT_TYPES, ...SYNC_STRIPE_EVENT_TYPES],
|
|
|
|
// [
|
|
// "customer.subscription.created",
|
|
// "customer.subscription.updated",
|
|
// "customer.subscription.deleted",
|
|
// "checkout.session.completed",
|
|
// "invoice.paid",
|
|
// "invoice.upcoming",
|
|
// "invoice.created",
|
|
// "invoice.finalized",
|
|
// "invoice.updated",
|
|
// "subscription_schedule.canceled",
|
|
// "subscription_schedule.updated",
|
|
// "customer.discount.deleted",
|
|
// ],
|
|
});
|
|
|
|
return endpoint;
|
|
};
|