d6aa165592
- Refactor PosPaymentBridgeAbstract to enforce PosPaymentResult type for emitPaymentResultForTest method. - Simplify PosPaymentBridgeService by removing commented-out code and improving error handling. - Replace PosPaymentFormDialogComponent with SharedInvoicePaymentFormDialog in root.component.html for better payment handling. - Enhance root.component.ts to manage payment form visibility and submission logic. - Update light-bottomsheet.component.html for improved styling. - Add new return form features including item removal and total price calculation in returnForm components. - Introduce new payment form dialog for handling invoice payments with detailed structure and validation. - Implement payment handling logic in sale-invoice-single-view component to support correction payments. - Ensure proper integration of payment components in the shared components index.
159 lines
4.7 KiB
TypeScript
159 lines
4.7 KiB
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { Maybe } from '../models';
|
|
import { ToastService } from './toast.service';
|
|
|
|
interface INativeBridgeHost {
|
|
pay?: (amount: number, id: Maybe<string>) => unknown;
|
|
print?: (payload: INativePrintRequest) => unknown;
|
|
isEnabled?: () => boolean;
|
|
getUUID?: () => Maybe<string>;
|
|
}
|
|
|
|
export interface INativePayRequest {
|
|
amount: number;
|
|
id: Maybe<string>;
|
|
}
|
|
|
|
export interface INativePrintRequest {
|
|
title: string;
|
|
items: {
|
|
label: string;
|
|
value: string;
|
|
}[];
|
|
}
|
|
|
|
export interface INativeBridgeResult<T = unknown> {
|
|
success: boolean;
|
|
data?: T;
|
|
error?: string;
|
|
}
|
|
|
|
export interface INativeBridgeDeviceInfo {
|
|
deviceName: string;
|
|
androidId: string;
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class NativeBridgeService {
|
|
private readonly toastService = inject(ToastService);
|
|
|
|
private get host(): INativeBridgeHost | undefined {
|
|
const globalWindow = window as unknown as {
|
|
NativeBridge?: INativeBridgeHost;
|
|
};
|
|
|
|
return globalWindow.NativeBridge;
|
|
}
|
|
|
|
isEnabled(): boolean {
|
|
// @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;
|
|
}
|
|
|
|
canPay(): boolean {
|
|
return this.isEnabled() && typeof this.host?.pay === 'function';
|
|
}
|
|
|
|
canPrint(): boolean {
|
|
return this.isEnabled() && typeof this.host?.print === 'function';
|
|
}
|
|
|
|
pay(request: INativePayRequest): any {
|
|
// if (request.amount <= 1_000) {
|
|
// const errorMessage = 'برای مقادیر زیر ۱۰ هزار ریال، پرداخت ممکن نیست.';
|
|
// this.toastService.warn({ text: errorMessage, life: 3000 });
|
|
// return {
|
|
// success: false,
|
|
// error: errorMessage,
|
|
// };
|
|
// }
|
|
this.toastService.info({ text: 'در حال پردازش پرداخت...' });
|
|
try {
|
|
// @ts-ignore
|
|
window.NativeBridge.pay(request.amount, request.id || '');
|
|
return { success: true };
|
|
} catch (error) {
|
|
// this.toastService.info({ text: (error as Error).message });
|
|
return { success: false, error: (error as Error).message };
|
|
}
|
|
// 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) {
|
|
// this.toastService.info({ text: (error as Error).message, });
|
|
// return { success: false, error: (error as Error).message };
|
|
// }
|
|
}
|
|
|
|
print(request: INativePrintRequest[]): INativeBridgeResult {
|
|
return this.invokePrint(request);
|
|
}
|
|
|
|
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 };
|
|
}
|
|
}
|
|
|
|
private invokePrint(payload: INativePrintRequest[]): INativeBridgeResult {
|
|
if (!this.isEnabled() || !this.canPrint()) {
|
|
this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' });
|
|
return { success: false, error: 'متاسفانه ارتباط با چاپگر برقرار نیست.' };
|
|
}
|
|
try {
|
|
this.toastService.info({ text: 'در حال چاپ ...' });
|
|
// @ts-ignore
|
|
window.NativeBridge.print(JSON.stringify(payload));
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
this.toastService.warn({ text: 'متاسفانه ارتباط با چاپگر برقرار نیست.' });
|
|
return { success: false, error: (error as Error).message };
|
|
}
|
|
}
|
|
|
|
getNativeDeviceId(): Maybe<string> {
|
|
return typeof this.host?.getUUID === 'function' ? this.host.getUUID!() : null;
|
|
}
|
|
|
|
private tryParse(value: unknown): unknown {
|
|
if (typeof value !== 'string') return value;
|
|
try {
|
|
return JSON.parse(value);
|
|
} catch {
|
|
return value;
|
|
}
|
|
}
|
|
}
|