fix(payment): resolve zibal refund apiKey and token resolution across aliases
Deploy Canina / deploy (push) Successful in 33s

This commit is contained in:
parsa aghaei committed 2026-08-22 17:13:17 +03:30
1 parent d62787cd99
commit 992c815749
13 files changed
+7768 -7059

No files matched your search

+55 -10
View File
@@ -458,16 +458,53 @@ export class ZibalService implements IPaymentGateway {
private async getAccessToken(): Promise<string> {
try {
const setting = await this.prisma.uiText.findUnique({
where: { key: 'ZIBAL_EBANK_TOKEN' },
// Check multiple possible keys where the admin might have saved the API Token / EBank token
const settings = await this.prisma.uiText.findMany({
where: {
key: {
in: [
'ZIBAL_EBANK_TOKEN',
'zibal_ebank_token',
'ZIBAL_API_KEY',
'zibal_api_key',
'ZIBAL_TOKEN',
'zibal_token',
'ZIBAL_MERCHANT',
'zibal_merchant',
],
},
},
});
if (setting && setting.value && setting.value.trim() !== '') {
return setting.value.trim();
const map = new Map(settings.map((s) => [s.key, s.value?.trim()]));
const token =
map.get('ZIBAL_EBANK_TOKEN') ||
map.get('zibal_ebank_token') ||
map.get('ZIBAL_API_KEY') ||
map.get('zibal_api_key') ||
map.get('ZIBAL_TOKEN') ||
map.get('zibal_token');
if (token && token.length > 0 && token !== 'zibal') {
return token;
}
// If token not set, check merchant if it's a real merchant key (not sandbox 'zibal')
const merchant = map.get('ZIBAL_MERCHANT') || map.get('zibal_merchant');
if (merchant && merchant.length > 0 && merchant !== 'zibal') {
return merchant;
}
} catch (e) {
this.logger.warn(`Could not read ZIBAL_EBANK_TOKEN: ${e}`);
this.logger.warn(`Could not read Zibal token from db: ${e}`);
}
return process.env.ZIBAL_EBANK_TOKEN || '';
return (
process.env.ZIBAL_EBANK_TOKEN ||
process.env.ZIBAL_API_KEY ||
process.env.ZIBAL_TOKEN ||
process.env.ZIBAL_MERCHANT ||
''
);
}
private async apiRequest<T>(
@@ -482,16 +519,19 @@ export class ZibalService implements IPaymentGateway {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (apiKey) {
headers['Authorization'] = `Bearer ${apiKey}`;
// Zibal API accepts Bearer Token in Authorization header
if (token && token !== 'zibal') {
headers['Authorization'] = `Bearer ${token}`;
}
// Automatically inject apiKey into body if it's a POST request and not provided
// Prepare JSON body: ensure apiKey / merchant is correctly passed
const finalBody =
body && method === 'POST'
? {
apiKey: body.apiKey || apiKey,
...body,
// Ensure apiKey is set if not already present
apiKey: body.apiKey || (token && token !== 'zibal' ? token : apiKey),
}
: body;
@@ -509,7 +549,12 @@ export class ZibalService implements IPaymentGateway {
});
const data = await res.json();
this.logger.log(`[Zibal Core API Response] ${endpoint} -> ${JSON.stringify(data)}`);
return data as T;
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
this.logger.error(`[Zibal Core API Error] ${endpoint} failed: ${msg}`);
throw err;
} finally {
clearTimeout(timeoutId);
}
+10
View File
@@ -100,6 +100,16 @@ export const CANONICAL_ALIASES: Record<string, string[]> = {
charity_round_step: ['CHARITY_ROUND_STEP', 'charityRoundStep'],
charityRoundStep: ['CHARITY_ROUND_STEP', 'charity_round_step'],
// Zibal Gateway & Tokens
ZIBAL_EBANK_TOKEN: ['zibal_ebank_token', 'ZIBAL_API_KEY', 'zibal_api_key', 'ZIBAL_TOKEN', 'zibal_token'],
zibal_ebank_token: ['ZIBAL_EBANK_TOKEN', 'ZIBAL_API_KEY', 'zibal_api_key', 'ZIBAL_TOKEN', 'zibal_token'],
ZIBAL_API_KEY: ['ZIBAL_EBANK_TOKEN', 'zibal_ebank_token', 'zibal_api_key', 'ZIBAL_TOKEN', 'zibal_token'],
zibal_api_key: ['ZIBAL_EBANK_TOKEN', 'zibal_ebank_token', 'ZIBAL_API_KEY', 'ZIBAL_TOKEN', 'zibal_token'],
ZIBAL_MERCHANT: ['zibal_merchant', 'merchantId', 'merchant'],
zibal_merchant: ['ZIBAL_MERCHANT', 'merchantId', 'merchant'],
ZIBAL_EBANK_ACCOUNT_ID: ['zibal_ebank_account_id', 'accountId'],
zibal_ebank_account_id: ['ZIBAL_EBANK_ACCOUNT_ID', 'accountId'],
// Modes
MAINTENANCE_MODE: ['maintenance_mode'],
maintenance_mode: ['MAINTENANCE_MODE'],