Nick: init
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { Logger } from "../../lib/logger";
|
||||
import Stripe from "stripe";
|
||||
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? "");
|
||||
|
||||
async function getCustomerDefaultPaymentMethod(customerId: string) {
|
||||
const paymentMethods = await stripe.customers.listPaymentMethods(customerId, {
|
||||
limit: 3,
|
||||
});
|
||||
return paymentMethods.data[0]?.id;
|
||||
}
|
||||
|
||||
type ReturnStatus = "succeeded" | "requires_action" | "failed";
|
||||
export async function createPaymentIntent(
|
||||
team_id: string,
|
||||
customer_id: string
|
||||
): Promise<{ return_status: ReturnStatus; charge_id: string }> {
|
||||
try {
|
||||
const paymentIntent = await stripe.paymentIntents.create({
|
||||
amount: 1100,
|
||||
currency: "usd",
|
||||
customer: customer_id,
|
||||
description: "Firecrawl: Auto re-charge of 1000 credits",
|
||||
payment_method_types: ["card"],
|
||||
payment_method: await getCustomerDefaultPaymentMethod(customer_id),
|
||||
off_session: true,
|
||||
confirm: true,
|
||||
});
|
||||
|
||||
if (paymentIntent.status === "succeeded") {
|
||||
Logger.info(`Payment succeeded for team: ${team_id}`);
|
||||
return { return_status: "succeeded", charge_id: paymentIntent.id };
|
||||
} else if (
|
||||
paymentIntent.status === "requires_action" ||
|
||||
paymentIntent.status === "processing" ||
|
||||
paymentIntent.status === "requires_capture"
|
||||
) {
|
||||
Logger.warn(`Payment requires further action for team: ${team_id}`);
|
||||
return { return_status: "requires_action", charge_id: paymentIntent.id };
|
||||
} else {
|
||||
Logger.error(`Payment failed for team: ${team_id}`);
|
||||
return { return_status: "failed", charge_id: paymentIntent.id };
|
||||
}
|
||||
} catch (error) {
|
||||
Logger.error(
|
||||
`Failed to create or confirm PaymentIntent for team: ${team_id}`
|
||||
);
|
||||
console.error(error);
|
||||
return { return_status: "failed", charge_id: "" };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user