feat: setup_payment in attach body
This commit is contained in:
@@ -23,6 +23,7 @@ import { getOptionsFromCheckoutSession } from "./handleCheckoutCompleted/getOpti
|
||||
import { getEarliestPeriodEnd } from "../stripeSubUtils/convertSubUtils.js";
|
||||
import { notNullish } from "@/utils/genUtils.js";
|
||||
import { CusService } from "@/internal/customers/CusService.js";
|
||||
import { handleSetupCheckout } from "./handleCheckoutCompleted/handleSetupCheckout.js";
|
||||
|
||||
export const handleCheckoutSessionCompleted = async ({
|
||||
req,
|
||||
@@ -75,6 +76,15 @@ export const handleCheckoutSessionCompleted = async ({
|
||||
checkoutSession.metadata?.autumn_metadata_id
|
||||
);
|
||||
|
||||
if (attachParams.setupPayment) {
|
||||
await handleSetupCheckout({
|
||||
req,
|
||||
db,
|
||||
attachParams,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const checkoutSub =
|
||||
checkoutSession.subscription as Stripe.Subscription | null;
|
||||
|
||||
|
||||
@@ -27,9 +27,7 @@ export const handleCheckoutSub = async ({
|
||||
}) => {
|
||||
const { org, customer } = attachParams;
|
||||
|
||||
if (!subscription) {
|
||||
return;
|
||||
}
|
||||
if (!subscription) return;
|
||||
|
||||
const { start, end } = subToPeriodStartEnd({ sub: subscription });
|
||||
|
||||
|
||||
@@ -26,7 +26,9 @@ export const handleRemainingSets = async ({
|
||||
let remainingSets = itemSets ? itemSets.slice(1) : [];
|
||||
|
||||
const remainingItems = remainingSets.flatMap((set) => set.items);
|
||||
let invoiceIds: string[] = [checkoutSession.invoice as string];
|
||||
let invoiceIds: string[] = checkoutSession.invoice
|
||||
? [checkoutSession.invoice as string]
|
||||
: [];
|
||||
|
||||
// Replace items with empty price if needed...
|
||||
for (const price of attachParams.prices) {
|
||||
|
||||
34
server/src/external/stripe/webhookHandlers/handleCheckoutCompleted/handleSetupCheckout.ts
vendored
Normal file
34
server/src/external/stripe/webhookHandlers/handleCheckoutCompleted/handleSetupCheckout.ts
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
import { DrizzleCli } from "@/db/initDrizzle.js";
|
||||
import { handleAddProduct } from "@/internal/customers/attach/attachFunctions/addProductFlow/handleAddProduct.js";
|
||||
import { getDefaultAttachConfig } from "@/internal/customers/attach/attachUtils/getAttachConfig.js";
|
||||
import { AttachParams } from "@/internal/customers/cusProducts/AttachParams.js";
|
||||
import { ExtendedRequest } from "@/utils/models/Request.js";
|
||||
import { AttachBranch } from "@autumn/shared";
|
||||
import Stripe from "stripe";
|
||||
import { createStripeCli } from "../../utils.js";
|
||||
|
||||
export const handleSetupCheckout = async ({
|
||||
req,
|
||||
db,
|
||||
attachParams,
|
||||
}: {
|
||||
req: ExtendedRequest;
|
||||
db: DrizzleCli;
|
||||
attachParams: AttachParams;
|
||||
}) => {
|
||||
const logger = req.logger;
|
||||
const { org, customer } = attachParams;
|
||||
|
||||
logger.info(`HANDLING SETUP CHECKOUT COMPLETED`);
|
||||
|
||||
// 1. Check attach prices...
|
||||
await handleAddProduct({
|
||||
req,
|
||||
attachParams: {
|
||||
...attachParams,
|
||||
stripeCli: createStripeCli({ org, env: customer.env }),
|
||||
},
|
||||
branch: AttachBranch.New,
|
||||
config: getDefaultAttachConfig(),
|
||||
});
|
||||
};
|
||||
@@ -119,7 +119,7 @@ export const handleCreateCheckout = async ({
|
||||
notNullish(checkoutParams.payment_method_types) ||
|
||||
notNullish(checkoutParams.payment_method_configuration);
|
||||
|
||||
const sessionParams = {
|
||||
let sessionParams = {
|
||||
customer: customer.processor.id,
|
||||
line_items: items,
|
||||
subscription_data: subscriptionData,
|
||||
@@ -145,6 +145,20 @@ export const handleCreateCheckout = async ({
|
||||
: undefined,
|
||||
} satisfies Stripe.Checkout.SessionCreateParams;
|
||||
|
||||
if (attachParams.setupPayment) {
|
||||
sessionParams = {
|
||||
customer: customer.processor?.id,
|
||||
mode: "setup",
|
||||
success_url: successUrl || toSuccessUrl({ org, env: customer.env }),
|
||||
currency: org.default_currency || "usd",
|
||||
...(checkoutParams as any),
|
||||
metadata: {
|
||||
...(attachParams.checkoutSessionParams?.metadata || {}),
|
||||
autumn_metadata_id: metaId,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
checkout = await stripeCli.checkout.sessions.create(sessionParams);
|
||||
logger.info(
|
||||
|
||||
@@ -80,6 +80,7 @@ export const getAttachParams = async ({
|
||||
disableFreeTrial: attachBody.free_trial === false || false,
|
||||
checkoutSessionParams: attachBody.checkout_session_params,
|
||||
isCustom: attachBody.is_custom,
|
||||
setupPayment: attachBody.setup_payment,
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { AttachParams } from "@/internal/customers/cusProducts/AttachParams.js";
|
||||
import { AttachBranch } from "@autumn/shared";
|
||||
|
||||
export const handleCheckoutErrors = ({
|
||||
attachParams,
|
||||
branch,
|
||||
}: {
|
||||
attachParams: AttachParams;
|
||||
branch: AttachBranch;
|
||||
}) => {
|
||||
if (attachParams.setupPayment) {
|
||||
// Make sure only usage prices are added?
|
||||
}
|
||||
};
|
||||
@@ -76,6 +76,7 @@ export type AttachParams = {
|
||||
finalizeInvoice?: boolean;
|
||||
req?: any;
|
||||
fromCancel?: boolean;
|
||||
setupPayment?: boolean;
|
||||
};
|
||||
|
||||
export type InsertCusProductParams = {
|
||||
|
||||
@@ -56,6 +56,7 @@ export const AttachBodySchema = z
|
||||
|
||||
// Checkout params
|
||||
skip_checkout: z.boolean().optional(),
|
||||
setup_payment: z.boolean().optional(),
|
||||
})
|
||||
.refine(
|
||||
(data) => {
|
||||
|
||||
Reference in New Issue
Block a user