247 lines
8.1 KiB
Dart
247 lines
8.1 KiB
Dart
import 'dart:convert';
|
|
import 'package:aman_kassa_flutter/core/base/base_service.dart';
|
|
import 'package:aman_kassa_flutter/core/locator.dart';
|
|
import 'package:aman_kassa_flutter/core/models/aman_dao.dart';
|
|
import 'package:aman_kassa_flutter/core/models/close_day_data.dart';
|
|
import 'package:aman_kassa_flutter/core/models/halyk_post_session.dart';
|
|
import 'package:aman_kassa_flutter/core/models/transaction_item.dart';
|
|
import 'package:aman_kassa_flutter/core/services/ApiService.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:intent/action.dart';
|
|
import 'package:intent/extra.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:intent/intent.dart';
|
|
|
|
|
|
|
|
class BankService extends BaseService {
|
|
final ApiService _api = locator<ApiService>();
|
|
static const String _url = 'http://195.200.74.83:5000';
|
|
final MethodChannel _channel = MethodChannel('channel:com.amanKassa/bank');
|
|
|
|
|
|
Future<int> version() async {
|
|
String result;
|
|
try {
|
|
result = await _channel.invokeMethod('version');
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
result = '0';
|
|
}
|
|
log.i(result);
|
|
return int.parse(result) ?? 0;
|
|
}
|
|
|
|
Future<HalykPosSession> renewToken({String token, String login, String password}) async {
|
|
HalykPosSession result;
|
|
try {
|
|
result = await _api.halykPosToken(token, login, password);
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
Future<bool> init({String token}) async {
|
|
|
|
//Intent intent = new Intent ();
|
|
// intent.setComponent (new ComponentName ("ru.m4bank.softpos", "ru.m4bank.feature.externalapplication.ExternalApplicationActivity"));
|
|
// intent.setFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
// intent.putExtra (OPERATION_TYPE_KEY, operationType);
|
|
// operationType - see the section "List of supported operations" intent.putExtra (INPUT_DATA_KEY, inputJsonData);
|
|
// inputJsonData - json string containing input data for the operation startActivityForResult (intent, INTENT_REQUEST_CODE);
|
|
// INTENT_REQUEST_CODE - the value specified by the user API is used to filter the received result in onActivityResult
|
|
// List<String> result;
|
|
// try {
|
|
// Intent intent = Intent()
|
|
// ..setAction(Action.ACTION_ALL_APPS)
|
|
// ..putExtra("ru.m4bank.softpos", "ru.m4bank.feature.externalapplication.ExternalApplicationActivity")
|
|
// ..putExtra("ru.m4bank.ExternalApplication.OperationTypeKey", "OPERATIONS_LIST");
|
|
//
|
|
//
|
|
// result = await intent.startActivityForResult();
|
|
// } catch (e, stack) {
|
|
// log.e("BankService", e, stack);
|
|
// }
|
|
// log.i(result);
|
|
//
|
|
// return true;
|
|
try {
|
|
String response = await _channel.invokeMethod('init', <String, dynamic>{
|
|
'serverUrl': _url,
|
|
'token': token
|
|
});
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao.success;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> connect() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("connection");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao.success;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> currency() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("currency");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao.success;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> permissions() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("permissions");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao.success;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<AmanDao> auth({String login, String password}) async {
|
|
try {
|
|
String response = await _channel.invokeMethod("auth",
|
|
<String, dynamic>{'login': login, 'password': password});
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return new AmanDao(msg: 'Ошибка авторизации');
|
|
}
|
|
}
|
|
|
|
Future<AmanDao> closeDay() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("closeDay");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return new AmanDao(msg: 'Ошибка при закрытии дня');
|
|
}
|
|
}
|
|
|
|
Future<AmanDao> pay({double amount}) async {
|
|
try {
|
|
|
|
double total = amount * 100;
|
|
log.i('total: $total, ${total.toInt()}');
|
|
String response = await _channel.invokeMethod("pay", <String, dynamic>{'amount': total.toInt()});
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return new AmanDao(msg: 'Ошибка оплаты');
|
|
}
|
|
}
|
|
|
|
Future<AmanDao> findTransaction({int transactionNumber, String authorizationCode}) async {
|
|
try {
|
|
log.i('transactionNumber: $transactionNumber');
|
|
String response = await _channel.invokeMethod("findTransaction", <String, dynamic>{'transactionNumber': transactionNumber, 'authorizationCode': authorizationCode});
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return new AmanDao(msg: 'Ошибка при поиске транзакии банка');
|
|
}
|
|
}
|
|
|
|
Future<AmanDao> refund() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("refund");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return new AmanDao(msg: 'Ошибка при возврате');
|
|
}
|
|
}
|
|
|
|
Future<bool> shutdown() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("shutdown");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao.success;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> cancel() async {
|
|
try {
|
|
String response = await _channel.invokeMethod("cancel");
|
|
AmanDao dao = AmanDao.fromJson(json.decode(response));
|
|
log.i('${dao.success} - ${dao.msg}');
|
|
return dao.success;
|
|
} catch (e, stack) {
|
|
log.e("BankService", e, stack);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
CloseDayData closeDayDataConvert(dynamic rows) {
|
|
final DateFormat formatter = DateFormat('dd.MM.yyyy');
|
|
final DateTime now = DateTime.now();
|
|
final String formatted = formatter.format(now);
|
|
List<TransactionItem> items = (rows as List).map((e) => TransactionItem.fromJson(e)).toList();
|
|
num totalAmount = 0;
|
|
int totalCount = 0;
|
|
num paymentAmount = 0;
|
|
int paymentCount = 0;
|
|
num refundAmount = 0;
|
|
int refundCount = 0;
|
|
|
|
for(TransactionItem item in items) {
|
|
if(item.transactionType == 'PAYMENT') {
|
|
paymentCount++;
|
|
paymentAmount += ( item.amount / 100 );
|
|
totalAmount += ( item.amount / 100 );
|
|
} else if(item.transactionType == 'REFUND') {
|
|
refundCount++;
|
|
refundAmount += ( item.amount / 100 );
|
|
totalAmount -= ( item.amount / 100 );
|
|
}
|
|
totalCount++;
|
|
}
|
|
|
|
CloseDayData closeDayData = new CloseDayData(
|
|
items: items,
|
|
title: 'Отчет POS от $formatted',
|
|
totalAmount: totalAmount, totalCount: totalCount,
|
|
paymentAmount: paymentAmount, paymentCount: paymentCount,
|
|
refundAmount: refundAmount, refundCount: refundCount,
|
|
);
|
|
return closeDayData;
|
|
}
|
|
|
|
}
|