Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

some stripe updates...

Blue Mousec0cd24812bdf
diff --git a/package.json b/package.json
index 02249941c..a9e521b15 100644
--- a/package.json
+++ b/package.json
@@ -30,6 +30,7 @@
"@nestjs/schedule": "^1.0.1",
"@nestjs/swagger": "^5.1.0",
"@nestjs/typeorm": "^8.0.2",
+ "axios": "^0.23.0",
"bcrypt": "^5.0.1",
"blake3": "^2.1.4",
"body-parser": "^1.19.0",
diff --git a/src/app.module.ts b/src/app.module.ts
index 5e708276c..1c4d1afcd 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -26,7 +26,7 @@ import { PingModule } from './ping/ping.module'
username: process.env.MYSQL_USER,
password: process.env.MYSQL_ROOT_PASSWORD,
database: process.env.MYSQL_DATABASE,
- synchronize: false,
+ synchronize: true,
entities: [User, ActionToken, Project],
}),
ScheduleModule.forRoot(),
diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.ts
index ac51d4011..06086c587 100644
--- a/src/user/entities/user.entity.ts
+++ b/src/user/entities/user.entity.ts
@@ -95,6 +95,9 @@ export class User {
@Column({ type: 'timestamp', nullable: true })
exportedAt: Date;
+ @Column('varchar', { length: 28, nullable: true })
+ stripeSubID: string
+
@BeforeUpdate()
updateTimestamp() {
this.updated = new Date;
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 8f4545ad4..38c314484 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -13,7 +13,7 @@ import * as _isEmpty from 'lodash/isEmpty'
import { UserService } from './user.service'
import { ProjectService } from '../project/project.service'
import {
- User, UserType, MAX_EMAIL_REQUESTS, ACCOUNT_PLANS,
+ User, UserType, MAX_EMAIL_REQUESTS, ACCOUNT_PLANS, PlanCode,
} from './entities/user.entity'
import { Roles } from '../common/decorators/roles.decorator'
import { Pagination } from '../common/pagination/pagination'
@@ -243,6 +243,46 @@ export class UserController {
const { planCode } = body
const priceId = ACCOUNT_PLANS[planCode]?.priceId
+ if (user.stripeSubID) {
+ if (planCode === PlanCode.free) {
+ await stripe.subscriptions.update(user.stripeSubID, {
+ cancel_at_period_end: true,
+ })
+
+ return {
+ message: 'The subscription has been canceled. No more charges will be made to your card.',
+ }
+ }
+
+ if (_isEmpty(priceId)) {
+ throw new BadRequestException('Incorrect planCode provided')
+ }
+
+ await stripe.subscriptions.update(user.stripeSubID, {
+ cancel_at_period_end: false,
+ proration_behavior: 'always_invoice',
+ items: [{
+ price: priceId,
+ quantity: 1,
+ }]
+ })
+
+ return {
+ message: 'The subscription has been upgraded.',
+ }
+ }
+
+ // If user has no Stripe Subscription ID and wants to downgrade to free tier
+ if (planCode === PlanCode.free) {
+ await this.userService.update(user.id, {
+ planCode: PlanCode.free,
+ })
+
+ return {
+ message: 'Your account have been downgraded to the free tier.',
+ }
+ }
+
if (_isEmpty(priceId)) {
throw new BadRequestException('Incorrect planCode provided')
}
@@ -250,6 +290,7 @@ export class UserController {
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
payment_method_types: ['card'],
+ customer: user.id,
customer_email: user.email,
line_items: [
{
diff --git a/src/webhook/webhook.controller.ts b/src/webhook/webhook.controller.ts
index 35f34946e..3ee2df6f6 100644
--- a/src/webhook/webhook.controller.ts
+++ b/src/webhook/webhook.controller.ts
@@ -51,7 +51,10 @@ export class WebhookController {
await stripe.subscriptions.update(subscription_id, {
default_payment_method: payment_intent.payment_method,
})
- await this.userService.update(uid, { planCode })
+ await this.userService.update(uid, {
+ planCode,
+ stripeSubID: subscription_id,
+ })
}
break
@@ -65,7 +68,10 @@ export class WebhookController {
case 'customer.subscription.deleted': {
const { uid } = dataObject.metadata
- await this.userService.update(uid, { planCode: PlanCode.free })
+ await this.userService.update(uid, {
+ planCode: PlanCode.free,
+ stripeSubID: null,
+ })
break
}
default: