2026-05-01 19:45:30 +03:30
|
|
|
import { inject, Injectable } from '@angular/core';
|
2026-04-30 16:27:42 +03:30
|
|
|
import { Maybe } from '../models';
|
2026-05-01 19:45:30 +03:30
|
|
|
import { ToastService } from './toast.service';
|
2026-04-27 21:53:11 +03:30
|
|
|
|
|
|
|
|
interface INativeBridgeHost {
|
2026-05-06 22:01:20 +03:30
|
|
|
pay?: (amount: number, id: Maybe<string>) => unknown;
|
|
|
|
|
print?: (payload: INativePrintRequest) => unknown;
|
2026-05-01 19:45:30 +03:30
|
|
|
isEnabled?: () => boolean;
|
2026-05-06 22:01:20 +03:30
|
|
|
getUUID?: () => Maybe<string>;
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface INativePayRequest {
|
|
|
|
|
amount: number;
|
2026-04-30 16:27:42 +03:30
|
|
|
id: Maybe<string>;
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface INativePrintRequest {
|
2026-05-06 22:01:20 +03:30
|
|
|
title: string;
|
|
|
|
|
items: {
|
|
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}[];
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface INativeBridgeResult<T = unknown> {
|
|
|
|
|
success: boolean;
|
|
|
|
|
data?: T;
|
|
|
|
|
error?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 09:44:30 +03:30
|
|
|
export interface INativeBridgeDeviceInfo {
|
|
|
|
|
deviceName: string;
|
|
|
|
|
androidId: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 21:53:11 +03:30
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
|
export class NativeBridgeService {
|
2026-05-01 19:45:30 +03:30
|
|
|
private readonly toastService = inject(ToastService);
|
|
|
|
|
|
2026-04-27 21:53:11 +03:30
|
|
|
private get host(): INativeBridgeHost | undefined {
|
|
|
|
|
const globalWindow = window as unknown as {
|
2026-05-06 22:01:20 +03:30
|
|
|
NativeBridge?: INativeBridgeHost;
|
2026-04-27 21:53:11 +03:30
|
|
|
};
|
|
|
|
|
|
2026-05-06 22:01:20 +03:30
|
|
|
return globalWindow.NativeBridge;
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isEnabled(): boolean {
|
2026-05-07 23:28:12 +03:30
|
|
|
// @ts-ignore
|
|
|
|
|
return !!window.NativeBridge;
|
|
|
|
|
// if (window.NativeBridge) {
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
// if (!this.host) return false;
|
|
|
|
|
// const hostEnabled = this.host?.isEnabled;
|
|
|
|
|
// if (typeof hostEnabled === 'function') {
|
|
|
|
|
// try {
|
|
|
|
|
// return !!hostEnabled();
|
|
|
|
|
// } catch {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return !!environment.enableNativeBridge && !!this.host;
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canPay(): boolean {
|
|
|
|
|
return this.isEnabled() && typeof this.host?.pay === 'function';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canPrint(): boolean {
|
|
|
|
|
return this.isEnabled() && typeof this.host?.print === 'function';
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 22:01:20 +03:30
|
|
|
pay(request: INativePayRequest): any {
|
2026-05-07 23:28:12 +03:30
|
|
|
if (request.amount <= 10_000) {
|
|
|
|
|
const errorMessage = 'برای مقادیر زیر ۱۰۰ هزار ریال، پرداخت ممکن نیست.';
|
2026-05-08 18:08:57 +03:30
|
|
|
this.toastService.warn({ text: errorMessage, life: 3000 });
|
2026-05-07 23:28:12 +03:30
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: errorMessage,
|
|
|
|
|
};
|
2026-05-01 19:45:30 +03:30
|
|
|
}
|
2026-05-08 18:08:57 +03:30
|
|
|
this.toastService.info({ text: 'در حال پردازش پرداخت...' });
|
2026-05-01 19:45:30 +03:30
|
|
|
try {
|
2026-05-07 23:28:12 +03:30
|
|
|
// @ts-ignore
|
|
|
|
|
window.NativeBridge.pay(request.amount, request.id || '');
|
2026-05-06 22:01:20 +03:30
|
|
|
return { success: true };
|
2026-05-01 19:45:30 +03:30
|
|
|
} catch (error) {
|
2026-05-08 18:08:57 +03:30
|
|
|
this.toastService.info({ text: (error as Error).message });
|
2026-05-01 19:45:30 +03:30
|
|
|
return { success: false, error: (error as Error).message };
|
|
|
|
|
}
|
2026-05-07 23:28:12 +03:30
|
|
|
// const fn = window.NativeBridge.pay(123, 'test');
|
|
|
|
|
// if (typeof fn !== 'function') {
|
|
|
|
|
// this.toastService.error({ text: 'متاسفانه پرداخت امکانپذیر نیست.', life: 3000 });
|
|
|
|
|
// return { success: false, error: 'متاسفانه پرداخت امکانپذیر نیست.' };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// fn(123, 'test');
|
|
|
|
|
// // fn(request.amount, request.id || '');
|
|
|
|
|
|
|
|
|
|
// return { success: true };
|
|
|
|
|
|
|
|
|
|
// // return { success: true, data: parsed ?? raw };
|
|
|
|
|
// } catch (error) {
|
2026-05-08 18:08:57 +03:30
|
|
|
// this.toastService.info({ text: (error as Error).message, });
|
2026-05-07 23:28:12 +03:30
|
|
|
// return { success: false, error: (error as Error).message };
|
|
|
|
|
// }
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
2026-05-12 19:58:53 +03:30
|
|
|
print(request: INativePrintRequest[]): INativeBridgeResult {
|
2026-05-01 19:45:30 +03:30
|
|
|
return this.invokePrint(request);
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
2026-05-10 09:44:30 +03:30
|
|
|
async getDeviceInfo(): Promise<INativeBridgeResult<INativeBridgeDeviceInfo>> {
|
|
|
|
|
if (!this.isEnabled()) {
|
|
|
|
|
return { success: false, error: 'متاسفانه ارتباط با دستگاه برقرار نیست.' };
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const deviceInfo = await window.NativeBridge.deviceInfo();
|
|
|
|
|
return { success: true, data: JSON.parse(deviceInfo) };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return { success: false, error: (error as Error).message };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 19:58:53 +03:30
|
|
|
private invokePrint(payload: INativePrintRequest[]): INativeBridgeResult {
|
2026-05-08 18:08:57 +03:30
|
|
|
if (!this.isEnabled() || !this.canPrint()) {
|
|
|
|
|
this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' });
|
|
|
|
|
return { success: false, error: 'متاسفانه ارتباط با چاپگر برقرار نیست.' };
|
|
|
|
|
}
|
2026-04-27 21:53:11 +03:30
|
|
|
try {
|
2026-05-07 23:28:12 +03:30
|
|
|
// @ts-ignore
|
|
|
|
|
window.NativeBridge.print(JSON.stringify([payload]));
|
2026-05-06 22:01:20 +03:30
|
|
|
return { success: true };
|
2026-04-27 21:53:11 +03:30
|
|
|
} catch (error) {
|
2026-05-08 18:08:57 +03:30
|
|
|
this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' });
|
2026-05-07 23:28:12 +03:30
|
|
|
return { success: false, error: (error as Error).message };
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 22:01:20 +03:30
|
|
|
getNativeDeviceId(): Maybe<string> {
|
|
|
|
|
return typeof this.host?.getUUID === 'function' ? this.host.getUUID!() : null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 21:53:11 +03:30
|
|
|
private tryParse(value: unknown): unknown {
|
|
|
|
|
if (typeof value !== 'string') return value;
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(value);
|
|
|
|
|
} catch {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|