This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
LocalSpend-Tracker/lib/common/apifunctions/submit_receipt_api.dart

81 lines
2.1 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
2019-05-21 14:30:14 +00:00
import 'package:shared_preferences/shared_preferences.dart';
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
import 'package:local_spend/model/json/login_model.dart';
class Receipt {
var amount = "";
var time = "";
var street = "";
var category = "";
var organisationName = "";
var postcode = "";
var recurring = "";
var town = "";
var essential = "false";
}
Future<LoginModel> submitReceiptAPI(
BuildContext context, Receipt receipt) async {
//var apiUrl = ConfigWrapper.of(context).apiKey;
2019-05-21 14:30:14 +00:00
final url = "https://dev.peartrade.org/api/upload";
SharedPreferences preferences = await SharedPreferences.getInstance();
Map<String, String> body = {
'transaction_type' : "3",
'transaction_value': receipt.amount,
'purchase_time': receipt.time,
2019-08-12 10:54:13 +00:00
// 'category': receipt.category,
// 'category': receipt.category,
'essential': receipt.essential,
'organisation_name': receipt.organisationName,
'recurring': receipt.recurring,
'street_name': receipt.street,
'postcode': receipt.postcode,
'town': receipt.town,
2019-05-21 14:30:14 +00:00
'session_key': preferences.get('LastToken'),
};
2019-07-16 11:09:10 +00:00
// debugPrint('$body');
2019-08-12 10:37:59 +00:00
debugPrint(json.encode(body));
final response = await http.post(
url,
body: json.encode(body),
);
2019-08-12 10:37:59 +00:00
debugPrint(response.body);
if (response.statusCode == 200) {
final responseJson = json.decode(response.body);
2019-08-12 10:37:59 +00:00
print(responseJson[0]);
showDialogSingleButton(
context,
responseJson[0] == "" ? responseJson[0] : "Upload Successful",
"Transaction successfully submitted to server",
"OK"
);
return LoginModel.fromJson(responseJson);
} else {
final responseJson = json.decode(response.body);
2019-05-10 11:51:11 +00:00
showDialogSingleButton(
context,
"Unable to Submit Receipt",
// "You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
"Message from server: " + responseJson[1],
"OK");
return null;
}
}