Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

Use invoiced billing for certain expansion packs (#2532)

Micah Stairs1c7154bf102d
diff --git a/apps/api/src/services/billing/auto_charge.ts b/apps/api/src/services/billing/auto_charge.ts
index da005c1a7..0405347c9 100644
--- a/apps/api/src/services/billing/auto_charge.ts
+++ b/apps/api/src/services/billing/auto_charge.ts
@@ -115,8 +115,8 @@ async function _autoChargeScale(
chunk.price_associated_auto_recharge_price_id === undefined
? "undefined"
: JSON.stringify(
- chunk.price_associated_auto_recharge_price_id,
- ),
+ chunk.price_associated_auto_recharge_price_id,
+ ),
});
return {
success: false,
@@ -127,6 +127,11 @@ async function _autoChargeScale(
};
}
+ const invoiceOnly = Boolean(
+ (price.metadata as { invoice?: boolean } | null | undefined)
+ ?.invoice,
+ );
+
// Check for recharges this month
const currentMonth = new Date();
@@ -224,6 +229,7 @@ async function _autoChargeScale(
customer.stripe_customer_id,
chunk.price_associated_auto_recharge_price_id,
chunk.sub_id,
+ invoiceOnly,
);
if (!subscription) {
logger.error("Failed to create subscription");
@@ -263,13 +269,13 @@ async function _autoChargeScale(
canceled_at: null,
current_period_start: subscription.current_period_start
? new Date(
- subscription.current_period_start * 1000,
- ).toISOString()
+ subscription.current_period_start * 1000,
+ ).toISOString()
: null,
current_period_end: subscription.current_period_end
? new Date(
- subscription.current_period_end * 1000,
- ).toISOString()
+ subscription.current_period_end * 1000,
+ ).toISOString()
: null,
created: subscription.created
? new Date(subscription.created * 1000).toISOString()
diff --git a/apps/api/src/services/billing/stripe.ts b/apps/api/src/services/billing/stripe.ts
index 4e3444da0..d74838f29 100644
--- a/apps/api/src/services/billing/stripe.ts
+++ b/apps/api/src/services/billing/stripe.ts
@@ -69,10 +69,12 @@ export async function createSubscription(
customer_id: string,
price_id: string,
main_subscription_id: string,
+ invoiceOnly = false,
) {
- const defaultPaymentMethod =
- await getCustomerDefaultPaymentMethod(customer_id);
- if (!defaultPaymentMethod) {
+ const defaultPaymentMethod = invoiceOnly
+ ? null
+ : await getCustomerDefaultPaymentMethod(customer_id);
+ if (!invoiceOnly && !defaultPaymentMethod) {
logger.error("No default payment method found for customer", {
team_id,
customer_id,
@@ -153,15 +155,22 @@ export async function createSubscription(
const subscription = await stripe.subscriptions.create({
customer: customer_id,
items: [{ price: price_id }],
- off_session: true,
- payment_settings: {
- payment_method_types: [
- (defaultPaymentMethod?.type as Stripe.SubscriptionCreateParams.PaymentSettings.PaymentMethodType) ??
- "card",
- ],
- },
- default_payment_method: defaultPaymentMethod.id,
- collection_method: "charge_automatically",
+ ...(invoiceOnly
+ ? {
+ collection_method: "send_invoice",
+ days_until_due: 30,
+ }
+ : {
+ off_session: true,
+ payment_settings: {
+ payment_method_types: [
+ (defaultPaymentMethod?.type as Stripe.SubscriptionCreateParams.PaymentSettings.PaymentMethodType) ??
+ "card",
+ ],
+ },
+ default_payment_method: defaultPaymentMethod?.id,
+ collection_method: "charge_automatically",
+ }),
metadata: {
team_id,
auto_recharge: "true",