import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:provider/provider.dart'; import 'package:teso/Classes/API Clasess/UserFinance.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/Classes/API%20Clasess/SilverPurchaseRequest.dart'; import 'package:teso/Classes/Payload.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:teso/Notifications/NotificationPlugin.dart'; import 'package:teso/Pages/Sub_Pages/Payments/PaymentView.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:teso/util/consts.dart'; import 'package:http/http.dart' as http; class ProcessSilverPurchase extends StatefulWidget { final int silverAmount; final String method; final double cost; final TesoUser? user; const ProcessSilverPurchase({ Key? key, required this.user, required this.cost, required this.method, required this.silverAmount, }) : super(key: key); @override _ProcessSilverPurchaseState createState() => _ProcessSilverPurchaseState(); } class _ProcessSilverPurchaseState extends State { String processing = "Processing silver coin purchase"; Future purchaseCoin() async { try { if (widget.method != "goldcoins") { await Navigator.push( context, MaterialPageRoute( builder: (context) => PaymentView( selectedUrl: 'https://expresspaygh.com/payment_api_auto.php?token=99356106a8bbaa43c9.217546426106a8bbaa4417.9361359096566106a8bbaa&orderid=213', ), maintainState: true), ); } else { SharedPreferences prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso")!; Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; SilverPurchaseRequest request = new SilverPurchaseRequest(); request.coinamount = widget.silverAmount; request.amount = widget.cost; request.method = widget.method == "goldcoins" ? "gold" : "realcash"; var register = serverLocation + 'coins/purchase_silver'; var client = await http.post(Uri.parse(register), body: json.encode(request), headers: requestHeaders); Payload payload = new Payload(); payload.loadID = "TESN003"; payload.load1 = "SilverCoin Purchase"; if (client.statusCode == 200) { var posts = jsonDecode(client.body); UserFinance finance = UserFinance.fromJSON(posts); TesoUser user = widget.user!; user.gold = finance.gold.toString(); user.silver = finance.silver.toString(); setState(() { processing = "${request.coinamount} silver coins successfully purchased !!!"; }); Provider.of(context, listen: false).setUser(user); await notificationPlugin.showNotification( "Funds purchased", "You have successfully purchased ${request.coinamount} silver coins", payload.toString(), ); } else if (client.statusCode == 300) { await notificationPlugin.showNotification( "Insufficient Funds", "Unable to purchase silver coins due to insufficient funds", payload.toString(), ); setState(() { processing = "Insufficient funds to complete transaction"; }); } else { setState(() { processing = "An error occurred while processing purchase, please try again later"; }); } } } catch (e) { setState(() { processing = "An error occurred while processing purchase, please try again later"; }); } Future.delayed(Duration(seconds: 5), () => Navigator.pop(context)); } @override void initState() { super.initState(); purchaseCoin(); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Container( width: 200, height: 200, child: Column( children: [ Container( child: processing == "Processing silver coin purchase" ? CupertinoActivityIndicator( animating: true, radius: 15, ) : processing.contains("successfully") ? Icon( Icons.check_circle, color: Colors.green, ) : Icon( Icons.error, color: Colors.red, ), ), Container( child: Text(processing), ), ], ), ), ), ); } }