YSPayTypeView.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_alipay/flutter_alipay.dart';
  4. import 'package:ysairplane2/tools/YSNetWorking.dart';
  5. import 'package:ysairplane2/tools/YSPayKeybord.dart';
  6. import 'package:ysairplane2/tools/YSTools.dart';
  7. import 'package:fluwx/fluwx.dart' as fluwx;
  8. import 'YSMoney.dart';
  9. import 'YSSetPayPass.dart';
  10. class YSPayTypeView extends StatefulWidget {
  11. final Map orderDict;
  12. final VoidCallback postSuccess;
  13. const YSPayTypeView({Key key, this.orderDict, this.postSuccess}) : super(key: key);
  14. @override
  15. _YSPayTypeViewState createState() => _YSPayTypeViewState();
  16. }
  17. class _YSPayTypeViewState extends State<YSPayTypeView> {
  18. int _selected = 0;
  19. @override
  20. void initState() {
  21. fluwx.weChatResponseEventHandler.listen((res) {
  22. if (res is fluwx.WeChatPaymentResponse) {
  23. if(res.isSuccessful==true){
  24. widget.postSuccess();
  25. }
  26. }
  27. });
  28. super.initState();
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return Container(
  33. height: hsp(600),
  34. decoration: BoxDecoration(
  35. color: Colors.white,
  36. borderRadius: BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
  37. ),
  38. child: Column(
  39. children: [
  40. Container(
  41. height: hsp(99),
  42. decoration: BoxDecoration(
  43. border: Border(bottom: BorderSide(color: Color(0xFFD8D8D8),width: hsp(1)))
  44. ),
  45. padding: EdgeInsets.only(left: hsp(30),right: hsp(30)),
  46. alignment: Alignment.center,
  47. child: Text('支付方式',style: TextStyle(fontSize: zsp(30),color: Color(0xFF4A4A4A),fontWeight: FontWeight.bold),),
  48. ),
  49. Container(
  50. height: hsp(400),
  51. child: ListView.separated(
  52. itemBuilder: (context,index){
  53. return GestureDetector(
  54. onTap: (){
  55. setState(() {
  56. _selected = index;
  57. });
  58. },
  59. behavior: HitTestBehavior.opaque,
  60. child: Container(
  61. height: hsp(99),
  62. color: Colors.white,
  63. padding: EdgeInsets.only(left: hsp(30),right: hsp(30)),
  64. child: Row(
  65. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  66. children: [
  67. Row(
  68. children: [
  69. Container(
  70. height: hsp(50),
  71. width: hsp(50),
  72. margin: EdgeInsets.only(right: hsp(25)),
  73. child: Image.asset(index==0?'lib/images/余额.png':index==1?'lib/images/wexinLogo.png':'lib/images/aliLogo.png'),
  74. ),
  75. Text(index==0?'余额':index==1?'微信支付':'支付宝',style: TextStyle(fontSize: zsp(30),color: Color(0xFF343434),)),
  76. ],
  77. ),
  78. Icon(index==_selected?Icons.check_circle:Icons.radio_button_unchecked,size: hsp(40),
  79. color: Color(index==_selected?0xFF007EFF:0xFF9B9B9B),)
  80. ],
  81. ),
  82. ),
  83. );
  84. },
  85. separatorBuilder: (context,index){
  86. return Divider(height: hsp(1),thickness: hsp(1),color: Color(0xFFF1F2F3),);
  87. },
  88. itemCount: 3,
  89. padding: EdgeInsets.all(0),
  90. physics: NeverScrollableScrollPhysics(),
  91. shrinkWrap: true,
  92. )
  93. ,
  94. ),
  95. GestureDetector(
  96. onTap: (){
  97. _postOrderData();
  98. },
  99. child: Container(
  100. height: hsp(100),
  101. color: Color(0xFFFD2A2E),
  102. alignment: Alignment.center,
  103. child: Text('确认支付',style: TextStyle(fontSize: zsp(30),color: Colors.white,fontWeight: FontWeight.bold),),
  104. ),
  105. )
  106. ],
  107. ),
  108. );
  109. }
  110. _showAlertView (int type) async{
  111. showCupertinoDialog(
  112. context: context,
  113. builder: (context) {
  114. return CupertinoAlertDialog(
  115. title: Text('提示\n'),
  116. content: Text(type==1?'未设置支付密码':'余额不足请充值'),
  117. actions: <Widget>[
  118. CupertinoDialogAction(child: Text('取消'),onPressed: (){Navigator.pop(context);},),
  119. CupertinoDialogAction(child: Text(type==1?'去设置':'去充值'),onPressed: (){
  120. Navigator.pop(context);
  121. Navigator.of(context).push(
  122. CupertinoPageRoute(
  123. builder: (context){
  124. return type==1?YSSetPayPass():YSMoney();
  125. }
  126. )
  127. );
  128. },),
  129. ],
  130. );
  131. }
  132. );
  133. }
  134. _postPayData(String pass) async{
  135. var dict = await ysRequestHttpEncrypt(context,type: requestType.post,api: '/app/applets/balance/balancePay',
  136. parameter: {'orderSn':widget.orderDict['orderSn'],'price':widget.orderDict['price'],'password': pass},isLoading: true,isToken: true);
  137. if(dict!=null){
  138. if(dict==''){
  139. widget.postSuccess();
  140. }else if(dict['code']==804){
  141. _showAlertView(2);
  142. }else if(dict['code']==801){
  143. _showAlertView(1);
  144. }
  145. }
  146. }
  147. _postOrderData() async{
  148. if(_selected==0){
  149. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/balance/wx/checkBalance',parameter: {},isLoading: true,isToken: true,
  150. refresh: (){});
  151. if(dict!=null){
  152. if(dict['code']==801){
  153. _showAlertView(1);
  154. }else{
  155. showDialog(context: context,builder: (context){
  156. return AlertDialog(
  157. contentPadding: EdgeInsets.all(0),
  158. actionsPadding: EdgeInsets.all(0),
  159. buttonPadding: EdgeInsets.all(0),
  160. insetPadding: EdgeInsets.all(0),
  161. titlePadding: EdgeInsets.all(0),
  162. backgroundColor: Colors.transparent,
  163. content: ysPayKeybord(context,(value){
  164. _postPayData(value);
  165. },'${widget.orderDict['orderName']}',
  166. '${widget.orderDict['price']}')
  167. );
  168. });
  169. }
  170. }
  171. }else{
  172. Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/applets/createPayOrder',
  173. parameter: {'id':widget.orderDict['orderSn'],'way':_selected+1,'type':2},isLoading: true,
  174. isToken: true,refresh: (){});
  175. if(dict!=null){
  176. if(_selected==2){
  177. var payResult = await FlutterAlipay.pay('${dict['data']}');
  178. String payResultStatus = payResult.resultStatus;
  179. if (payResultStatus == "9000") {
  180. print('=================================订单支付成功');
  181. widget.postSuccess();
  182. } else if (payResultStatus == "6001") {
  183. print('=================================支付取消');
  184. } else if (payResultStatus == "4000") {
  185. print('=================================支付失败');
  186. } else if (payResultStatus == "8000") {
  187. print('=================================等待支付');
  188. } else if (payResultStatus == "6002") {
  189. print('=================================无网络');
  190. } else if (payResultStatus == "5000") {
  191. print('=================================重复支付');
  192. } else {
  193. print('=================================支付失败,未知错误');
  194. }
  195. }else if(_selected==1){
  196. Map result = dict['data'];
  197. fluwx.payWithWeChat(
  198. appId: result['appId'].toString(),
  199. partnerId: result['partnerId'].toString(),
  200. prepayId: result['prepayId'].toString(),
  201. packageValue: result['package'].toString(),
  202. nonceStr: result['nonceStr'].toString(),
  203. timeStamp: int.parse('${result['timeStamp']}'),
  204. sign: result['paySign'].toString(),
  205. ).then((data) {});
  206. }
  207. }
  208. }
  209. }
  210. }