feat: complete payment
This commit is contained in:
@@ -3,9 +3,10 @@ import { UserModule } from '../user/user.module';
|
||||
import { UserDataAccess } from '../../dataAccess/user.dataAccess';
|
||||
import { PaymentService } from './payment.service';
|
||||
import { PaymentController } from './payment.controller';
|
||||
import { PaymentDataAccess } from 'src/dataAccess/payment.dataAccess';
|
||||
import { PaymentDataAccess } from '../../dataAccess/payment.dataAccess';
|
||||
import { PaymentGateway } from '../IPG/PaymentGateway';
|
||||
import { ZarinPalGateway } from '../IPG/gateways/zarinpal';
|
||||
import { PurchaseDataAccess } from '../../dataAccess/purchase.dataAccess';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -15,6 +16,7 @@ import { ZarinPalGateway } from '../IPG/gateways/zarinpal';
|
||||
providers: [
|
||||
ZarinPalGateway,
|
||||
PaymentDataAccess,
|
||||
PurchaseDataAccess,
|
||||
PaymentGateway,
|
||||
PaymentService,
|
||||
UserDataAccess,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common";
|
||||
import { PaymentDataAccess } from "../../dataAccess/payment.dataAccess";
|
||||
import { startSession } from "mongoose";
|
||||
import mongoose, { Mongoose, startSession } from "mongoose";
|
||||
import { PaymentGateway } from "../IPG/PaymentGateway";
|
||||
import { UserDataAccess } from "../../dataAccess/user.dataAccess";
|
||||
import { PurchaseDataAccess } from "../../dataAccess/purchase.dataAccess";
|
||||
|
||||
@Injectable()
|
||||
export class PaymentService {
|
||||
@@ -11,18 +12,40 @@ export class PaymentService {
|
||||
private readonly paymentDataAccess: PaymentDataAccess,
|
||||
private readonly paymentGateway: PaymentGateway,
|
||||
private readonly userDataAccess: UserDataAccess,
|
||||
private readonly purchaseDataAccess: PurchaseDataAccess
|
||||
) { }
|
||||
|
||||
async createPayment(userId: string, amount: number, courseId: string) {
|
||||
const session = await startSession();
|
||||
session.startTransaction();
|
||||
try {
|
||||
const user = await this.userDataAccess.findOne(userId);
|
||||
console.log({courseId: new mongoose.Types.ObjectId(courseId)})
|
||||
const courseExists = user.courseIds.find(id => id.equals(courseId));
|
||||
if(courseExists) {
|
||||
console.log("first if")
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.BAD_REQUEST,
|
||||
error: 'شما قبلا این دوره را خریداری کرده اید',
|
||||
},
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
const paymentData = await this.paymentGateway.requestPayment({ amount, userId, callbackPath: "cart" });
|
||||
console.log(paymentData)
|
||||
if (!paymentData) {
|
||||
throw new Error("Error in payment process");
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.BAD_REQUEST,
|
||||
error: 'درخواست خرید دوره با مشکل مواجه شد',
|
||||
},
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const payment = await this.paymentDataAccess.createPayment(
|
||||
userId,
|
||||
amount,
|
||||
@@ -38,6 +61,7 @@ export class PaymentService {
|
||||
await session.abortTransaction();
|
||||
await session.endSession();
|
||||
this.logger.warn(error);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,10 +75,20 @@ export class PaymentService {
|
||||
}
|
||||
const verifyData = await this.paymentGateway.verifyPayment({ authority, amount: paymentData.amount });
|
||||
if (verifyData.code === 100 || verifyData.code === 101) {
|
||||
const purchase = await this.purchaseDataAccess.createPurchase(paymentData.userId.toString(), paymentData.courseId.toString(), paymentData._id);
|
||||
console.log(purchase)
|
||||
const registerCourse = await this.userDataAccess.registerCourse(paymentData.userId, paymentData.courseId);
|
||||
console.log(registerCourse)
|
||||
if (!registerCourse.success) {
|
||||
throw new Error("error in register course");
|
||||
if (!paymentData) {
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.BAD_REQUEST,
|
||||
error: 'درخواست خرید دوره با مشکل مواجه شد',
|
||||
},
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
return { success: true, payment: paymentData, verifyData }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user