Skip to content

Commit

Permalink
payment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Aug 22, 2024
1 parent ad58c4f commit cca915e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 41 deletions.
5 changes: 2 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ function loadConfig() {
username: process.env.DB_USER ?? 'postgres',
password: process.env.DB_PASS ?? 'postgres',
database: process.env.DB_NAME ?? 'postgres',
schema: "public",
synchronize: NODE_ENV === 'development' ? false : false, // true shouldn't be used in production - otherwise you can lose production data.
logging: true,
dropSchema: false, // Don't use this in production - Drops the schema each time data source is being initialized.
dropSchema: false, // Don't use this in production - Drops the schema each time data source is being initialized.
migrationsRun: true, // if migrations should be auto run on every application launch. As an alternative, you can use CLI and run migration:run command.
migrations: [`dist/db/migrations/*.js`], //list of migrations that need to be loaded by TypeORM
migrations: [`dist/db/migrations/*.js`], // list of migrations that need to be loaded by TypeORM
},
db2: {
name: 'flaskPostgres',
Expand Down
71 changes: 49 additions & 22 deletions src/features/payment/payment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import {
Get,
Param,
Post,
Query,
Req,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiHeader, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { PaymentService } from './payment.service';
import { isAuthenticated } from 'src/utils/auth';
import { FlaskUserTypesEnum } from 'src/types/interfaces/interface';
import config from 'src/config';
import axios from 'axios';
import { ValidatePaymentPipe } from './pipes/validate-campaign.pipe';
import {
CreateFlaskPaymentDto,
VerifyFlaskPaymentDto,
} from 'src/types/dtos/CreatePayment.dto';
import { ServerError } from 'src/filters/server-exception.filter';

@ApiTags('Payments')
@ApiSecurity('flask-access-token')
Expand All @@ -21,22 +31,16 @@ import axios from 'axios';
description: 'to use cache and flask authentication',
required: true,
})
@UsePipes(new ValidationPipe())
@Controller('payment')
export class PaymentController {
constructor(private paymentService: PaymentService) {}

@Post(`new`)
@ApiOperation({ description: 'Get all needs payments' })
async newPayments(
async newPayment(
@Req() req: Request,
@Body()
body: {
needId: number;
gateway: number;
amount: number;
donate: number;
useCredit: boolean;
},
@Body(ValidatePaymentPipe) body: CreateFlaskPaymentDto,
) {
const dappFlaskUserId = Number(req.headers['dappFlaskUserId']);

Expand All @@ -45,26 +49,49 @@ export class PaymentController {
}

const token =
config().dataCache.fetchPanelAuthentication(dappFlaskUserId).token;
config().dataCache.fetchDappAuthentication(dappFlaskUserId).token;
const configs = {
headers: {
'Content-Type': 'application/json',
Authorization: token,
},
};
try {
// create flask payment
const { data } = await axios.post(
'https://api.sayapp.company/api/v2/payment',
{
need_id: Number(body.needId),
amount: Number(body.amount),
donate: Number(body.donation),
use_credit: Boolean(body.useCredit),
gateWay: Number(body.gateWay),
},
configs,
);
console.log(data);
return data;
} catch (e) {
console.log(e);
}
}

// create flask child
// const { data } = await axios.post(
// 'https://api.sayapp.company/api/v2/child/add/',
// {
// needId: body.amount,
// amount: body.amount,
// donate: body.donate,
// useCredit: body.useCredit,
// },
// configs,
// );
console.log(body);
@Get(`verify`)
@ApiOperation({ description: 'Zibal calls the callback' })
async verifyPayment(
@Req() req: Request,
// callback: https://yourcallbackurl.com/callback?trackId=9900&success=1&status=2&orderId=1
@Query('trackId') trackId: string,
@Query('orderId') orderId: string,
) {
try {
const { data } = await axios.get(
`https://api.sayapp.company/api/v2/payment/verify?trackId=${trackId}&orderId=${orderId}`,
);
console.log(data);
} catch (e) {
console.log(e);
}
}

@Get(`all`)
Expand Down
12 changes: 10 additions & 2 deletions src/features/payment/payment.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import {
MiddlewareConsumer,
Module,
NestModule,
RequestMethod,
} from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AllUserEntity } from '../../entities/user.entity';
import { NeedEntity } from '../../entities/need.entity';
Expand Down Expand Up @@ -55,6 +60,9 @@ import { NeedReceipt } from 'src/entities/flaskEntities/needReceipt.entity';
})
export class PaymentModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(PaymentMiddleware).forRoutes('payment');
consumer
.apply(PaymentMiddleware)
.exclude({ path: 'payment/verify', method: RequestMethod.GET })
.forRoutes(PaymentController);
}
}
10 changes: 10 additions & 0 deletions src/features/payment/pipes/validate-campaign.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';

@Injectable()
export class ValidatePaymentPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata) {
console.log('Validating Payment...');

return value;
}
}
54 changes: 40 additions & 14 deletions src/types/dtos/CreatePayment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import { IsNotEmpty } from 'class-validator';

export class CreatePaymentDto {
id?: number;
verified?: Date;
id_need: number
id_user: number
order_id: string
credit_amount: number
donation_amount: number
card_no: string
gateway_payment_id: string
gateway_track_id: string
need_amount: number
transaction_date: Date
created: Date
updated: Date
id?: number;
verified?: Date;
id_need: number;
id_user: number;
order_id: string;
credit_amount: number;
donation_amount: number;
card_no: string;
gateway_payment_id: string;
gateway_track_id: string;
need_amount: number;
transaction_date: Date;
created: Date;
updated: Date;
}

export class CreateFlaskPaymentDto {
@IsNotEmpty()
needId: number;
@IsNotEmpty()
gateWay: number;
@IsNotEmpty()
amount: number;
@IsNotEmpty()
donation: number;
@IsNotEmpty()
useCredit: boolean;
}

export class VerifyFlaskPaymentDto {
// idpay
@IsNotEmpty()
id: number;
@IsNotEmpty()
order_id: number;
// zipal
@IsNotEmpty()
trackId: number;
@IsNotEmpty()
orderId: number;
}

0 comments on commit cca915e

Please sign in to comment.