Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Dec 5, 2024
1 parent 4f414e0 commit 47795b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
1 change: 1 addition & 0 deletions plugin/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function checkLicenseKey(
Authorization: `Bearer ${key}`,
},
});
console.log("response", response.json);
return response.status === 200;
} catch (error) {
logger.error("Error checking API key:", error);
Expand Down
51 changes: 9 additions & 42 deletions web/app/api/webhook/handlers/invoice-paid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,6 @@ async function resetUserUsageAndSetLastPayment(userId: string) {
.where(eq(UserUsageTable.userId, userId));
}

async function getSrmPriceKey(invoice: Stripe.Invoice) {
return invoice.lines.data[0].price?.metadata?.srm_price_key || "default";
}

async function getStripeProduct(invoice: Stripe.Invoice) {
const product = await stripe.products.retrieve(
invoice.lines.data[0].price?.product as string
);
return product;
}

async function getSrmProductKey(invoice: Stripe.Invoice) {
const product = await getStripeProduct(invoice);
return product.metadata?.srm_product_key || "default";
}

function createCustomerDataFromInvoice(
invoice: Stripe.Invoice,
priceKey: string,
productKey: string
): CustomerData {
return {
userId:
invoice.subscription_details?.metadata?.userId ||
invoice.metadata?.userId,
customerId: invoice.customer.toString(),
status: invoice.status,
billingCycle: priceKey as "monthly" | "lifetime" | "yearly",
paymentStatus: invoice.status,
product: productKey,
plan: priceKey,
lastPayment: new Date(),
};
}


export const handleInvoicePaid = createWebhookHandler(
Expand All @@ -68,35 +34,36 @@ export const handleInvoicePaid = createWebhookHandler(
message: "No subscription details found",
};
}
const metadata = invoice.subscription_details.metadata;

await db
.insert(UserUsageTable)
.values({
userId: invoice.subscription_details.metadata?.userId,
userId: metadata?.userId,
subscriptionStatus: invoice.status,
paymentStatus: invoice.status,
billingCycle: invoice.subscription_details.metadata?.type as
billingCycle: metadata?.type as
| "monthly"
| "yearly"
| "lifetime",
maxTokenUsage: 5000 * 1000,
lastPayment: new Date(),
currentProduct: invoice.subscription_details.metadata?.product,
currentPlan: invoice.subscription_details.metadata?.plan,
currentProduct: metadata?.product,
currentPlan: metadata?.plan,
})
.onConflictDoUpdate({
target: [UserUsageTable.userId],
set: {
subscriptionStatus: invoice.status,
paymentStatus: invoice.status,
maxTokenUsage: 5000 * 1000,
billingCycle: invoice.subscription_details.metadata?.type as
maxTokenUsage: 5000 * 1000,
billingCycle: metadata?.type as
| "monthly"
| "yearly"
| "lifetime",
lastPayment: new Date(),
currentProduct: invoice.subscription_details.metadata?.product,
currentPlan: invoice.subscription_details.metadata?.plan,
currentProduct: metadata?.product,
currentPlan: metadata?.plan,
},
});

Expand Down

0 comments on commit 47795b2

Please sign in to comment.