|
|
|
@ -8,13 +8,19 @@ import { AddToCartDto } from "./dto/cart/add-to-cart.dto"; |
|
|
|
|
import { JwtAuthGuard } from "src/guard/auth.guard"; |
|
|
|
|
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 { Transaction } from "./entities/transaction.entity"; |
|
|
|
|
import { Payment } from "./entities/payment.entity"; |
|
|
|
|
|
|
|
|
|
@Controller("shop") |
|
|
|
|
export class ProductsController { |
|
|
|
|
constructor( |
|
|
|
|
private readonly productsService: ProductsService, |
|
|
|
|
private paymentService: PaymentService, |
|
|
|
|
|
|
|
|
|
private readonly invoiceService: InvoiceService, |
|
|
|
|
@InjectModel(Transaction) private readonly transactionModel: typeof Transaction, |
|
|
|
|
@InjectModel(Payment) private readonly paymentModel: typeof Payment, |
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
////////////////////////////////////////products////////////////////////////////////////
|
|
|
|
@ -111,7 +117,7 @@ export class ProductsController { |
|
|
|
|
////////////////////////////////////////wallet////////////////////////////////////////
|
|
|
|
|
//getting wallet balance (user)
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get('wallet') |
|
|
|
|
@Get("wallet") |
|
|
|
|
async getBalance(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
return this.productsService.getBalance(userId); |
|
|
|
@ -138,6 +144,59 @@ export class ProductsController { |
|
|
|
|
async getTransactionByIdForAdmin(@Param("id") id: number) { |
|
|
|
|
return this.productsService.getTransactionByIdForAdmin(id); |
|
|
|
|
} |
|
|
|
|
////////////////////////////////////////payment////////////////////////////////////////
|
|
|
|
|
//payment request
|
|
|
|
|
@UseGuards(JwtAuthGuard) |
|
|
|
|
@Get("payment/request") |
|
|
|
|
async requestPayment(@Request() req) { |
|
|
|
|
const userId = req.user.id; |
|
|
|
|
const invoice = await this.invoiceService.getInvoicePendingByUser(userId); |
|
|
|
|
const totalAmount = invoice.totalPaymentAmount; |
|
|
|
|
if (totalAmount < 1000) { |
|
|
|
|
return { message: "please enter amount above 1000" }; |
|
|
|
|
} |
|
|
|
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}&amount=${totalAmount}`; |
|
|
|
|
const paymentUrl = await this.paymentService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
|
|
|
|
|
|
|
|
|
return { url: paymentUrl }; |
|
|
|
|
} |
|
|
|
|
//payment verify
|
|
|
|
|
@Get("verify") |
|
|
|
|
async verifyPayment(@Query() query: { Authority: string; Status: string; userId: number; amount: number }): Promise<any> { |
|
|
|
|
const { Authority, Status, userId, amount } = query; |
|
|
|
|
|
|
|
|
|
if (Status !== "OK") { |
|
|
|
|
throw new Error("Payment failed"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!userId) { |
|
|
|
|
throw new Error("User ID is required."); |
|
|
|
|
} |
|
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
|
try { |
|
|
|
|
const refId = await this.paymentService.verifyPayment(Authority, amount); |
|
|
|
|
await this.productsService.addBalance(userId, amount); |
|
|
|
|
const wallet = this.productsService.getWalletInfo(userId); |
|
|
|
|
await this.paymentModel.create({ |
|
|
|
|
userId, |
|
|
|
|
walletId: (await wallet).walletId, |
|
|
|
|
paymentAmount: amount, |
|
|
|
|
status: "completed", |
|
|
|
|
}); |
|
|
|
|
await this.transactionModel.create({ |
|
|
|
|
walletId: (await wallet).walletId, |
|
|
|
|
amount: String(amount).startsWith("+") ? String(amount) : `+${amount}`, |
|
|
|
|
}); |
|
|
|
|
return { message: "Payment successful", refId }; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.log(error); |
|
|
|
|
await this.paymentModel.create({ |
|
|
|
|
userId, |
|
|
|
|
walletId: (await wallet).walletId, |
|
|
|
|
paymentAmount: amount, |
|
|
|
|
status: "failed", |
|
|
|
|
}); |
|
|
|
|
throw new Error(`Error during payment verification: ${error.message}`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|