2019-05-10 12:43:45 +01:00
import ' dart:async ' ;
import ' dart:convert ' ;
import ' package:flutter/material.dart ' ;
import ' package:http/http.dart ' as http ;
2019-05-21 15:30:14 +01:00
import ' package:shared_preferences/shared_preferences.dart ' ;
2019-05-10 12:43:45 +01:00
import ' package:local_spend/common/functions/show_dialog_single_button.dart ' ;
import ' package:local_spend/model/json/login_model.dart ' ;
import ' package:local_spend/config.dart ' ;
// debug
import ' package:flutter/foundation.dart ' ;
2019-07-05 15:34:39 +01:00
class Receipt {
var amount = " " ;
var time = " " ;
var street = " " ;
var category = " " ;
var organisationName = " " ;
var postcode = " " ;
var recurring = " " ;
var town = " " ;
var essential = " false " ;
}
2019-05-10 12:43:45 +01:00
Future < LoginModel > submitReceiptAPI (
2019-07-05 15:34:39 +01:00
BuildContext context , Receipt receipt ) async {
2019-05-10 12:43:45 +01:00
//var apiUrl = ConfigWrapper.of(context).apiKey;
2019-05-21 15:30:14 +01:00
final url = " https://dev.peartrade.org/api/upload " ;
SharedPreferences preferences = await SharedPreferences . getInstance ( ) ;
2019-05-10 12:43:45 +01:00
Map < String , String > body = {
2019-07-05 22:56:15 +01:00
' transaction_type ' : " 3 " ,
2019-07-05 15:34:39 +01:00
' transaction_value ' : receipt . amount ,
' purchase_time ' : receipt . time ,
' category ' : receipt . category ,
' essential ' : receipt . essential ,
' organisation_name ' : receipt . organisationName ,
' recurring ' : receipt . recurring ,
' street_name ' : receipt . street ,
' postcode ' : receipt . postcode ,
2019-07-05 22:56:15 +01:00
' town ' : receipt . town ,
2019-05-21 15:30:14 +01:00
' session_key ' : preferences . get ( ' LastToken ' ) ,
2019-05-10 12:43:45 +01:00
} ;
debugPrint ( ' $ body ' ) ;
2019-07-05 22:56:15 +01:00
debugPrint ( json . encode ( body ) ) ;
2019-05-10 12:43:45 +01:00
final response = await http . post (
url ,
body: json . encode ( body ) ,
) ;
debugPrint ( response . body ) ;
if ( response . statusCode = = 200 ) {
final responseJson = json . decode ( response . body ) ;
return LoginModel . fromJson ( responseJson ) ;
} else {
final responseJson = json . decode ( response . body ) ;
2019-05-10 12:51:11 +01:00
2019-05-10 12:43:45 +01:00
showDialogSingleButton (
context ,
" Unable to Submit Receipt " ,
2019-07-05 22:56:15 +01:00
// "You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
responseJson . toString ( ) ,
2019-05-10 12:43:45 +01:00
" OK " ) ;
return null ;
}
}