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 { Module, forwardRef } from "@nestjs/common"; |
||||||
import { InvoiceService } from './invoice.service'; |
import { SequelizeModule } from "@nestjs/sequelize"; |
||||||
import { InvoiceController } from './invoice.controller'; |
import { InvoiceController } from "./invoice.controller"; |
||||||
import { SequelizeModule } from '@nestjs/sequelize'; |
import { InvoiceService } from "./invoice.service"; |
||||||
import { Invoice } from './entities/invoice.entity'; |
import { Invoice } from "./entities/invoice.entity"; |
||||||
|
import { CartModule } from "src/cart/cart.module"; |
||||||
|
|
||||||
@Module({ |
@Module({ |
||||||
imports : [SequelizeModule.forFeature([Invoice])], |
imports: [SequelizeModule.forFeature([Invoice]), forwardRef(()=>CartModule)], |
||||||
controllers: [InvoiceController], |
controllers: [InvoiceController], |
||||||
providers: [InvoiceService], |
providers: [InvoiceService], |
||||||
|
exports: [InvoiceService], |
||||||
}) |
}) |
||||||
export class InvoiceModule {} |
export class InvoiceModule {} |
||||||
|
@ -1,25 +1,59 @@ |
|||||||
import { Controller, Get, Query } from '@nestjs/common'; |
import { Controller, Post, Body, Param, Get, Query } from "@nestjs/common"; |
||||||
import { PaymentService } from './payment.service'; |
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 { |
export class PaymentController { |
||||||
constructor(private readonly paymentService: PaymentService) {} |
constructor( |
||||||
@Get('request') |
private readonly wallet: WalletService, |
||||||
async requestPayment(){ |
private readonly paymentService: PaymentService, |
||||||
const amount = 10000;
|
private readonly cartService: CartService, |
||||||
const description = 'Test payment'; |
) {} |
||||||
const callbackUrl = 'http://localhost:3000/payment/verify'; |
|
||||||
const paymentUrl = await this.paymentService.requestPayment(amount, description, callbackUrl); |
@Post("request/:userId") |
||||||
return { paymentUrl }; |
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!"); |
||||||
} |
} |
||||||
@Get('verify') |
|
||||||
async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string) { |
const totalAmount = userCartItems.totalPrice; |
||||||
if (status === 'OK') { |
|
||||||
const amount = 10000;
|
const callbackUrl = `http://localhost:3000/payment/verify?userId=${userId}`;
|
||||||
const refId = await this.paymentService.verifyPayment(authority, amount); |
const paymentUrl = await this.paymentService.requestPayment(totalAmount, "Purchase products", callbackUrl); |
||||||
return { success: true, refId }; |
|
||||||
} else { |
return { url: paymentUrl }; |
||||||
return { success: false, message: 'Payment canceled' }; |
} |
||||||
|
|
||||||
|
@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 { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; |
||||||
import { WalletService } from './wallet.service'; |
import { WalletService } from "./wallet.service"; |
||||||
import { AddBalanceResponse } from './add-balance-response.interface'; |
import { AddBalanceResponse } from "./add-balance-response.interface"; |
||||||
|
|
||||||
@Controller('wallet') |
@Controller("wallet") |
||||||
export class WalletController { |
export class WalletController { |
||||||
constructor(private readonly walletService: WalletService) {} |
constructor(private readonly walletService: WalletService) {} |
||||||
@Get(':userId') |
@Get(":userId") |
||||||
async getBalance(@Param('userId') userId: number): Promise<number> { |
async getBalance(@Param("userId") userId: number): Promise<number> { |
||||||
return this.walletService.getBalance(userId); |
return this.walletService.getBalance(userId); |
||||||
} |
} |
||||||
@Post(':userId/add') |
@Post(":userId/add") |
||||||
async addBalance( |
async addBalance(@Param("userId") userId: number, @Body("amount") amount: number): Promise<AddBalanceResponse> { |
||||||
@Param('userId') userId: number,
|
|
||||||
@Body('amount') amount: number
|
|
||||||
): Promise<AddBalanceResponse> { |
|
||||||
return this.walletService.addBalance(userId, amount); |
return this.walletService.addBalance(userId, amount); |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue