2026-05-01 19:45:30 +03:30
|
|
|
import { inject, Injectable } from '@angular/core';
|
2026-04-27 21:53:11 +03:30
|
|
|
import { environment } from 'src/environments/environment';
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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-06 22:01:20 +03:30
|
|
|
if (!this.host) return false;
|
2026-05-01 19:45:30 +03:30
|
|
|
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-01 19:45:30 +03:30
|
|
|
this.toastService.info({ text: 'در حال پردازش پرداخت...', life: 3000 });
|
|
|
|
|
const fn = this.host?.pay;
|
|
|
|
|
if (typeof fn !== 'function') {
|
2026-05-06 22:01:20 +03:30
|
|
|
this.toastService.error({ text: 'متاسفانه پرداخت امکانپذیر نیست.', life: 3000 });
|
|
|
|
|
return { success: false, error: 'متاسفانه پرداخت امکانپذیر نیست.' };
|
2026-05-01 19:45:30 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-06 22:01:20 +03:30
|
|
|
fn(request.amount, request.id);
|
2026-05-01 19:45:30 +03:30
|
|
|
|
2026-05-06 22:01:20 +03:30
|
|
|
return { success: true };
|
2026-05-01 19:45:30 +03:30
|
|
|
|
2026-05-06 22:01:20 +03:30
|
|
|
// return { success: true, data: parsed ?? raw };
|
2026-05-01 19:45:30 +03:30
|
|
|
} catch (error) {
|
|
|
|
|
return { success: false, error: (error as Error).message };
|
|
|
|
|
}
|
2026-04-27 21:53:11 +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-06 22:01:20 +03:30
|
|
|
private invokePrint(payload: INativePrintRequest): INativeBridgeResult {
|
2026-05-01 19:45:30 +03:30
|
|
|
const fn = this.host?.print;
|
2026-04-27 21:53:11 +03:30
|
|
|
if (typeof fn !== 'function') {
|
2026-05-06 22:01:20 +03:30
|
|
|
return { success: false, error: 'متاسفانه چاپ امکانپذیر نیست.' };
|
2026-04-27 21:53:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-06 22:01:20 +03:30
|
|
|
fn(payload);
|
2026-04-27 21:53:11 +03:30
|
|
|
|
2026-05-06 22:01:20 +03:30
|
|
|
return { success: true };
|
2026-04-27 21:53:11 +03:30
|
|
|
} catch (error) {
|
2026-05-06 22:01:20 +03:30
|
|
|
return { success: false, error: 'متاسفانه چاپ امکانپذیر نیست.' };
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|