AnhLedger commited on
Commit
d18f023
·
1 Parent(s): 89ad83e

add payment dto

Browse files
backend/src/payment/dto/create-payment.dto.ts CHANGED
@@ -1 +1,19 @@
1
- export class CreatePaymentDto {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IsNumber, IsOptional, IsString } from "class-validator"
2
+
3
+ export class CreatePaymentDto {
4
+ @IsNumber()
5
+ amount: number
6
+
7
+ @IsOptional()
8
+ @IsString()
9
+ bankCode: string
10
+
11
+ @IsString()
12
+ orderDescription: string
13
+
14
+ @IsString()
15
+ orderType: string
16
+
17
+ @IsString()
18
+ language: string
19
+ }
backend/src/payment/payment.controller.ts CHANGED
@@ -3,6 +3,7 @@ import { Controller, Post, Body, Req, Res } from '@nestjs/common';
3
  import { PaymentService } from './payment.service.js';
4
  import { Request, Response } from 'express';
5
  import { Public } from '../modules/authentication/authentication.decorator.js';
 
6
 
7
  @Controller('payment')
8
  export class PaymentController {
@@ -10,13 +11,13 @@ export class PaymentController {
10
 
11
  @Public()
12
  @Post('create_payment_url')
13
- async createPaymentUrl(@Req() req: Request, @Body() body: any) {
14
- console.log("hello")
15
  const ipAddr =
16
  req.headers['x-forwarded-for'] ||
17
  req.socket.remoteAddress ||
18
  req.socket?.remoteAddress;
19
- console.log(ipAddr);
20
  return await this.paymentService.createPaymentUrl(
21
  body.amount,
22
  body.bankCode,
 
3
  import { PaymentService } from './payment.service.js';
4
  import { Request, Response } from 'express';
5
  import { Public } from '../modules/authentication/authentication.decorator.js';
6
+ import { CreatePaymentDto } from './dto/create-payment.dto.js';
7
 
8
  @Controller('payment')
9
  export class PaymentController {
 
11
 
12
  @Public()
13
  @Post('create_payment_url')
14
+ async createPaymentUrl(@Req() req: Request, @Body() body: CreatePaymentDto) {
15
+
16
  const ipAddr =
17
  req.headers['x-forwarded-for'] ||
18
  req.socket.remoteAddress ||
19
  req.socket?.remoteAddress;
20
+
21
  return await this.paymentService.createPaymentUrl(
22
  body.amount,
23
  body.bankCode,
backend/src/payment/payment.service.ts CHANGED
@@ -9,18 +9,18 @@ export class PaymentService {
9
  constructor(private readonly configService: ConfigService) {}
10
 
11
  async createPaymentUrl(amount: number, bankCode: string, orderDescription: string, orderType: string, language: string, ipAddr: string) {
12
- console.log("hi")
13
  const tmnCode = this.configService.get<string>('vnp_TmnCode');
14
  const secretKey = this.configService.get<string>('vnp_HashSecret');
15
  const vnpUrl = this.configService.get<string>('vnp_Url');
16
  const returnUrl = this.configService.get<string>('vnp_ReturnUrl');
17
- console.log("1")
18
  const date = new Date();
19
  const createDate = this.formatDate(date, 'yyyymmddHHmmss');
20
- const orderId = "5638"
21
  const locale = language || 'vn';
22
  const currCode = 'VND';
23
- console.log("2")
24
  const vnp_Params: Record<string, string> = {
25
  vnp_Version: '2.1.0',
26
  vnp_Command: 'pay',
@@ -40,24 +40,17 @@ export class PaymentService {
40
  vnp_Params['vnp_BankCode'] = bankCode;
41
  }
42
 
43
- // Sort the parameters
44
- // const sortedParams = Object.keys(vnp_Params)
45
- // .sort()
46
- // .reduce((acc, key) => {
47
- // acc[key] = vnp_Params[key];
48
- // return acc;
49
- // }, {} as Record<string, string>);
50
  const sortedParams = this.sortObject(vnp_Params);
51
- console.log("4")
52
  // Sign the data
53
  const signData = querystring.stringify(sortedParams, { encode: false });
54
  const hmac = crypto.createHmac('sha512', secretKey);
55
  const signed = hmac.update(Buffer.from(signData, 'utf-8')).digest('hex');
56
  sortedParams['vnp_SecureHash'] = signed;
57
- console.log("5")
58
  // Create the URL
59
  const res = `${vnpUrl}?${querystring.stringify(sortedParams, { encode: false })}`;
60
- console.log("6");
61
  return res;
62
  }
63
  // Format date helper function
 
9
  constructor(private readonly configService: ConfigService) {}
10
 
11
  async createPaymentUrl(amount: number, bankCode: string, orderDescription: string, orderType: string, language: string, ipAddr: string) {
12
+
13
  const tmnCode = this.configService.get<string>('vnp_TmnCode');
14
  const secretKey = this.configService.get<string>('vnp_HashSecret');
15
  const vnpUrl = this.configService.get<string>('vnp_Url');
16
  const returnUrl = this.configService.get<string>('vnp_ReturnUrl');
17
+
18
  const date = new Date();
19
  const createDate = this.formatDate(date, 'yyyymmddHHmmss');
20
+ const orderId = Date.now().toString();
21
  const locale = language || 'vn';
22
  const currCode = 'VND';
23
+
24
  const vnp_Params: Record<string, string> = {
25
  vnp_Version: '2.1.0',
26
  vnp_Command: 'pay',
 
40
  vnp_Params['vnp_BankCode'] = bankCode;
41
  }
42
 
 
 
 
 
 
 
 
43
  const sortedParams = this.sortObject(vnp_Params);
44
+
45
  // Sign the data
46
  const signData = querystring.stringify(sortedParams, { encode: false });
47
  const hmac = crypto.createHmac('sha512', secretKey);
48
  const signed = hmac.update(Buffer.from(signData, 'utf-8')).digest('hex');
49
  sortedParams['vnp_SecureHash'] = signed;
50
+
51
  // Create the URL
52
  const res = `${vnpUrl}?${querystring.stringify(sortedParams, { encode: false })}`;
53
+
54
  return res;
55
  }
56
  // Format date helper function