import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/Pages/PageWidgets/CoinPurchase/MomoType.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:teso/util/SizeConfig.dart'; import 'package:teso/util/consts.dart'; import 'package:http/http.dart' as http; class WithdrawalPage extends StatefulWidget { final String momonumber; final String amount; final String provider; const WithdrawalPage({Key key, this.momonumber, this.amount, this.provider}) : super(key: key); @override _WithdrawalPageState createState() => _WithdrawalPageState(); } class _WithdrawalPageState extends State { String processing = "Processing request, please wait"; bool confirmBox = false; String recipientName = ""; String currency; String image; submit() async { String num = widget.momonumber; String provider = widget.provider; try { SharedPreferences prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register2 = paymentServer + "withdrawals/getAccountInfo/$provider/account/$num"; var client1 = await http.get(Uri.parse(register2), headers: requestHeaders); if (client1.statusCode == 200) { var parsedJson = json.decode(client1.body); if (parsedJson["status-text"] == "Success") { setState(() { confirmBox = true; recipientName = parsedJson["account-info"]; currency = parsedJson["options"][0]["currency"]; }); } else if (parsedJson["status"] == 4) { setState(() { confirmBox = false; processing = parsedJson["status-text"]; }); } else { setState(() { confirmBox = false; processing = "Request Failed, please try again later !!!"; }); } } } catch (e) {} if (!confirmBox) Future.delayed(Duration(seconds: 5), () => Navigator.pop(context)); } withdraw() async { setState(() { confirmBox = false; }); String num = widget.momonumber; String withdraw = widget.amount; String provider = widget.provider; try { SharedPreferences prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; Map body = { "provider": provider, "number": num, "amount": withdraw, }; var register2 = paymentServer + "withdrawals/cashout"; var client1 = await http.post( Uri.parse(register2), headers: requestHeaders, body: json.encode(body), ); if (client1.statusCode == 200) { if (client1.body == "Payment Made") { setState(() { confirmBox = false; processing = "Payment made successfully"; }); } else if (client1.body == "Payment Pending") { setState(() { confirmBox = false; processing = "Payment submitted successfully, " + "funds maybe transferred to the specified as soon as possible"; }); } } else { if (client1.body == "Insufficient Gold") { setState(() { confirmBox = false; processing = "Insufficient Gold coins, you need to have more than 9 gold coins to make a withdrawal"; }); } else { setState(() { confirmBox = false; processing = "Request Failed, please try again later !!!"; }); } } } catch (e) {} Provider.of(context, listen: false).getUserInformation(); if (!confirmBox) Future.delayed(Duration(seconds: 5), () => Navigator.pop(context)); } @override void initState() { switch (widget.provider) { case "VODAFONE": setState(() { image = "assets/images/Vodafone.png"; }); break; case "MTN": setState(() { image = "assets/images/MTN.png"; }); break; case "AIRTEL": setState(() { image = "assets/images/AirtelTigo.png"; }); break; } submit(); super.initState(); } select(s) {} @override Widget build(BuildContext context) { return Scaffold( body: Center( child: !confirmBox ? pendingCenter(context) : confirmCenter(context), ), ); } Widget pendingCenter(BuildContext context) { return Container( width: 200, height: 200, child: Column( children: [ Container( child: processing == "Processing request, please wait" ? CupertinoActivityIndicator( animating: true, radius: 15, ) : processing.contains("successfully") ? Icon( Icons.check_circle, color: Colors.green, size: SizeConfig.safeBlockHorizontal * 20, ) : Icon( Icons.error, color: Colors.red, size: SizeConfig.safeBlockHorizontal * 30, ), ), SizedBox( height: 10, ), Container( child: Text( processing, textAlign: TextAlign.center, style: TextStyle( fontSize: SizeConfig.safeBlockHorizontal * 3.8, ), ), ), ], ), ); } Widget confirmCenter(BuildContext context) { SizeConfig().init(context); return Container( width: SizeConfig.safeBlockHorizontal * 72, height: SizeConfig.safeBlockVertical * 42, child: Container( width: SizeConfig.safeBlockHorizontal * 72, height: SizeConfig.safeBlockVertical * 42, child: Column( children: [ SizedBox( height: 20, ), buildType(context, image, widget.provider, select, widget.provider), SizedBox( height: 20, ), Container( child: Text( "You requested to withdraw $currency ${widget.amount} to $recipientName on ${widget.momonumber}", textAlign: TextAlign.center, style: TextStyle( fontSize: SizeConfig.safeBlockHorizontal * 3.8, ), ), ), SizedBox( height: 20, ), new Row( mainAxisAlignment: MainAxisAlignment.center, children: [ new ElevatedButton( style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), ), primary: Color(0xFFb90222), padding: new EdgeInsets.all(10.0), ), onPressed: () => Navigator.pop(context), child: new Container( height: 20.0, width: 70.0, alignment: Alignment.center, decoration: new BoxDecoration( color: Color(0xFFb90222), borderRadius: new BorderRadius.circular(60.0), ), child: new Text( "Cancel", style: new TextStyle(color: Colors.white), ), ), ), SizedBox( width: 10, ), new ElevatedButton( style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), ), primary: Colors.green, padding: new EdgeInsets.all(10.0), ), onPressed: withdraw, child: new Container( height: 20.0, width: 70.0, alignment: Alignment.center, decoration: new BoxDecoration( color: Colors.green, borderRadius: new BorderRadius.circular(60.0), ), child: new Text( "Confirm", style: new TextStyle(color: Colors.white), ), ), ), ], ) ], ), ), ); } }