Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented feature to create plan with flutterwave #717

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 150 additions & 172 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
},
"dependencies": {
"@css-inline/css-inline": "^0.14.1",
"@css-inline/css-inline-linux-x64-gnu": "^0.14.1",
"@faker-js/faker": "^8.4.1",
"@nestjs-modules/mailer": "^2.0.2",
"@nestjs/axios": "^3.0.2",
Expand Down Expand Up @@ -84,13 +83,15 @@
"@nestjs/schematics": "^10.1.3",
"@nestjs/testing": "^10.3.10",
"@types/bcrypt": "^5.0.2",
"@types/estree": "^1.0.5",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/jest": "^29.5.12",
"@types/node": "^20.3.1",
"@types/passport-google-oauth2": "^0.1.8",
"@types/passport-jwt": "^4.0.1",
"@types/speakeasy": "^2.0.10",
"@types/supertest": "^6.0.0",
"@types/yargs": "^17.0.33",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
Expand Down
26 changes: 26 additions & 0 deletions src/modules/flutterwave/dto/payment-plan.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { IsArray, IsNotEmpty, IsNumber, IsString } from 'class-validator';

export class CreatePaymentPlanDto {
@IsString()
@IsNotEmpty()
name: string;

@IsString()
@IsNotEmpty()
description: string;

@IsNumber()
@IsNotEmpty()
amount: number;

@IsNumber()
@IsNotEmpty()
duration: number;

@IsString()
@IsNotEmpty()
interval: string;

@IsString()
features: string;
}
23 changes: 23 additions & 0 deletions src/modules/flutterwave/entities/payment-plan.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { AbstractBaseEntity } from '../../../entities/base.entity';
import { Column, Entity } from 'typeorm';

@Entity()
export class PaymentPlan extends AbstractBaseEntity {
@Column({ nullable: false })
name: string;

@Column({ nullable: false })
description: string;

@Column({ nullable: false })
amount: number;

@Column({ nullable: false })
duration: number;

@Column({ nullable: false })
interval: string;

@Column({ nullable: true })
features: string;
}
1 change: 1 addition & 0 deletions src/modules/flutterwave/flutterwave.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Controller, Get, Post, Body, Patch, Param, Delete, Req } from '@nestjs/
import { FlutterwaveService } from './flutterwave.service';
import { CreateFlutterwavePaymentDto } from './dto/create-flutterwave-payment.dto';
import { UserPayload } from '../user/interfaces/user-payload.interface';
import { skipAuth } from 'src/helpers/skipAuth';

@Controller('payments/flutterwave')
export class FlutterwaveController {
Expand Down
9 changes: 6 additions & 3 deletions src/modules/flutterwave/flutterwave.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { FlutterwaveController } from './flutterwave.controller';
import { HttpModule } from '@nestjs/axios';
import { Payment } from './entities/payment.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PaymentPlanController } from './payment-plan.controller';
import { PaymentPlanService } from './payment-plan.service';
import { PaymentPlan } from './entities/payment-plan.entity';

@Module({
imports: [HttpModule, TypeOrmModule.forFeature([Payment])],
controllers: [FlutterwaveController],
providers: [FlutterwaveService],
imports: [HttpModule, TypeOrmModule.forFeature([Payment, PaymentPlan])],
controllers: [FlutterwaveController, PaymentPlanController],
providers: [FlutterwaveService, PaymentPlanService],
})
export class FlutterwaveModule {}
13 changes: 13 additions & 0 deletions src/modules/flutterwave/payment-plan.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Body, Controller, Post } from '@nestjs/common';
import { PaymentPlanService } from './payment-plan.service';
import { CreatePaymentPlanDto } from './dto/payment-plan.dto';

@Controller('payment/flutterwave/plan')
export class PaymentPlanController {
constructor(private readonly paymentPlanService: PaymentPlanService) {}

@Post()
async create(@Body() createPaymentPlanDto: CreatePaymentPlanDto) {
return this.paymentPlanService.create(createPaymentPlanDto);
}
}
102 changes: 102 additions & 0 deletions src/modules/flutterwave/payment-plan.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PaymentPlanService } from './payment-plan.service';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { Repository } from 'typeorm';
import { PaymentPlan } from './entities/payment-plan.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { of } from 'rxjs';
import { AxiosResponse } from 'axios';
import { CreatePaymentPlanDto } from './dto/payment-plan.dto';

