-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
106531c
commit 4353a86
Showing
12 changed files
with
414 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { StripeController } from './stripe.controller'; | ||
|
||
describe('StripeController', () => { | ||
let controller: StripeController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [StripeController], | ||
}).compile(); | ||
|
||
controller = module.get<StripeController>(StripeController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { StripeService } from './stripe.service'; | ||
import { Roles } from '../auth/roles.decorator'; | ||
import { Post, Param, Req } from '@nestjs/common'; | ||
|
||
@Controller('subscriptions') | ||
export class StripeController { | ||
constructor(private readonly stripeService: StripeService) {} | ||
|
||
@Roles('user') | ||
@Post('subscribe/:planId') | ||
async subscribeToPlan(@Param('planId') planId: string, @Req() req) { | ||
const userId = req.user.sub; | ||
return this.stripeService.createSubscription(userId, planId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { StripeController } from './stripe.controller'; | ||
import { StripeService } from './stripe.service'; | ||
|
||
@Module({}) | ||
export class StripeModule { | ||
static forRootAsync(): DynamicModule { | ||
return { | ||
module: StripeModule, | ||
controllers: [StripeController], | ||
imports: [ConfigModule.forRoot()], | ||
providers: [ | ||
StripeService, | ||
{ | ||
provide: 'STRIPE_API_KEY', | ||
useFactory: async (configService: ConfigService) => | ||
configService.get('STRIPE_API_KEY'), | ||
inject: [ConfigService], | ||
}, | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { StripeService } from './stripe.service'; | ||
|
||
describe('StripeService', () => { | ||
let service: StripeService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [StripeService], | ||
}).compile(); | ||
|
||
service = module.get<StripeService>(StripeService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import Stripe from 'stripe'; | ||
import { User } from 'src/user/user.schema'; | ||
import { InsurancePlan } from 'src/insurance/insurance.schema'; | ||
|
||
@Injectable() | ||
export class StripeService { | ||
private stripe: Stripe; | ||
|
||
constructor(@Inject('STRIPE_API_KEY') private readonly apiKey: string) { | ||
this.stripe = new Stripe(this.apiKey) | ||
} | ||
|
||
async createSubscription(userId: string, insuranceId: string) { | ||
const insurance = await InsurancePlan.findById(insuranceId); | ||
|
||
if (!insurance) { | ||
throw new Error('Insurance not found'); | ||
} | ||
|
||
const user = await User.findById(userId); | ||
|
||
// Create a customer if user doesn't have a Stripe ID | ||
if (!user.stripeCustomerId) { | ||
const customer = await this.stripe.customers.create({ email: user.email, }); | ||
user.stripeCustomerId = customer.id; | ||
} | ||
|
||
const subscription = await this.stripe.subscriptions.create({ | ||
customer: user.stripeCustomerId, | ||
items: [{ price_data: { unit_amount: insurance.monthlyPremium * 100, currency: 'usd', product: insurance.id, recurring: { interval: 'month' } } }], | ||
}); | ||
|
||
return subscription; | ||
} | ||
|
||
|
||
// async createSubscription(userId: string, planId: string) { | ||
// const customer = await this.getOrCreateStripeCustomer(userId); | ||
|
||
// const priceId = await this.getPriceIdFromPlan(planId); // Get the Stripe price_id for the plan | ||
|
||
// const subscription = await this.stripe.subscriptions.create({ | ||
// customer: customer.id, | ||
// items: [{ price: priceId }], | ||
// }); | ||
|
||
// return subscription; | ||
// } | ||
|
||
// // Helper function to get or create a Stripe customer for the user | ||
// async getOrCreateStripeCustomer(userId: string) { | ||
// // Query your database to see if the user already has a Stripe customerId | ||
// const user = await User.findById(userId); | ||
// if (user.stripeCustomerId !== null) { | ||
// return await this.stripe.customers.retrieve(user.stripeCustomerId); | ||
// } | ||
|
||
// const customer = await this.stripe.customers.create({ email: user.email }); | ||
|
||
// // Save the customerId in your database | ||
// await this.saveStripeCustomerId(userId, customer.id); | ||
|
||
// return customer; | ||
// } | ||
|
||
// // Helper function to get Stripe price_id for the plan | ||
// async getPriceIdFromPlan(planId: string) { | ||
// const plan = await InsurancePlan.findById(planId); | ||
// return plan.stripePriceId; // Assuming the Stripe priceId is saved with the insurance plan | ||
// } | ||
|
||
// // Helper function to save the Stripe customerId in your database | ||
// async saveStripeCustomerId(userId: string, stripeCustomerId: string) { | ||
// const user = await User.findByIdAndUpdate(userId, {stripeCustomerId: stripeCustomerId}, {new: true}); | ||
// return user; | ||
// } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.