Update webhook handler and tailwind paths, add first subscription ite… (#40)

* Update webhook handler and tailwind paths, add first subscription item to types

In the lemon-squeezy-webhook-handler, changed logger from error to warning and refactored the generation of lineItems to use firstSubscriptionItem details and obtain the priceAmount. In tailwind's index file, adjusted the path to include src directories in packages. Also, the first_subscription_item has been added to the subscription-webhook type for better data representation.

* Refactor subscription interval calculation

The subscription interval calculation was refactored to use the new 'getSubscriptionIntervalType' function. This new function provides a more accurate calculation, where it determines the interval type ('monthly' or 'yearly') by comparing the renewal date with the current date.

* Update dependencies across multiple packages

This commit includes updates to the version of "supabase-js", "lucide-react", "react-query", "react-table", "postcss", "stripe" among other dependencies in multiple package.json files. The "pnpm-lock.yaml" file is also updated to reflect these changes. This ensures the project dependencies stay up-to-date to include new features, improvements and bug fixes.
This commit is contained in:
Giancarlo Buomprisco
2024-06-30 23:29:06 +08:00
committed by GitHub
parent 90c1e23821
commit 9583aeec9f
20 changed files with 333 additions and 227 deletions

View File

@@ -27,10 +27,10 @@
"@kit/tailwind-config": "workspace:*",
"@kit/tsconfig": "workspace:*",
"@kit/ui": "workspace:^",
"@supabase/supabase-js": "^2.44.1",
"@supabase/supabase-js": "^2.44.2",
"@types/react": "^18.3.3",
"date-fns": "^3.6.0",
"lucide-react": "^0.397.0",
"lucide-react": "^0.399.0",
"next": "14.2.4",
"react": "18.3.1",
"react-hook-form": "^7.52.0",

View File

@@ -1,13 +1,13 @@
import {
getOrder,
getSubscription,
getVariant,
} from '@lemonsqueezy/lemonsqueezy.js';
import { getOrder, getSubscription, getVariant } from '@lemonsqueezy/lemonsqueezy.js';
import { BillingConfig, BillingWebhookHandlerService } from '@kit/billing';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
import { getLemonSqueezyEnv } from '../schema/lemon-squeezy-server-env.schema';
import { OrderWebhook } from '../types/order-webhook';
import { SubscriptionInvoiceWebhook } from '../types/subscription-invoice-webhook';
@@ -16,6 +16,7 @@ import { initializeLemonSqueezyClient } from './lemon-squeezy-sdk';
import { createLemonSqueezySubscriptionPayloadBuilderService } from './lemon-squeezy-subscription-payload-builder.service';
import { createHmac } from './verify-hmac';
type UpsertSubscriptionParams =
Database['public']['Functions']['upsert_subscription']['Args'] & {
line_items: Array<LineItem>;
@@ -238,14 +239,13 @@ export class LemonSqueezyWebhookHandlerService
const endsAt = subscription.ends_at;
const renewsAt = subscription.renews_at;
const trialEndsAt = subscription.trial_ends_at;
const intervalCount = subscription.billing_anchor;
const { data: order, error } = await getOrder(orderId);
if (error ?? !order) {
const logger = await getLogger();
logger.error(
logger.warn(
{
orderId,
subscriptionId,
@@ -258,17 +258,20 @@ export class LemonSqueezyWebhookHandlerService
throw new Error('Failed to fetch order');
}
const priceAmount = order?.data.attributes.first_order_item.price ?? 0;
const firstSubscriptionItem = subscription.first_subscription_item;
const lineItems = [
{
id: subscription.order_item_id.toString(),
id: firstSubscriptionItem.id.toString(),
product: productId.toString(),
variant: variantId.toString(),
quantity: order.data.attributes.first_order_item.quantity,
priceAmount: order.data.attributes.first_order_item.price,
quantity: firstSubscriptionItem.quantity,
priceAmount,
},
];
const interval = intervalCount === 1 ? 'month' : 'year';
const { interval, intervalCount } = getSubscriptionIntervalType(renewsAt);
const payloadBuilderService =
createLemonSqueezySubscriptionPayloadBuilderService();
@@ -419,3 +422,26 @@ async function isSigningSecretValid(rawBody: string, signatureHeader: string) {
function timingSafeEqual(digest: string, signature: Buffer) {
return digest.toString() === signature.toString();
}
function getSubscriptionIntervalType(renewsAt: string) {
const renewalDate = new Date(renewsAt);
const currentDate = new Date();
// Calculate the difference in milliseconds
const timeDifference = renewalDate.getTime() - currentDate.getTime();
// Convert milliseconds to days
const daysDifference = timeDifference / (1000 * 3600 * 24);
if (daysDifference <= 32) {
return {
interval: 'monthly',
intervalCount: 1,
};
}
return {
interval: 'yearly',
intervalCount: 12,
};
}

View File

@@ -43,6 +43,15 @@ interface Attributes {
created_at: string;
updated_at: string;
test_mode: boolean;
first_subscription_item: {
id: number;
subscription_id: number;
price_id: number;
quantity: number;
created_at: string;
updated_at: string;
};
}
interface Urls {

View File

@@ -17,7 +17,7 @@
"dependencies": {
"@stripe/react-stripe-js": "^2.7.2",
"@stripe/stripe-js": "^4.0.0",
"stripe": "^16.0.0"
"stripe": "^16.1.0"
},
"devDependencies": {
"@kit/billing": "workspace:^",