Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: update stripe version

Nevo David98d9ddc83e32
diff --git a/libraries/nestjs-libraries/src/services/stripe.service.ts b/libraries/nestjs-libraries/src/services/stripe.service.ts
index 455dae072..537eaa7a9 100644
--- a/libraries/nestjs-libraries/src/services/stripe.service.ts
+++ b/libraries/nestjs-libraries/src/services/stripe.service.ts
@@ -5,16 +5,14 @@ import { SubscriptionService } from '@gitroom/nestjs-libraries/database/prisma/s
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { BillingSubscribeDto } from '@gitroom/nestjs-libraries/dtos/billing/billing.subscribe.dto';
-import { capitalize, groupBy } from 'lodash';
+import { groupBy } from 'lodash';
import { pricing } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/pricing';
import { AuthService } from '@gitroom/helpers/auth/auth.service';
import { TrackService } from '@gitroom/nestjs-libraries/track/track.service';
import { UsersService } from '@gitroom/nestjs-libraries/database/prisma/users/users.service';
import { TrackEnum } from '@gitroom/nestjs-libraries/user/track.enum';
-const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
- apiVersion: '2024-04-10',
-});
+const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
@Injectable()
export class StripeService {
@@ -102,17 +100,15 @@ export class StripeService {
}
async createSubscription(event: Stripe.CustomerSubscriptionCreatedEvent) {
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
const {
uniqueId,
billing,
period,
- }: {
+ } = event.data.object.metadata as {
billing: 'STANDARD' | 'PRO';
period: 'MONTHLY' | 'YEARLY';
uniqueId: string;
- } = event.data.object.metadata;
+ };
try {
const check = await this.checkValidCard(event);
@@ -134,17 +130,15 @@ export class StripeService {
);
}
async updateSubscription(event: Stripe.CustomerSubscriptionUpdatedEvent) {
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
const {
uniqueId,
billing,
period,
- }: {
+ } = event.data.object.metadata as {
billing: 'STANDARD' | 'PRO';
period: 'MONTHLY' | 'YEARLY';
uniqueId: string;
- } = event.data.object.metadata;
+ };
const check = await this.checkValidCard(event);
if (!check) {
@@ -199,8 +193,7 @@ export class StripeService {
const productsList = groupBy(
products.data.map((p) => ({
- // @ts-ignore
- name: p.product?.name,
+ name: (p.product as Stripe.Product)?.name,
recurring: p?.recurring?.interval!,
price: p?.tiers?.[0]?.unit_amount! / 100,
})),
@@ -271,19 +264,21 @@ export class StripeService {
};
try {
- const price = await stripe.invoices.retrieveUpcoming({
+ const price = await stripe.invoices.createPreview({
customer,
subscription: currentUserSubscription?.data?.[0]?.id,
- subscription_proration_behavior: 'create_prorations',
- subscription_billing_cycle_anchor: 'now',
- subscription_items: [
- {
- id: currentUserSubscription?.data?.[0]?.items?.data?.[0]?.id,
- price: findPrice?.id!,
- quantity: 1,
- },
- ],
- subscription_proration_date: proration_date,
+ subscription_details: {
+ proration_behavior: 'create_prorations',
+ billing_cycle_anchor: 'now',
+ items: [
+ {
+ id: currentUserSubscription?.data?.[0]?.items?.data?.[0]?.id,
+ price: findPrice?.id!,
+ quantity: 1,
+ },
+ ],
+ proration_date: proration_date,
+ },
});
return {
@@ -312,21 +307,49 @@ export class StripeService {
await stripe.subscriptions.list({
customer,
status: 'all',
+ expand: ['data.latest_invoice'],
})
).data.filter((f) => f.status !== 'canceled'),
};
- const { cancel_at } = await stripe.subscriptions.update(
- currentUserSubscription.data[0].id,
- {
- cancel_at_period_end:
- !currentUserSubscription.data[0].cancel_at_period_end,
- metadata: {
- service: 'gitroom',
- id,
- },
- }
- );
+ const sub = currentUserSubscription.data[0];
+
+ // If the user is toggling back (un-cancelling), just remove the cancel
+ if (sub.cancel_at_period_end) {
+ const { cancel_at } = await stripe.subscriptions.update(sub.id, {
+ cancel_at_period_end: false,
+ metadata: { service: 'gitroom', id },
+ });
+
+ return {
+ id,
+ cancel_at: cancel_at ? new Date(cancel_at * 1000) : undefined,
+ };
+ }
+
+ // Check if the latest invoice has a failed payment
+ const latestInvoice = sub.latest_invoice as Stripe.Invoice | null;
+ const hasFailedPayment =
+ sub.status === 'past_due' ||
+ latestInvoice?.status === 'open' ||
+ latestInvoice?.status === 'uncollectible';
+
+ if (hasFailedPayment) {
+ // Payment already failed β€” cancel immediately and delete subscription
+ await stripe.subscriptions.cancel(sub.id);
+ await this._subscriptionService.deleteSubscription(customer);
+
+ return {
+ id,
+ cancel_at: new Date(),
+ };
+ }
+
+ // Payment succeeded β€” cancel at end of billing period
+ const { cancel_at } = await stripe.subscriptions.update(sub.id, {
+ cancel_at_period_end: true,
+ metadata: { service: 'gitroom', id },
+ });
return {
id,
@@ -361,11 +384,16 @@ export class StripeService {
const now = Math.floor(Date.now() / 1000);
for (const promoCode of promotionCodes.data) {
+ const coupon =
+ typeof promoCode.promotion.coupon === 'string'
+ ? null
+ : promoCode.promotion.coupon;
+
// Check if it has autoapply metadata set to true (check both promo and coupon metadata)
const autoApply = Object.assign(
{},
promoCode.metadata,
- promoCode.coupon.metadata
+ coupon?.metadata
)?.autoapply;
if (autoApply !== 'true') continue;
@@ -373,8 +401,7 @@ export class StripeService {
if (promoCode.expires_at && promoCode.expires_at < now) continue;
// Check if the coupon has expired (redeem_by)
- if (promoCode.coupon.redeem_by && promoCode.coupon.redeem_by < now)
- continue;
+ if (coupon?.redeem_by && coupon.redeem_by < now) continue;
// Check if max redemptions reached
if (
@@ -403,15 +430,10 @@ export class StripeService {
userId: string,
allowTrial: boolean
) {
- const stripeCustom = new Stripe(process.env.STRIPE_SECRET_KEY!, {
- // @ts-ignore
- apiVersion: '2025-03-31.basil',
- });
-
const user = await this._userService.getUserById(userId);
try {
- await stripeCustom.customers.update(customer, {
+ await stripe.customers.update(customer, {
email: user.email,
...(body.dub
? {
@@ -431,9 +453,7 @@ export class StripeService {
}
const isUtm = body.utm ? `&utm_source=${body.utm}` : '';
- // @ts-ignore
- const { client_secret } = await stripeCustom.checkout.sessions.create({
- // @ts-ignore
+ const { client_secret } = await stripe.checkout.sessions.create({
ui_mode: 'custom',
customer,
return_url:
@@ -805,8 +825,13 @@ export class StripeService {
async paymentSucceeded(event: Stripe.InvoicePaymentSucceededEvent) {
// get subscription from payment
+ const subscriptionId =
+ event.data.object.parent?.subscription_details?.subscription;
+ if (!subscriptionId) {
+ return { ok: true };
+ }
const subscription = await stripe.subscriptions.retrieve(
- event.data.object.subscription as string
+ typeof subscriptionId === 'string' ? subscriptionId : subscriptionId.id
);
const { userId, ud } = subscription.metadata;
diff --git a/package.json b/package.json
index 787c795c9..4877bc528 100644
--- a/package.json
+++ b/package.json
@@ -124,7 +124,6 @@
"@types/react-dropzone": "^4.2.2",
"@types/remove-markdown": "^0.3.4",
"@types/sha256": "^0.2.2",
- "@types/stripe": "^8.0.417",
"@types/striptags": "^0.0.5",
"@types/yup": "^0.32.0",
"@uidotdev/usehooks": "^2.4.1",
@@ -225,7 +224,7 @@
"sharp": "^0.33.4",
"simple-statistics": "^7.8.3",
"slugify": "^1.6.6",
- "stripe": "^15.5.0",
+ "stripe": "^20.4.0",
"striptags": "^3.2.0",
"subtitle": "4.2.2-alpha.0",
"sweetalert2": "11.4.8",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index e19f84f87..dd2638cbd 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -14,7 +14,7 @@ importers:
dependencies:
'@ag-ui/mastra':
specifier: 0.2.0
'@ai-sdk/openai':
specifier: ^2.0.52
version: 2.0.95([email protected])
@@ -41,7 +41,7 @@ importers:
'@copilotkit/runtime':
specifier: 1.10.6
- version: 1.10.6(9413ba786a25fdda1f6d87c0f5568c1b)
+ version: 1.10.6(a81f1decc55e512f3b92296d45b735ac)
'@dub/analytics':
specifier: ^0.0.32
version: 0.0.32
@@ -258,9 +258,6 @@ importers:
'@types/sha256':
specifier: ^0.2.2
version: 0.2.2
- '@types/stripe':
- specifier: ^8.0.417
- version: 8.0.417
'@types/striptags':
specifier: ^0.0.5
version: 0.0.5
@@ -562,8 +559,8 @@ importers:
specifier: ^1.6.6
version: 1.6.6
stripe:
- specifier: ^15.5.0
- version: 15.12.0
+ specifier: ^20.4.0
+ version: 20.4.0(@types/[email protected])
striptags:
specifier: ^3.2.0
version: 3.2.0
@@ -7831,10 +7828,6 @@ packages:
resolution: {integrity: sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==}
- '@types/[email protected]':
- resolution: {integrity: sha512-PTuqskh9YKNENnOHGVJBm4sM0zE8B1jZw1JIskuGAPkMB+OH236QeN8scclhYGPA4nG6zTtPXgwpXdp+HPDTVw==}
- deprecated: This is a stub types definition. stripe provides its own type definitions, so you do not need this installed.
-
resolution: {integrity: sha512-mBUyVLvQJcRlqbCsO6ma/YblmFGSo8Jo8T0+ZzBate9Y8gTtLHemeiebzAFSBfF97EsycYQnXHCDkNeduiKPyA==}
@@ -15774,9 +15767,14 @@ packages:
resolution: {integrity: sha512-A21Xsm1XzUkK0qK1ZrytDUvqsQWict2Cykhvi0fBQntGG5JSprESasEyV1EZ/4CiR5WB5KjzLTrP/bO37B0wPg==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
- resolution: {integrity: sha512-slTbYS1WhRJXVB8YXU8fgHizkUrM9KJyrw4Dd8pLEwzKHYyQTIE46EePC2MVbSDZdE24o1GdNtzmJV4PrPpmJA==}
- engines: {node: '>=12.*'}
+ resolution: {integrity: sha512-F/aN1IQ9vHmlyLNi3DkiIbyzQb6gyBG0uYFd/VrEVQSc9BLtlgknPUx0EvzZdBMRLFuRaPFIFd7Mxwtg7Pbwzw==}
+ engines: {node: '>=16'}
+ peerDependencies:
+ '@types/node': '>=16'
+ peerDependenciesMeta:
+ '@types/node':
+ optional: true
resolution: {integrity: sha512-g45ZOGzHDMe2bdYMdIvdAfCQkCTDMGBazSw1ypMowwGIee7ZQ5dU0rBJ8Jqgl+jAKIv4dbeE1jscZq9wid1Tkw==}
@@ -17320,10 +17318,10 @@ snapshots:
'@ag-ui/core': 0.0.46
'@ag-ui/proto': 0.0.46
dependencies:
'@ag-ui/client': 0.0.46
- '@ag-ui/core': 0.0.37
+ '@ag-ui/core': 0.0.46
'@langchain/core': 0.3.80(@opentelemetry/[email protected])(@opentelemetry/[email protected](@opentelemetry/[email protected]))(@opentelemetry/[email protected](@opentelemetry/[email protected]))([email protected]([email protected]([email protected])([email protected]))([email protected]))
partial-json: 0.1.7
@@ -17336,12 +17334,12 @@ snapshots:
- react
- react-dom
dependencies:
'@ag-ui/client': 0.0.46
- '@ag-ui/core': 0.0.37
+ '@ag-ui/core': 0.0.46
'@ai-sdk/ui-utils': 1.2.11([email protected])
- '@copilotkit/runtime': 1.10.6(9413ba786a25fdda1f6d87c0f5568c1b)
+ '@copilotkit/runtime': 1.10.6(a81f1decc55e512f3b92296d45b735ac)
rxjs: 7.8.1
@@ -19236,12 +19234,12 @@ snapshots:
- encoding
- graphql
- '@copilotkit/[email protected](9413ba786a25fdda1f6d87c0f5568c1b)':
+ '@copilotkit/[email protected](a81f1decc55e512f3b92296d45b735ac)':
dependencies:
'@ag-ui/client': 0.0.46
- '@ag-ui/core': 0.0.37
+ '@ag-ui/core': 0.0.46
'@ag-ui/encoder': 0.0.46
'@ag-ui/proto': 0.0.46
'@anthropic-ai/sdk': 0.57.0
'@copilotkit/shared': 1.10.6
@@ -26437,10 +26435,6 @@ snapshots:
'@types/[email protected]': {}
- '@types/[email protected]':
- dependencies:
- stripe: 15.12.0
-
'@types/[email protected]': {}
@@ -36939,10 +36933,9 @@ snapshots:
- dependencies:
+ optionalDependencies:
'@types/node': 18.16.9
- qs: 6.15.0