fix: handle cancel payment

This commit is contained in:
Matin
2025-01-06 00:55:45 +03:30
parent cc7f487e0d
commit 494c2e7b8f
5 changed files with 23 additions and 20 deletions
+1
View File
@@ -6,6 +6,7 @@ CONNECTION_STRING=
MERCHANT_ID=250ae134-635f-4728-8415-ee9a984616e8 MERCHANT_ID=250ae134-635f-4728-8415-ee9a984616e8
IPG_TYPE=sandbox IPG_TYPE=sandbox
SITE_URL=https://dlearn-api.run.danakcorp.com/payment SITE_URL=https://dlearn-api.run.danakcorp.com/payment
ACADEMY_URL=http://academy.shinan.ir
EMAIL_HOST= EMAIL_HOST=
EMAIL_PORT= EMAIL_PORT=
EMAIL_USER= EMAIL_USER=
+1
View File
@@ -6,6 +6,7 @@ CONNECTION_STRING=
MERCHANT_ID=250ae134-635f-4728-8415-ee9a984616e8 MERCHANT_ID=250ae134-635f-4728-8415-ee9a984616e8
IPG_TYPE=sandbox IPG_TYPE=sandbox
SITE_URL=https://dlearn-api.run.danakcorp.com/payment SITE_URL=https://dlearn-api.run.danakcorp.com/payment
ACADEMY_URL=http://academy.shinan.ir
EMAIL_HOST= EMAIL_HOST=
EMAIL_PORT= EMAIL_PORT=
EMAIL_USER= EMAIL_USER=
+10 -15
View File
@@ -20,7 +20,7 @@ export class ZarinPalGateway {
amount, amount,
callback_url: `${this.callbackURL}/${callbackPath}`, callback_url: `${this.callbackURL}/${callbackPath}`,
description: "خرید دوره", description: "خرید دوره",
currency: "IRR" as const, currency: "IRT" as const,
metadata: { userId }, metadata: { userId },
} }
@@ -40,20 +40,15 @@ export class ZarinPalGateway {
} }
async verifyPayment(authority: string, amount: number) { async verifyPayment(authority: string, amount: number) {
try { const verifyData = {
const verifyData = { merchant_id: this.merchantID,
merchant_id: this.merchantID, amount,
amount, authority,
authority,
}
const response = await axios.post(`${this.gatewayApiURL}/v4/payment/verify.json`, verifyData, { headers: this.requestHeaders });
const { data } = response.data;
return data;
} catch (error) {
this.logger.error("error in payment verification", error);
throw new InternalServerErrorException("error in payment verification");
} }
const response = await axios.post(`${this.gatewayApiURL}/v4/payment/verify.json`, verifyData, { headers: this.requestHeaders });
const { data } = response.data;
return data;
} }
} }
+10 -3
View File
@@ -6,6 +6,7 @@ import {
HttpStatus, HttpStatus,
Post, Post,
Query, Query,
Res,
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { import {
@@ -15,6 +16,7 @@ import {
ApiQuery, ApiQuery,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { CurrentUser } from '../../decorators/currentUser.decorator'; import { CurrentUser } from '../../decorators/currentUser.decorator';
import { Response } from 'express';
import { AuthGuard } from '../../auth/auth.guard'; import { AuthGuard } from '../../auth/auth.guard';
import { ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
import { Roles } from '../../decorators/role.decorators'; import { Roles } from '../../decorators/role.decorators';
@@ -58,9 +60,14 @@ export class PaymentController {
description: 'get status', description: 'get status',
}) })
@Get('/verify/Zarinpal/cart') @Get('/verify/Zarinpal/cart')
async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string) { async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string,@Res() res: Response) {
console.log({ authorityInController: authority, status }) if (status === 'NOK') {
return this.paymentService.verifyPayment(authority, status); return res.redirect(`${process.env.ACADEMY_URL}/payment/verify/Zarinpal/cart?Authority=${authority}&Status=${status}`)
}
const result = await this.paymentService.verifyPayment(authority, status);
console.log({resultincontroller:result})
return res.redirect(`${process.env.ACADEMY_URL}/payment/verify/Zarinpal/cart?Authority=${authority}&Status=${status}`)
} }
@ApiOperation({ @ApiOperation({
+1 -2
View File
@@ -115,13 +115,12 @@ export class PaymentService {
async verifyPayment(authority: string, status: string) { async verifyPayment(authority: string, status: string) {
console.log({ authorityInService: authority })
const paymentData = await this.paymentDataAccess.getPaymentByAuthority(authority); const paymentData = await this.paymentDataAccess.getPaymentByAuthority(authority);
console.log({ paymentData })
if (!paymentData) { if (!paymentData) {
throw new Error("payment not found"); throw new Error("payment not found");
} }
const verifyData = await this.paymentGateway.verifyPayment({ authority, amount: paymentData.amount }); const verifyData = await this.paymentGateway.verifyPayment({ authority, amount: paymentData.amount });
console.log({verifyData})
if (verifyData.code === 100 || verifyData.code === 101) { if (verifyData.code === 100 || verifyData.code === 101) {
await this.purchaseDataAccess.createPurchase(paymentData.userId.toString(), paymentData.courseId.toString(), paymentData._id.toString()); await this.purchaseDataAccess.createPurchase(paymentData.userId.toString(), paymentData.courseId.toString(), paymentData._id.toString());
paymentData.paymentStatus = PaymentStatus.Completed; paymentData.paymentStatus = PaymentStatus.Completed;