|
|
@ -7,8 +7,6 @@ import { RoleGuard } from "src/guard/role.guard"; |
|
|
|
import { AddToCartDto } from "./dto/cart/add-to-cart.dto"; |
|
|
|
import { AddToCartDto } from "./dto/cart/add-to-cart.dto"; |
|
|
|
import { JwtAuthGuard } from "src/guard/auth.guard"; |
|
|
|
import { JwtAuthGuard } from "src/guard/auth.guard"; |
|
|
|
import { UpdateCartDto } from "./dto/cart/update-cart.dto"; |
|
|
|
import { UpdateCartDto } from "./dto/cart/update-cart.dto"; |
|
|
|
import { PaymentService } from "src/payment/payment.service"; |
|
|
|
|
|
|
|
import { InvoiceService } from "src/invoice/invoice.service"; |
|
|
|
|
|
|
|
import { InjectModel } from "@nestjs/sequelize"; |
|
|
|
import { InjectModel } from "@nestjs/sequelize"; |
|
|
|
import { Transaction } from "./entities/transaction.entity"; |
|
|
|
import { Transaction } from "./entities/transaction.entity"; |
|
|
|
import { Payment } from "./entities/payment.entity"; |
|
|
|
import { Payment } from "./entities/payment.entity"; |
|
|
@ -17,8 +15,6 @@ import { Payment } from "./entities/payment.entity"; |
|
|
|
export class ProductsController { |
|
|
|
export class ProductsController { |
|
|
|
constructor( |
|
|
|
constructor( |
|
|
|
private readonly productsService: ProductsService, |
|
|
|
private readonly productsService: ProductsService, |
|
|
|
private paymentService: PaymentService, |
|
|
|
|
|
|
|
private readonly invoiceService: InvoiceService, |
|
|
|
|
|
|
|
@InjectModel(Transaction) private readonly transactionModel: typeof Transaction, |
|
|
|
@InjectModel(Transaction) private readonly transactionModel: typeof Transaction, |
|
|
|
@InjectModel(Payment) private readonly paymentModel: typeof Payment, |
|
|
|
@InjectModel(Payment) private readonly paymentModel: typeof Payment, |
|
|
|
) {} |
|
|
|
) {} |
|
|
@ -128,7 +124,7 @@ export class ProductsController { |
|
|
|
async addBalance(@Body("amount") amount: number, @Request() req) { |
|
|
|
async addBalance(@Body("amount") amount: number, @Request() req) { |
|
|
|
const userId = req.user.id; |
|
|
|
const userId = req.user.id; |
|
|
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${amount}`; |
|
|
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${amount}`; |
|
|
|
const paymentUrl = this.paymentService.requestPayment(amount, "Wallet Charge", callbackUrl); |
|
|
|
const paymentUrl = this.productsService.requestPayment(amount, "Wallet Charge", callbackUrl); |
|
|
|
return paymentUrl; |
|
|
|
return paymentUrl; |
|
|
|
} |
|
|
|
} |
|
|
|
//get transaction (user)
|
|
|
|
//get transaction (user)
|
|
|
@ -150,13 +146,13 @@ export class ProductsController { |
|
|
|
@Get("payment/request") |
|
|
|
@Get("payment/request") |
|
|
|
async requestPayment(@Request() req) { |
|
|
|
async requestPayment(@Request() req) { |
|
|
|
const userId = req.user.id; |
|
|
|
const userId = req.user.id; |
|
|
|
const invoice = await this.invoiceService.getInvoicePendingByUser(userId); |
|
|
|
const invoice = await this.productsService.getInvoicePendingByUser(userId); |
|
|
|
const totalAmount = invoice.totalPaymentAmount; |
|
|
|
const totalAmount = invoice.totalPaymentAmount; |
|
|
|
if (totalAmount < 1000) { |
|
|
|
if (totalAmount < 1000) { |
|
|
|
return { message: "please enter amount above 1000" }; |
|
|
|
return { message: "please enter amount above 1000" }; |
|
|
|
} |
|
|
|
} |
|
|
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${totalAmount}`; |
|
|
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${totalAmount}`; |
|
|
|
const paymentUrl = await this.paymentService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
|
|
|
const paymentUrl = await this.productsService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
|
|
|
|
|
|
|
|
|
|
|
return { url: paymentUrl }; |
|
|
|
return { url: paymentUrl }; |
|
|
|
} |
|
|
|
} |
|
|
@ -174,7 +170,7 @@ export class ProductsController { |
|
|
|
} |
|
|
|
} |
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
try { |
|
|
|
try { |
|
|
|
const refId = await this.paymentService.verifyPayment(Authority, amount); |
|
|
|
const refId = await this.productsService.verifyPayment(Authority, amount); |
|
|
|
await this.productsService.addBalance(userId, amount); |
|
|
|
await this.productsService.addBalance(userId, amount); |
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
await this.paymentModel.create({ |
|
|
|
await this.paymentModel.create({ |
|
|
@ -199,4 +195,25 @@ export class ProductsController { |
|
|
|
throw new Error(`Error during payment verification: ${error.message}`); |
|
|
|
throw new Error(`Error during payment verification: ${error.message}`); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
////////////////////////////////////////invoice////////////////////////////////////////
|
|
|
|
|
|
|
|
//get invoice (user)
|
|
|
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
|
|
|
@Get('invoice') |
|
|
|
|
|
|
|
async getInvoiceByUser(@Request() req) { |
|
|
|
|
|
|
|
const userId = req.user.id; |
|
|
|
|
|
|
|
return this.productsService.getInvoiceByUser(userId); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//get invoices list (admin)
|
|
|
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
|
|
|
@Get('invoice/list') |
|
|
|
|
|
|
|
async getInvoices() { |
|
|
|
|
|
|
|
return this.productsService.getInvoices(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//get specific user invoices
|
|
|
|
|
|
|
|
@UseGuards(RoleGuard) |
|
|
|
|
|
|
|
@Get(':id') |
|
|
|
|
|
|
|
async getUserInvoice(@Param('id') id:number) { |
|
|
|
|
|
|
|
return this.productsService.getUserInvoices(id); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|