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 { UserDataAccess } from '../../dataAccess/user.dataAccess';
|
||||||
import { PaymentService } from './payment.service';
|
import { PaymentService } from './payment.service';
|
||||||
import { PaymentController } from './payment.controller';
|
import { PaymentController } from './payment.controller';
|
||||||
import { PaymentDataAccess } from 'src/dataAccess/payment.dataAccess';
|
import { PaymentDataAccess } from '../../dataAccess/payment.dataAccess';
|
||||||
import { PaymentGateway } from '../IPG/PaymentGateway';
|
import { PaymentGateway } from '../IPG/PaymentGateway';
|
||||||
import { ZarinPalGateway } from '../IPG/gateways/zarinpal';
|
import { ZarinPalGateway } from '../IPG/gateways/zarinpal';
|
||||||
|
import { PurchaseDataAccess } from '../../dataAccess/purchase.dataAccess';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -15,6 +16,7 @@ import { ZarinPalGateway } from '../IPG/gateways/zarinpal';
|
|||||||
providers: [
|
providers: [
|
||||||
ZarinPalGateway,
|
ZarinPalGateway,
|
||||||
PaymentDataAccess,
|
PaymentDataAccess,
|
||||||
|
PurchaseDataAccess,
|
||||||
PaymentGateway,
|
PaymentGateway,
|
||||||
PaymentService,
|
PaymentService,
|
||||||
UserDataAccess,
|
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 { PaymentDataAccess } from "../../dataAccess/payment.dataAccess";
|
||||||
import { startSession } from "mongoose";
|
import mongoose, { Mongoose, startSession } from "mongoose";
|
||||||
import { PaymentGateway } from "../IPG/PaymentGateway";
|
import { PaymentGateway } from "../IPG/PaymentGateway";
|
||||||
import { UserDataAccess } from "../../dataAccess/user.dataAccess";
|
import { UserDataAccess } from "../../dataAccess/user.dataAccess";
|
||||||
|
import { PurchaseDataAccess } from "../../dataAccess/purchase.dataAccess";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PaymentService {
|
export class PaymentService {
|
||||||
@@ -11,18 +12,40 @@ export class PaymentService {
|
|||||||
private readonly paymentDataAccess: PaymentDataAccess,
|
private readonly paymentDataAccess: PaymentDataAccess,
|
||||||
private readonly paymentGateway: PaymentGateway,
|
private readonly paymentGateway: PaymentGateway,
|
||||||
private readonly userDataAccess: UserDataAccess,
|
private readonly userDataAccess: UserDataAccess,
|
||||||
|
private readonly purchaseDataAccess: PurchaseDataAccess
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async createPayment(userId: string, amount: number, courseId: string) {
|
async createPayment(userId: string, amount: number, courseId: string) {
|
||||||
const session = await startSession();
|
const session = await startSession();
|
||||||
session.startTransaction();
|
session.startTransaction();
|
||||||
try {
|
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" });
|
const paymentData = await this.paymentGateway.requestPayment({ amount, userId, callbackPath: "cart" });
|
||||||
console.log(paymentData)
|
console.log(paymentData)
|
||||||
if (!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(
|
const payment = await this.paymentDataAccess.createPayment(
|
||||||
userId,
|
userId,
|
||||||
amount,
|
amount,
|
||||||
@@ -38,6 +61,7 @@ export class PaymentService {
|
|||||||
await session.abortTransaction();
|
await session.abortTransaction();
|
||||||
await session.endSession();
|
await session.endSession();
|
||||||
this.logger.warn(error);
|
this.logger.warn(error);
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,10 +75,20 @@ export class PaymentService {
|
|||||||
}
|
}
|
||||||
const verifyData = await this.paymentGateway.verifyPayment({ authority, amount: paymentData.amount });
|
const verifyData = await this.paymentGateway.verifyPayment({ authority, amount: paymentData.amount });
|
||||||
if (verifyData.code === 100 || verifyData.code === 101) {
|
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);
|
const registerCourse = await this.userDataAccess.registerCourse(paymentData.userId, paymentData.courseId);
|
||||||
console.log(registerCourse)
|
console.log(registerCourse)
|
||||||
if (!registerCourse.success) {
|
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 }
|
return { success: true, payment: paymentData, verifyData }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user