feat(payment): fix zibal refund api, add maskable fields, receipt modal, standardized buttons and order refund options
Deploy Canina / deploy (push) Successful in 41s
Deploy Canina / deploy (push) Successful in 41s
This commit is contained in:
1 parent
f9e40564cf
commit
d62787cd99
22 files changed
+10934
-9318
No files matched your search
@@ -172,6 +172,21 @@ export class AdminController {
|
||||
return { success: true, data: order };
|
||||
}
|
||||
|
||||
@Post('orders/:id/refund-wallet')
|
||||
@ApiOperation({ summary: 'استرداد مستقیم مبلغ سفارش به کیف پول کاربر' })
|
||||
async refundOrderToWallet(
|
||||
@Param('id') id: string,
|
||||
@Body('amount') amount?: number,
|
||||
@Body('reason') reason?: string,
|
||||
) {
|
||||
const result = await this.adminService.refundOrderToWallet(
|
||||
id,
|
||||
amount ? Number(amount) : undefined,
|
||||
reason,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Get('coupons')
|
||||
@ApiOperation({ summary: 'لیست کد تخفیفها' })
|
||||
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
|
||||
|
||||
@@ -615,6 +615,65 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
async refundOrderToWallet(orderId: string, customAmount?: number, reason?: string) {
|
||||
const order = await this.prisma.order.findUnique({
|
||||
where: { id: orderId },
|
||||
include: { user: true },
|
||||
});
|
||||
|
||||
if (!order) {
|
||||
throw new NotFoundException('سفارش مورد نظر یافت نشد');
|
||||
}
|
||||
|
||||
if (!order.userId || !order.user) {
|
||||
throw new BadRequestException('این سفارش به کاربر ثبتنامشدهای متصل نیست');
|
||||
}
|
||||
|
||||
const refundAmount = customAmount !== undefined && customAmount > 0
|
||||
? customAmount
|
||||
: Number(order.totalAmount || 0);
|
||||
|
||||
if (refundAmount <= 0) {
|
||||
throw new BadRequestException('مبلغ استرداد باید بیشتر از صفر باشد');
|
||||
}
|
||||
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
// 1. Increment user wallet balance
|
||||
await tx.user.update({
|
||||
where: { id: order.userId },
|
||||
data: {
|
||||
walletBalance: { increment: refundAmount },
|
||||
},
|
||||
});
|
||||
|
||||
// 2. Create wallet transaction entry
|
||||
await tx.walletTransaction.create({
|
||||
data: {
|
||||
userId: order.userId,
|
||||
amount: refundAmount,
|
||||
type: 'deposit',
|
||||
status: 'completed',
|
||||
description: reason || `استرداد وجه سفارش لغو شده #${order.trackingNumber || order.id.slice(0, 8)} به کیف پول`,
|
||||
},
|
||||
});
|
||||
|
||||
// 3. Mark order as cancelled and note refund
|
||||
await tx.order.update({
|
||||
where: { id: order.id },
|
||||
data: {
|
||||
status: 'cancelled',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `مبلغ ${refundAmount.toLocaleString('fa-IR')} تومان با موفقیت به کیف پول کاربر بازگردانده شد.`,
|
||||
refundAmount,
|
||||
userId: order.userId,
|
||||
};
|
||||
}
|
||||
|
||||
async getCoupons(query: PaginationQuery = {}) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
|
||||
@@ -25,6 +25,9 @@ async function bootstrap() {
|
||||
|
||||
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
||||
|
||||
// Trust reverse proxy (Nginx / Docker ingress) to resolve real client IP from X-Forwarded-For
|
||||
app.set('trust proxy', true);
|
||||
|
||||
// Serve static uploads folder under both /uploads/ and /api/uploads/
|
||||
app.useStaticAssets(join(process.cwd(), 'uploads'), {
|
||||
prefix: '/uploads/',
|
||||
|
||||
@@ -45,6 +45,23 @@ export class PaymentController {
|
||||
private readonly zibalEBankService: ZibalEBankService,
|
||||
) {}
|
||||
|
||||
private extractRealClientIp(req: Request, fallbackIp?: string): string {
|
||||
const xForwardedFor = req.headers['x-forwarded-for'];
|
||||
if (xForwardedFor) {
|
||||
const forwardedIps = Array.isArray(xForwardedFor)
|
||||
? xForwardedFor[0]
|
||||
: xForwardedFor.split(',')[0];
|
||||
if (forwardedIps && forwardedIps.trim()) {
|
||||
return forwardedIps.trim().replace(/^::ffff:/, '');
|
||||
}
|
||||
}
|
||||
const xRealIp = req.headers['x-real-ip'];
|
||||
if (xRealIp && typeof xRealIp === 'string') {
|
||||
return xRealIp.trim().replace(/^::ffff:/, '');
|
||||
}
|
||||
return (fallbackIp || req.ip || '').replace(/^::ffff:/, '');
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Post('zibal/initiate')
|
||||
@@ -56,16 +73,17 @@ export class PaymentController {
|
||||
description: 'خطا در ایجاد تراکنش یا نامعتبر بودن سفارش',
|
||||
})
|
||||
async initiateOrderPayment(
|
||||
@Req() req: { user: { id: string } },
|
||||
@Req() req: Request & { user: { id: string } },
|
||||
@Body() body: InitiatePaymentDto,
|
||||
@Ip() ip: string,
|
||||
@Headers('user-agent') userAgent?: string,
|
||||
) {
|
||||
const realIp = this.extractRealClientIp(req, ip);
|
||||
return this.paymentService.initiateOrderPayment(
|
||||
req.user.id,
|
||||
body.orderId,
|
||||
body.customCallbackUrl,
|
||||
{ ipAddress: ip, userAgent },
|
||||
{ ipAddress: realIp, userAgent },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -77,16 +95,17 @@ export class PaymentController {
|
||||
description: 'لینک هدایت به درگاه پرداخت برای شارژ کیف پول',
|
||||
})
|
||||
async initiateWalletTopup(
|
||||
@Req() req: { user: { id: string } },
|
||||
@Req() req: Request & { user: { id: string } },
|
||||
@Body() body: InitiateWalletTopupDto,
|
||||
@Ip() ip: string,
|
||||
@Headers('user-agent') userAgent?: string,
|
||||
) {
|
||||
const realIp = this.extractRealClientIp(req, ip);
|
||||
return this.paymentService.initiateWalletTopup(
|
||||
req.user.id,
|
||||
Number(body.amount),
|
||||
body.customCallbackUrl,
|
||||
{ ipAddress: ip, userAgent },
|
||||
{ ipAddress: realIp, userAgent },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -95,11 +114,13 @@ export class PaymentController {
|
||||
summary: 'دریافت کالبک بازگشت از درگاه زیبال و تایید تراکنش',
|
||||
})
|
||||
async handleZibalCallback(
|
||||
@Req() req: Request,
|
||||
@Query() query: ZibalCallbackQueryDto,
|
||||
@Res() res: Response,
|
||||
@Ip() ip: string,
|
||||
@Headers('user-agent') userAgent?: string,
|
||||
) {
|
||||
const realIp = this.extractRealClientIp(req, ip);
|
||||
const trackId = query.trackId || '';
|
||||
const success = query.success;
|
||||
const status = query.status;
|
||||
@@ -119,7 +140,7 @@ export class PaymentController {
|
||||
success,
|
||||
status,
|
||||
orderId,
|
||||
{ ipAddress: ip, userAgent },
|
||||
{ ipAddress: realIp, userAgent },
|
||||
);
|
||||
|
||||
const params = new URLSearchParams({
|
||||
|
||||
@@ -540,19 +540,26 @@ export class ZibalService implements IPaymentGateway {
|
||||
cardNumber?: string;
|
||||
description?: string;
|
||||
}) {
|
||||
const accountId =
|
||||
options.accountId ||
|
||||
(await this.prisma.uiText.findUnique({ where: { key: 'ZIBAL_EBANK_ACCOUNT_ID' } }))?.value;
|
||||
const merchant = await this.getMerchant();
|
||||
const accountIdSetting = (
|
||||
await this.prisma.uiText.findUnique({ where: { key: 'ZIBAL_EBANK_ACCOUNT_ID' } })
|
||||
)?.value;
|
||||
|
||||
const payload = {
|
||||
const payload: Record<string, any> = {
|
||||
merchant: merchant || undefined,
|
||||
merchantId: merchant || undefined,
|
||||
trackId: Number(options.trackId),
|
||||
accountId: accountId || 'default',
|
||||
tryReverse: options.tryReverse ?? false,
|
||||
amount: options.amount,
|
||||
cardNumber: options.cardNumber,
|
||||
amount: options.amount ? Number(options.amount) : undefined,
|
||||
cardNumber: options.cardNumber || undefined,
|
||||
description: options.description || 'استرداد وجه سفارش در کنینا',
|
||||
};
|
||||
|
||||
const finalAccountId = options.accountId || accountIdSetting;
|
||||
if (finalAccountId && finalAccountId.trim() !== '') {
|
||||
payload.accountId = finalAccountId.trim();
|
||||
}
|
||||
|
||||
return this.apiRequest<any>('/v1/account/refund', 'POST', payload);
|
||||
}
|
||||
|
||||
@@ -563,7 +570,10 @@ export class ZibalService implements IPaymentGateway {
|
||||
refundId?: number | string;
|
||||
transactionTrackId?: number | string;
|
||||
}) {
|
||||
const merchant = await this.getMerchant();
|
||||
return this.apiRequest<any>('/v1/account/refund/inquiry', 'POST', {
|
||||
merchant: merchant || undefined,
|
||||
merchantId: merchant || undefined,
|
||||
refundId: params.refundId ? Number(params.refundId) : undefined,
|
||||
transactionTrackId: params.transactionTrackId
|
||||
? Number(params.transactionTrackId)
|
||||
@@ -582,9 +592,12 @@ export class ZibalService implements IPaymentGateway {
|
||||
fromDate?: string;
|
||||
toDate?: string;
|
||||
}) {
|
||||
const merchant = await this.getMerchant();
|
||||
return this.apiRequest<any>('/v1/account/refund/list', 'POST', {
|
||||
merchant: merchant || undefined,
|
||||
merchantId: merchant || undefined,
|
||||
status: filter.status,
|
||||
type: filter.type || 4,
|
||||
type: filter.type,
|
||||
page: filter.page || 1,
|
||||
size: filter.size || 20,
|
||||
fromDate: filter.fromDate,
|
||||
|
||||
Reference in new issue
Block a user