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
IPG_TYPE=sandbox
SITE_URL=https://dlearn-api.run.danakcorp.com/payment
ACADEMY_URL=http://academy.shinan.ir
EMAIL_HOST=
EMAIL_PORT=
EMAIL_USER=
+1
View File
@@ -6,6 +6,7 @@ CONNECTION_STRING=
MERCHANT_ID=250ae134-635f-4728-8415-ee9a984616e8
IPG_TYPE=sandbox
SITE_URL=https://dlearn-api.run.danakcorp.com/payment
ACADEMY_URL=http://academy.shinan.ir
EMAIL_HOST=
EMAIL_PORT=
EMAIL_USER=
+10 -15
View File
@@ -20,7 +20,7 @@ export class ZarinPalGateway {
amount,
callback_url: `${this.callbackURL}/${callbackPath}`,
description: "خرید دوره",
currency: "IRR" as const,
currency: "IRT" as const,
metadata: { userId },
}
@@ -40,20 +40,15 @@ export class ZarinPalGateway {
}
async verifyPayment(authority: string, amount: number) {
try {
const verifyData = {
merchant_id: this.merchantID,
amount,
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 verifyData = {
merchant_id: this.merchantID,
amount,
authority,
}
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,
Post,
Query,
Res,
UseGuards,
} from '@nestjs/common';
import {
@@ -15,6 +16,7 @@ import {
ApiQuery,
} from '@nestjs/swagger';
import { CurrentUser } from '../../decorators/currentUser.decorator';
import { Response } from 'express';
import { AuthGuard } from '../../auth/auth.guard';
import { ApiTags } from '@nestjs/swagger';
import { Roles } from '../../decorators/role.decorators';
@@ -58,9 +60,14 @@ export class PaymentController {
description: 'get status',
})
@Get('/verify/Zarinpal/cart')
async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string) {
console.log({ authorityInController: authority, status })
return this.paymentService.verifyPayment(authority, status);
async verifyPayment(@Query('Authority') authority: string, @Query('Status') status: string,@Res() res: Response) {
if (status === 'NOK') {
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({
+1 -2
View File
@@ -115,13 +115,12 @@ export class PaymentService {
async verifyPayment(authority: string, status: string) {
console.log({ authorityInService: authority })
const paymentData = await this.paymentDataAccess.getPaymentByAuthority(authority);
console.log({ paymentData })
if (!paymentData) {
throw new Error("payment not found");
}
const verifyData = await this.paymentGateway.verifyPayment({ authority, amount: paymentData.amount });
console.log({verifyData})
if (verifyData.code === 100 || verifyData.code === 101) {
await this.purchaseDataAccess.createPurchase(paymentData.userId.toString(), paymentData.courseId.toString(), paymentData._id.toString());
paymentData.paymentStatus = PaymentStatus.Completed;