describe('PaymentPlanService', () => {
let service: PaymentPlanService;
let httpService: HttpService;
let paymentPlanRepo: Repository<PaymentPlan>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PaymentPlanService,
{
provide: HttpService,
useValue: {
post: jest.fn(),
},
},
{
provide: ConfigService,
useValue: {
get: jest.fn().mockImplementation((key: string) => {
if (key === 'FLUTTERWAVE_SECRET_KEY') return 'mock-secret-key';
if (key === 'FLUTTERWAVE_BASE_URL') return 'mock-base-url';
}),
},
},
{
provide: getRepositoryToken(PaymentPlan),
useClass: Repository,
},
],
}).compile();

service = module.get<PaymentPlanService>(PaymentPlanService);
httpService = module.get<HttpService>(HttpService);
paymentPlanRepo = module.get<Repository<PaymentPlan>>(getRepositoryToken(PaymentPlan));
});

it('should create a payment plan successfully', async () => {
const paymentPlanResponse: AxiosResponse = {
data: {
data: {
id: 'plan-id',
name: 'Basic Plan',
amount: 10000,
duration: 12,
interval: 'monthly',
},
},
status: 200,
statusText: 'OK',
headers: {},
config: {
headers: undefined,
},
};

jest.spyOn(httpService, 'post').mockReturnValue(of(paymentPlanResponse));
jest.spyOn(paymentPlanRepo, 'create').mockImplementation(dto => dto as PaymentPlan);
jest.spyOn(paymentPlanRepo, 'save').mockResolvedValue({} as PaymentPlan);

const createPaymentPlanDto: CreatePaymentPlanDto = {
name: 'Basic Plan',
amount: 10000,
duration: 12,
interval: 'monthly',
description: 'This is a basic plan',
features: 'Default plan',
};

const paymentPlan = {
name: 'Basic Plan',
amount: 10000,
duration: 12,
interval: 'monthly',
};

const result = await service.create(createPaymentPlanDto);

expect(result).toEqual({
status: 200,
message: 'Payment plan created successfully',
data: {
paymentPlan: paymentPlanResponse.data.data,
},
});
expect(httpService.post).toHaveBeenCalledWith('mock-base-url/payment-plans', paymentPlan, {
headers: service['headers'],
});
expect(paymentPlanRepo.create).toHaveBeenCalledWith(createPaymentPlanDto);
expect(paymentPlanRepo.save).toHaveBeenCalledWith(expect.any(Object));
});
});
49 changes: 49 additions & 0 deletions src/modules/flutterwave/payment-plan.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { PaymentPlan } from './entities/payment-plan.entity';
import { Repository } from 'typeorm';
import { CreatePaymentPlanDto } from './dto/payment-plan.dto';
import { ConfigService } from '@nestjs/config';
import { HttpService } from '@nestjs/axios';

@Injectable()
export class PaymentPlanService {
private readonly secretkey: string;
private readonly baseUrl: string;
private readonly headers: {};
constructor(
@InjectRepository(PaymentPlan)
private readonly paymentPlanRepo: Repository<PaymentPlan>,
private readonly httpService: HttpService,
private readonly configService: ConfigService
) {
this.secretkey = configService.get<string>('FLUTTERWAVE_SECRET_KEY');
this.baseUrl = configService.get<string>('FLUTTERWAVE_BASE_URL');
this.headers = {
Authorization: `Bearer ${this.secretkey}`,
'Content-Type': 'application/json',
};
}

async create(createPaymentPlanDto: CreatePaymentPlanDto) {
const { name, amount, duration, interval } = createPaymentPlanDto;
const paymentPlan = {
name,
amount,
duration,
interval,
};
const response = await this.httpService
.post(`${this.baseUrl}/payment-plans`, paymentPlan, { headers: this.headers })
.toPromise();
const payment_plan = await this.paymentPlanRepo.create(createPaymentPlanDto);
await this.paymentPlanRepo.save(payment_plan);
return {
status: 200,
message: 'Payment plan created successfully',
data: {
paymentPlan: response.data.data,
},
};
}
}
Loading