2024-04-08 15:05:50 +05:30
|
|
|
import 'dart:convert';
|
2024-06-18 10:36:24 +05:30
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2024-04-08 15:05:50 +05:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:scgateway_flutter_plugin/scgateway_flutter_plugin.dart';
|
|
|
|
|
|
|
|
|
|
import '../../Utils/api_urls.dart';
|
2024-06-18 10:36:24 +05:30
|
|
|
import '../../Utils/base_manager.dart';
|
|
|
|
|
import '../../data/network/network_api_services.dart';
|
2024-04-08 15:05:50 +05:30
|
|
|
import '../../model/SmallCaseModel/broker_account_model.dart';
|
|
|
|
|
|
|
|
|
|
// void openDashboardPage(BuildContext context) {
|
|
|
|
|
// Navigator.pushReplacement(
|
|
|
|
|
// context, MaterialPageRoute(builder: ((context) => PortfolioMainUI())));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// void openEquityPage(BuildContext context, Map<String, dynamic> holdings) {
|
|
|
|
|
// Navigator.push(
|
|
|
|
|
// context,
|
|
|
|
|
// MaterialPageRoute(
|
|
|
|
|
// builder: ((context) => Equityinner(holdings: holdings))));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
//broker account table
|
|
|
|
|
|
|
|
|
|
//delete broker account
|
|
|
|
|
Future<bool> deleteBrokerAccount(int id) async {
|
|
|
|
|
final response = await http.Client()
|
2024-06-18 10:36:24 +05:30
|
|
|
.get(Uri.parse('${ApiUrls.base}api/delete_brokerage_account/$id'));
|
2024-04-08 15:05:50 +05:30
|
|
|
if (response.statusCode == 200) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//post broker account
|
|
|
|
|
Future<bool> postBrokerAccount({
|
|
|
|
|
required String brokerName,
|
|
|
|
|
required String authToken,
|
|
|
|
|
required String txnId,
|
|
|
|
|
}) async {
|
2024-06-18 10:36:24 +05:30
|
|
|
final response = await NetworkApiServices().postApi(
|
|
|
|
|
FormData.fromMap({
|
2024-04-08 15:05:50 +05:30
|
|
|
"broker_name": brokerName,
|
|
|
|
|
"auth_token": authToken,
|
|
|
|
|
"transaction_id": txnId,
|
2024-06-18 10:36:24 +05:30
|
|
|
}),
|
|
|
|
|
'${ApiUrls.base}add-broker-account',
|
2024-04-08 15:05:50 +05:30
|
|
|
);
|
2024-06-18 10:36:24 +05:30
|
|
|
|
|
|
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
|
|
|
Map<String, dynamic> responseData =
|
|
|
|
|
Map<String, dynamic>.from(response.data);
|
|
|
|
|
if (responseData['status'] == "success") {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-08 15:05:50 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 10:36:24 +05:30
|
|
|
//fetch broker accounts
|
|
|
|
|
Future<List<Data>> fetchBrokerAccounts() async {
|
|
|
|
|
final response = await NetworkApiServices().getApi(
|
|
|
|
|
'${ApiUrls.base}get-broker-account-data',
|
|
|
|
|
);
|
|
|
|
|
// log(response.data.toString());
|
|
|
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
|
|
|
Map<String, dynamic> responseData =
|
|
|
|
|
Map<String, dynamic>.from(response.data);
|
|
|
|
|
if (responseData['status'] == "success") {
|
|
|
|
|
BrokerAccountModel brokerAccountModel =
|
|
|
|
|
BrokerAccountModel.fromJson(responseData);
|
|
|
|
|
|
|
|
|
|
return brokerAccountModel.data!;
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 15:05:50 +05:30
|
|
|
Future<String> fetchAuthToken() async {
|
2024-06-18 10:36:24 +05:30
|
|
|
final response = await NetworkApiServices().getApi(
|
|
|
|
|
'${ApiUrls.base}get-small-case-auth-token',
|
2024-04-08 15:05:50 +05:30
|
|
|
);
|
2024-06-18 10:36:24 +05:30
|
|
|
// log(response.data.toString());
|
|
|
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
|
|
|
Map<String, dynamic> responseData =
|
|
|
|
|
Map<String, dynamic>.from(response.data);
|
|
|
|
|
if (responseData['status'] == "success") {
|
|
|
|
|
return response.data['data'];
|
|
|
|
|
} else {
|
|
|
|
|
return "Failed";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "Failed";
|
2024-04-08 15:05:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> fetchBrokerConnectTxnId({required String authToken}) async {
|
2024-06-18 10:36:24 +05:30
|
|
|
final response = await NetworkApiServices().postApi(
|
|
|
|
|
FormData.fromMap({"auth_token": authToken}),
|
|
|
|
|
'${ApiUrls.base}get-transaction-id',
|
2024-04-08 15:05:50 +05:30
|
|
|
);
|
2024-06-18 10:36:24 +05:30
|
|
|
log(response.data.toString());
|
|
|
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
|
|
|
Map<String, dynamic> responseData =
|
|
|
|
|
Map<String, dynamic>.from(response.data);
|
|
|
|
|
if (responseData['status'] == "success") {
|
|
|
|
|
return response.data['data']["data"]
|
|
|
|
|
['transactionId']; // jsonDecode(response.body)
|
|
|
|
|
} else {
|
|
|
|
|
return "Failed";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "Failed";
|
2024-04-08 15:05:50 +05:30
|
|
|
}
|
|
|
|
|
|
2024-06-18 10:36:24 +05:30
|
|
|
Future<String> fetchHoldingsImportTxnId(String authToken) async {
|
|
|
|
|
log(authToken);
|
|
|
|
|
final response = await NetworkApiServices().postApi(
|
|
|
|
|
FormData.fromMap({"auth_token": authToken}),
|
|
|
|
|
'${ApiUrls.base}create-transaction-holdings-import',
|
2024-04-08 15:05:50 +05:30
|
|
|
);
|
2024-06-18 10:36:24 +05:30
|
|
|
log(response.data.toString());
|
|
|
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
|
|
|
Map<String, dynamic> responseData =
|
|
|
|
|
Map<String, dynamic>.from(response.data);
|
|
|
|
|
if (responseData['status'] == "success" &&
|
|
|
|
|
!responseData.toString().contains(
|
|
|
|
|
"{status: error, status_code: 500, message: Something went wrong.}")) {
|
|
|
|
|
return response.data['data']["data"]
|
|
|
|
|
['transactionId']; // jsonDecode(response.body)
|
|
|
|
|
} else {
|
|
|
|
|
return "Failed";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "Failed";
|
2024-04-08 15:05:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> fetchHoldings(String authToken) async {
|
2024-06-18 10:36:24 +05:30
|
|
|
final response = await NetworkApiServices().postApi(
|
|
|
|
|
FormData.fromMap({"auth_token": authToken}),
|
|
|
|
|
'${ApiUrls.base}fetch-holdings',
|
2024-04-08 15:05:50 +05:30
|
|
|
);
|
2024-06-18 10:36:24 +05:30
|
|
|
log(response.data.toString());
|
|
|
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
|
|
|
Map<String, dynamic> responseData =
|
|
|
|
|
Map<String, dynamic>.from(response.data);
|
|
|
|
|
if (responseData['status'] == "success") {
|
|
|
|
|
return responseData;
|
|
|
|
|
} else {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
2024-04-08 15:05:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> fetchStocksOrderTxnId(String authToken, String body) async {
|
|
|
|
|
var response = await http.post(Uri.parse(
|
2024-06-18 10:36:24 +05:30
|
|
|
'${ApiUrls.base}api/create_post_transaction_stock_order?body=$body&auth_token=$authToken'));
|
2024-04-08 15:05:50 +05:30
|
|
|
var txnId = jsonDecode(response.body)['data']['transactionId'];
|
|
|
|
|
return txnId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum TradeType {
|
|
|
|
|
BUY,
|
|
|
|
|
SELL,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loginNTrade(String ticker, int quantity, TradeType tradeType) {
|
|
|
|
|
fetchAuthToken().then((fetchedAuthToken) {
|
|
|
|
|
// debugPrint("fetchedAuthToken $fetchedAuthToken");
|
|
|
|
|
fetchBrokerConnectTxnId(authToken: fetchedAuthToken).then(
|
|
|
|
|
(txnId) => ScgatewayFlutterPlugin.initGateway(fetchedAuthToken).then(
|
|
|
|
|
(value) => ScgatewayFlutterPlugin.triggerGatewayTransaction(
|
|
|
|
|
txnId,
|
|
|
|
|
).then(
|
|
|
|
|
(loginRes) {
|
|
|
|
|
if (loginRes != null) {
|
|
|
|
|
var data = jsonDecode(loginRes)['data'];
|
|
|
|
|
if (data != null) {
|
|
|
|
|
String authToken = jsonDecode(data)['smallcaseAuthToken'];
|
|
|
|
|
String brokerName = jsonDecode(data)['broker'];
|
|
|
|
|
String txnId = jsonDecode(data)['transactionId'];
|
|
|
|
|
String body =
|
|
|
|
|
'{"intent":"TRANSACTION","orderConfig":{"type":"SECURITIES","securities":[{"ticker":"$ticker","quantity":"$quantity","type":"${tradeType.name}"},{"ticker":"RELIANCE","quantity":1,"type":"BUY"}]}}';
|
|
|
|
|
fetchStocksOrderTxnId(authToken, body).then(
|
|
|
|
|
(stocksOrderTxnId) => ScgatewayFlutterPlugin
|
|
|
|
|
.triggerGatewayTransaction(stocksOrderTxnId)
|
|
|
|
|
.then(
|
|
|
|
|
(value) => debugPrint("Stocks Order res $value")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|