|
|
|
@ -463,7 +463,35 @@ export class ProductsService { |
|
|
|
|
throw new HttpException("An error occurred while adding balance to the wallet.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//process of payment
|
|
|
|
|
async processPayment(userId: number, amount: number): Promise<string> { |
|
|
|
|
const wallet = await this.walletModel.findOne({ where: { userId } }); |
|
|
|
|
|
|
|
|
|
if (!wallet) { |
|
|
|
|
throw new HttpException("Please Charge your wallet", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (wallet.balance < amount) { |
|
|
|
|
throw new HttpException("Insufficient funds", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
wallet.balance -= amount; |
|
|
|
|
|
|
|
|
|
await this.transactionModel.create({ |
|
|
|
|
walletId: wallet.id, |
|
|
|
|
amount: `-${amount}`, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
await wallet.save(); |
|
|
|
|
|
|
|
|
|
return "Payment processed successfully"; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error("Error processing payment:", error.message); |
|
|
|
|
throw new HttpException("An error occurred while processing the payment.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
///////////////////////////////////////////payment//////////////////////////////////////////////
|
|
|
|
|
//payment request
|
|
|
|
|
async requestPayment(amount: number, description: string, callbackUrl: string): Promise<string> { |
|
|
|
|
try { |
|
|
|
|
const result = await this.zarinpal.PaymentRequest({ |
|
|
|
@ -482,4 +510,142 @@ export class ProductsService { |
|
|
|
|
throw new InternalServerErrorException(`Error in payment request: ${error.message}`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//payment verify
|
|
|
|
|
async verifyPayment(authority: string, amount: number): Promise<string> { |
|
|
|
|
try { |
|
|
|
|
const result = await this.zarinpal.PaymentVerification({ |
|
|
|
|
Amount: amount, |
|
|
|
|
Authority: authority, |
|
|
|
|
}); |
|
|
|
|
if (result.status === 100) { |
|
|
|
|
return result.RefID; |
|
|
|
|
} else { |
|
|
|
|
throw new Error(`Payment verification failed with status: ${result.status}`); |
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
throw new InternalServerErrorException(`Error in payment verification: ${error.message}`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
///////////////////////////////////////////invoice//////////////////////////////////////////////
|
|
|
|
|
// get invoice by user
|
|
|
|
|
async getInvoiceByUser(userId: number) { |
|
|
|
|
try { |
|
|
|
|
if (!userId) { |
|
|
|
|
throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const invoices = await this.invoiceModel.findAll({ |
|
|
|
|
where: { userId }, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!invoices) { |
|
|
|
|
throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { invoices }; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//get list of invoices by admin
|
|
|
|
|
async getInvoices() { |
|
|
|
|
try { |
|
|
|
|
const invoices = await this.invoiceModel.findAll(); |
|
|
|
|
|
|
|
|
|
if (!invoices) { |
|
|
|
|
throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { invoices }; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//get user invoices
|
|
|
|
|
async getUserInvoices(userId: number) { |
|
|
|
|
try { |
|
|
|
|
if (!userId) { |
|
|
|
|
throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const invoices = await this.invoiceModel.findAll({ |
|
|
|
|
where: { userId }, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!invoices) { |
|
|
|
|
throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { invoices }; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//create invoices from cart
|
|
|
|
|
async createInvoiceFromCart(userId: number): Promise<Invoice> { |
|
|
|
|
try { |
|
|
|
|
const invoice = await this.invoiceModel.create({ |
|
|
|
|
userId, |
|
|
|
|
totalPaymentAmount: 0, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!invoice) { |
|
|
|
|
throw new HttpException("Failed to create invoice", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return invoice; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
throw new HttpException("An error occurred while creating the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//update total payment
|
|
|
|
|
async updateTotalPayment(userId: number) { |
|
|
|
|
const userCartItems = await this.getUserOpenCart(userId); |
|
|
|
|
if (!userCartItems || !userCartItems.cartItems || userCartItems.cartItems.length === 0) { |
|
|
|
|
throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let invoice = await this.invoiceModel.findOne({ where: { userId, status: "pending" } }); |
|
|
|
|
if (!invoice) { |
|
|
|
|
throw new HttpException("Invoice not found", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
invoice.totalPaymentAmount = userCartItems.totalPrice; |
|
|
|
|
await invoice.save(); |
|
|
|
|
} |
|
|
|
|
//get pending user invoices
|
|
|
|
|
async getInvoicePendingByUser(userId: number): Promise<Invoice> { |
|
|
|
|
try { |
|
|
|
|
if (!userId) { |
|
|
|
|
throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const invoice = await this.invoiceModel.findOne({ |
|
|
|
|
where: { userId, status: "pending" }, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!invoice) { |
|
|
|
|
throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return invoice; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|