parent
9e665f8710
commit
35521f8e0c
14 changed files with 334 additions and 106 deletions
@ -0,0 +1,53 @@ |
||||
'use strict'; |
||||
|
||||
module.exports = { |
||||
up: async (queryInterface, Sequelize) => { |
||||
await queryInterface.createTable('Invoices', { |
||||
id: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
autoIncrement: true, |
||||
primaryKey: true, |
||||
}, |
||||
userId: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
references: { |
||||
model: 'Users', |
||||
key: 'id',
|
||||
}, |
||||
onDelete: 'CASCADE',
|
||||
}, |
||||
totalAmount: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
}, |
||||
products: { |
||||
type: Sequelize.JSON,
|
||||
allowNull: false, |
||||
}, |
||||
paymentStatus: { |
||||
type: Sequelize.STRING, |
||||
allowNull: false, |
||||
}, |
||||
refId: { |
||||
type: Sequelize.STRING, |
||||
allowNull: false, |
||||
}, |
||||
createdAt: { |
||||
type: Sequelize.DATE, |
||||
allowNull: false, |
||||
defaultValue: Sequelize.NOW, |
||||
}, |
||||
updatedAt: { |
||||
type: Sequelize.DATE, |
||||
allowNull: false, |
||||
defaultValue: Sequelize.NOW, |
||||
}, |
||||
}); |
||||
}, |
||||
|
||||
down: async (queryInterface, Sequelize) => { |
||||
await queryInterface.dropTable('Invoices'); |
||||
}, |
||||
}; |
@ -1,12 +1,14 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { InvoiceService } from './invoice.service'; |
||||
import { InvoiceController } from './invoice.controller'; |
||||
import { SequelizeModule } from '@nestjs/sequelize'; |
||||
import { Invoice } from './entities/invoice.entity'; |
||||
import { Module, forwardRef } from "@nestjs/common"; |
||||
import { SequelizeModule } from "@nestjs/sequelize"; |
||||
import { InvoiceController } from "./invoice.controller"; |
||||
import { InvoiceService } from "./invoice.service"; |
||||
import { Invoice } from "./entities/invoice.entity"; |
||||
import { CartModule } from "src/cart/cart.module"; |
||||
|
||||
@Module({ |
||||
imports : [SequelizeModule.forFeature([Invoice])], |
||||
imports: [SequelizeModule.forFeature([Invoice]), forwardRef(()=>CartModule)], |
||||
controllers: [InvoiceController], |
||||
providers: [InvoiceService], |
||||
exports: [InvoiceService], |
||||
}) |
||||
export class InvoiceModule {} |
||||
|
@ -1,25 +1,59 @@ |
||||
import { Controller, Get, Query } from '@nestjs/common'; |
||||
import { PaymentService } from './payment.service'; |
||||
import { Controller, Post, Body, Param, Get, Query } from "@nestjs/common"; |
||||
import { PaymentService } from "./payment.service"; |
||||
import { CartService } from "../cart/cart.service"; |
||||
import { InvoiceService } from "../invoice/invoice.service"; |
||||
import { WalletService } from "src/wallet/wallet.service"; |
||||
|
||||
@Controller('payment') |
||||
@Controller("payment") |
||||
export class PaymentController { |
||||
constructor(private readonly paymentService: PaymentService) {} |
||||
@Get('request') |
||||
async requestPayment(){ |
||||
const amount = 10000;
|
||||
const description = 'Test payment'; |
||||
const callbackUrl = 'http://localhost:3000/payment/verify'; |
||||
const paymentUrl = await this.paymentService.requestPayment(amount, description, callbackUrl); |
||||
return { paymentUrl }; |
||||
} |
||||
@Get('verify') |
||||
async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string) { |
||||
if (status === 'OK') { |
||||
const amount = 10000;
|
||||
const refId = await this.paymentService.verifyPayment(authority, amount); |
||||
return { success: true, refId }; |
||||
} else { |
||||
return { success: false, message: 'Payment canceled' }; |
||||
constructor( |
||||
private readonly wallet: WalletService, |
||||
private readonly paymentService: PaymentService, |
||||
private readonly cartService: CartService, |
||||
) {} |
||||
|
||||
@Post("request/:userId") |
||||
async requestPayment(@Param("userId") userId: number): Promise<{ url: string }> { |
||||
const userCartItems = await this.cartService.getUserCart(userId); |
||||
|
||||
if (!userCartItems || userCartItems.cartItems.length === 0) { |
||||
throw new Error("Cart is empty!"); |
||||
} |
||||
|
||||
const totalAmount = userCartItems.totalPrice; |
||||
|
||||
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}`;
|
||||
const paymentUrl = await this.paymentService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
||||
|
||||
return { url: paymentUrl }; |
||||
} |
||||
|
||||
@Get("verify") |
||||
async verifyPayment(@Query() query: { Authority: string; Status: string; userId: number }): Promise<any> { |
||||
const { Authority, Status, userId } = query; |
||||
|
||||
if (Status !== "OK") { |
||||
throw new Error("Payment failed"); |
||||
} |
||||
|
||||
if (!userId) { |
||||
throw new Error("User ID is required."); |
||||
} |
||||
|
||||
try { |
||||
const userCartItems = await this.cartService.getUserCart(userId); |
||||
if (!userCartItems || userCartItems.cartItems.length === 0) { |
||||
throw new Error("Cart is empty!"); |
||||
} |
||||
const totalAmount = userCartItems.totalPrice; |
||||
|
||||
const refId = await this.paymentService.verifyPayment(Authority, totalAmount); |
||||
await this.wallet.addBalance(userId,totalAmount) |
||||
return { message: "Payment successful", refId}; |
||||
} catch (error) { |
||||
console.log(error) |
||||
throw new Error(`Error during payment verification: ${error.message}`); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
@ -1,19 +1,16 @@ |
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; |
||||
import { WalletService } from './wallet.service'; |
||||
import { AddBalanceResponse } from './add-balance-response.interface'; |
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; |
||||
import { WalletService } from "./wallet.service"; |
||||
import { AddBalanceResponse } from "./add-balance-response.interface"; |
||||
|
||||
@Controller('wallet') |
||||
@Controller("wallet") |
||||
export class WalletController { |
||||
constructor(private readonly walletService: WalletService) {} |
||||
@Get(':userId') |
||||
async getBalance(@Param('userId') userId: number): Promise<number> { |
||||
@Get(":userId") |
||||
async getBalance(@Param("userId") userId: number): Promise<number> { |
||||
return this.walletService.getBalance(userId); |
||||
} |
||||
@Post(':userId/add') |
||||
async addBalance( |
||||
@Param('userId') userId: number,
|
||||
@Body('amount') amount: number
|
||||
): Promise<AddBalanceResponse> { |
||||
@Post(":userId/add") |
||||
async addBalance(@Param("userId") userId: number, @Body("amount") amount: number): Promise<AddBalanceResponse> { |
||||
return this.walletService.addBalance(userId, amount); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue