fix: resolve wallet topup callback and verify flow, fix admin orders BigInt serialization 500 error, and fix reactive B2B settings toggle
Deploy Canina / deploy (push) Successful in 1m41s
Deploy Canina / deploy (push) Successful in 1m41s
This commit is contained in:
1 parent
42707c04cf
commit
b527befe4a
10 files changed
+176
-29
No files matched your search
@@ -9,6 +9,11 @@ import { PrismaExceptionFilter } from './common/filters/prisma-exception.filter'
|
||||
import { DecimalInterceptor } from './common/interceptors/decimal.interceptor';
|
||||
import helmet from 'helmet';
|
||||
|
||||
// Ensure BigInt can be serialized to JSON across all NestJS / Express responses
|
||||
(BigInt.prototype as any).toJSON = function () {
|
||||
return Number(this);
|
||||
};
|
||||
|
||||
async function bootstrap() {
|
||||
process.env.JWT_ACCESS_SECRET =
|
||||
process.env.JWT_ACCESS_SECRET ||
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
HttpStatus,
|
||||
Headers,
|
||||
Ip,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import type { Request, Response } from 'express';
|
||||
import { PaymentService } from './payment.service';
|
||||
@@ -138,6 +139,25 @@ export class PaymentController {
|
||||
}
|
||||
}
|
||||
|
||||
@Get('zibal/verify')
|
||||
@ApiOperation({ summary: 'استعلام و تایید تراکنش زیبال توسط کلاینت' })
|
||||
async verifyPaymentDirect(
|
||||
@Query('trackId') trackId: string,
|
||||
@Query('success') success?: string,
|
||||
@Query('status') status?: string,
|
||||
@Query('orderId') orderId?: string,
|
||||
@Ip() ip?: string,
|
||||
@Headers('user-agent') userAgent?: string,
|
||||
) {
|
||||
if (!trackId) {
|
||||
throw new BadRequestException('trackId is required');
|
||||
}
|
||||
return this.paymentService.verifyAndProcess(trackId, success, status, orderId, {
|
||||
ipAddress: ip,
|
||||
userAgent,
|
||||
});
|
||||
}
|
||||
|
||||
@Post('zibal/callback')
|
||||
@ApiOperation({ summary: 'دریافت وبهوک / کالبک لِیزی زیبال' })
|
||||
async handleZibalLazyCallback(
|
||||
|
||||
@@ -56,9 +56,9 @@ export class PaymentService {
|
||||
);
|
||||
}
|
||||
|
||||
const frontendUrl = await this.zibalService.getFrontendUrl();
|
||||
const backendUrl = await this.zibalService.getBackendUrl();
|
||||
const callbackUrl =
|
||||
customCallbackUrl || `${frontendUrl}/payment/verify?orderId=${order.id}`;
|
||||
customCallbackUrl || `${backendUrl}/payment/zibal/callback?orderId=${order.id}`;
|
||||
|
||||
const requestPayload = {
|
||||
amountRials,
|
||||
@@ -151,9 +151,9 @@ export class PaymentService {
|
||||
}
|
||||
|
||||
const amountRials = Math.round(amountTomans * 10);
|
||||
const frontendUrl = await this.zibalService.getFrontendUrl();
|
||||
const backendUrl = await this.zibalService.getBackendUrl();
|
||||
const callbackUrl =
|
||||
customCallbackUrl || `${frontendUrl}/payment/verify?type=wallet`;
|
||||
customCallbackUrl || `${backendUrl}/payment/zibal/callback?type=WALLET_TOPUP`;
|
||||
|
||||
const requestPayload = {
|
||||
amountRials,
|
||||
|
||||
@@ -122,6 +122,31 @@ export class ZibalService implements IPaymentGateway {
|
||||
return envUrl ? envUrl.replace(/\/+$/, '') : 'https://canina.ir';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves base backend API URL for Zibal callbacks
|
||||
*/
|
||||
async getBackendUrl(): Promise<string> {
|
||||
try {
|
||||
const setting = await this.prisma.uiText.findUnique({
|
||||
where: { key: 'BACKEND_URL' },
|
||||
});
|
||||
if (setting && setting.value && setting.value.trim() !== '') {
|
||||
return setting.value.trim().replace(/\/+$/, '');
|
||||
}
|
||||
} catch (e) {
|
||||
this.logger.warn(`Could not read BACKEND_URL from db: ${e}`);
|
||||
}
|
||||
|
||||
const envUrl = process.env.BACKEND_URL || process.env.API_URL;
|
||||
if (envUrl) return envUrl.replace(/\/+$/, '');
|
||||
|
||||
const frontendUrl = await this.getFrontendUrl();
|
||||
if (frontendUrl.includes('stage')) {
|
||||
return 'https://stageapi.canina.ir/api';
|
||||
}
|
||||
return 'https://api.canina.ir/api';
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Request Payment (درخواست پرداخت با قابلیت Retry)
|
||||
* Amount must be in Rials.
|
||||
|
||||
@@ -22,14 +22,40 @@ export class SettingsService {
|
||||
this.prisma.setting.findMany(),
|
||||
]);
|
||||
|
||||
const merged = [...uiTexts];
|
||||
const mergedMap = new Map<string, string>();
|
||||
|
||||
// 1. Add all from uiTexts
|
||||
uiTexts.forEach((u) => {
|
||||
mergedMap.set(u.key, u.value);
|
||||
});
|
||||
|
||||
// 2. Add from settings and unpack object keys
|
||||
settings.forEach((s) => {
|
||||
const valStr =
|
||||
typeof s.value === 'string' ? s.value : JSON.stringify(s.value);
|
||||
merged.push({ key: s.key, value: valStr });
|
||||
mergedMap.set(s.key, valStr);
|
||||
|
||||
if (
|
||||
typeof s.value === 'object' &&
|
||||
s.value !== null &&
|
||||
!Array.isArray(s.value)
|
||||
) {
|
||||
Object.entries(s.value as Record<string, any>).forEach(([k, v]) => {
|
||||
mergedMap.set(k, String(v));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return merged;
|
||||
// If b2bRegistrationOpen is set, sync b2b_enabled
|
||||
if (mergedMap.has('b2bRegistrationOpen')) {
|
||||
const val = mergedMap.get('b2bRegistrationOpen')!;
|
||||
mergedMap.set('b2b_enabled', val);
|
||||
}
|
||||
|
||||
return Array.from(mergedMap.entries()).map(([key, value]) => ({
|
||||
key,
|
||||
value,
|
||||
}));
|
||||
}
|
||||
|
||||
async updateUiText(key: string, value: string) {
|
||||
@@ -80,11 +106,35 @@ export class SettingsService {
|
||||
|
||||
async updateCategorySetting(category: string, value: Prisma.InputJsonValue) {
|
||||
const key = `${category}_config`;
|
||||
return this.prisma.setting.upsert({
|
||||
const res = await this.prisma.setting.upsert({
|
||||
where: { key },
|
||||
update: { category, value },
|
||||
create: { key, category, value },
|
||||
});
|
||||
|
||||
// If system config is updated, also sync individual keys into uiText for fast & reliable lookup
|
||||
if (category === 'system' && typeof value === 'object' && value !== null) {
|
||||
const obj = value as Record<string, any>;
|
||||
const promises: Promise<any>[] = [];
|
||||
if ('b2bRegistrationOpen' in obj) {
|
||||
const valStr = String(obj.b2bRegistrationOpen);
|
||||
promises.push(
|
||||
this.prisma.uiText.upsert({
|
||||
where: { key: 'b2bRegistrationOpen' },
|
||||
update: { value: valStr },
|
||||
create: { key: 'b2bRegistrationOpen', value: valStr },
|
||||
}),
|
||||
this.prisma.uiText.upsert({
|
||||
where: { key: 'b2b_enabled' },
|
||||
update: { value: valStr },
|
||||
create: { key: 'b2b_enabled', value: valStr },
|
||||
}),
|
||||
);
|
||||
}
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async getSmsSettings() {
|
||||
|
||||
Reference in new issue
Block a user