import 'dart:io'; import 'package:chat/data/UserData.dart'; import 'package:chat/data/constants.dart'; import 'package:chat/generated/i18n.dart'; import 'package:chat/home/MoneyPage.dart'; import 'package:chat/models/money_change.dart'; import 'package:chat/utils/app_navigator.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; import 'HttpUtil.dart'; import 'LoadingDialog.dart'; import 'ShadowButton.dart'; import 'TokenMgr.dart'; import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx; class PayWayContent extends StatefulWidget { @required final goodsId; @required final money; PayWayContent({Key key, this.goodsId, this.money}) : super(key: key); PayWayContentState createState() => PayWayContentState(); } class PayWay { static const int WxApp = 0; //微信app支付 static const int ApplePay = 1; //苹果支付 static const int GooglePay = 2; //谷歌支付 static const int WxH5 = 3; //微信h5支付 static const int Paypal = 4; //paypal支付 static const int ShareToOther = 5; //微信他人代付 } // enum PayWay { wx, apple, google, h5, paypal, shareToOther } class PayWayContentState extends State { int payWay = PayWay.WxH5; Widget _buildTip() { return Container( margin: EdgeInsets.only(bottom: 5), padding: EdgeInsets.only(top: 10, left: 10, right: 10), decoration: BoxDecoration(border: Border(bottom: Constants.GreyBorderSide)), child: Column( children: [ InkWell( onTap: () { Navigator.of(context).pop(); }, child: Align( alignment: Alignment.centerRight, child: Icon( IconData( 0xe679, fontFamily: 'iconfont', ), size: 16, color: const Color(0xFF969696), )), ), Container( alignment: Alignment.center, margin: EdgeInsets.only(bottom: 16), child: Text( I18n.of(context).confirm_pay, textScaleFactor: 1.0, style: TextStyle(fontSize: 15), ), ), Container( margin: EdgeInsets.only(bottom: 19), alignment: Alignment.center, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( UserData().language == LanguageType.SimplifiedChinese ? '¥' : '₫', textScaleFactor: 1.0, style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700), ), Text( ' ${widget.money}', textScaleFactor: 1.0, style: TextStyle(fontSize: 28, fontWeight: FontWeight.w700), ) ], ), ) ], ), ); } Widget _buildPayWayItem(img, name, radio, callback) { return InkWell( onTap: callback, child: Container( child: Row( children: [ ClipRRect( borderRadius: BorderRadius.all(Radius.circular(5)), child: Image.asset( img, height: 26, )), Padding( padding: EdgeInsets.only(left: 15), child: Text( name, textScaleFactor: 1.0, style: TextStyle(fontSize: 14), ), ), Expanded( child: Container(alignment: Alignment.centerRight, child: radio), ) ], ), )); } void addGoods() async { var data = { "userid": UserData().basicInfo.userId, "moneytype": 1, 'gid': widget.goodsId, "payway": payWay, }; data['sign'] = TokenMgr().getSign(data); data['isenable'] = Constants.isSandbox ? 0 : 1; Response res = await HttpUtil().post('order/add/goods', data: data); Map resData = res.data; print('resData: $resData'); if (resData['code'] == 0) { Navigator.of(context).pop(); var url = resData['data']['Weburl']; UserData().isBannerStatus = true; if (payWay == PayWay.Paypal) { AppNavigator.pushWebview(context, url); } else if (payWay == PayWay.WxH5) { if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } else if (payWay == PayWay.ShareToOther) { shareToOther(url); } setState(() {}); } // var data = { // "userid": UserData().basicInfo.userId, // "moneytype": 1, // 'gid': widget.goodsId, // "payway": payWay, // }; // data['sign'] = TokenMgr().getSign(data); // Response res = await HttpUtil().post('order/add/goods', data: data); // Map resData = res.data; // if (resData['code'] == 0) { // fluwx.pay( // appId: resData['data']['WxPayData']['appid'], // partnerId: resData['data']['WxPayData']['mch_id'], // prepayId: resData['data']['WxPayData']['prepay_id'], // packageValue: 'Sign=WXPay', // nonceStr: resData['data']['WxPayData']['nonce_str'], // timeStamp: int.parse(resData['data']['WxPayData']['timestamp']), // sign: resData['data']['WxPayData']['sign'], // ); // setState(() {}); // } } //确定按钮 Widget _buildConfirmButton() { Text text = new Text(I18n.of(context).determine, textScaleFactor: 1.0, style: TextStyle( fontSize: 18, color: Colors.white, textBaseline: TextBaseline.ideographic)); LinearGradient gradientColor = new LinearGradient(colors: [ Colors.blueAccent, Colors.blueAccent, ]); // callback() { // if (payWay == PayWay.WxH5 || payWay == PayWay.Paypal) { // addGoods(); // } // if (payWay == PayWay.ShareToOther) { // shareToOther(); // } // } return new Container( margin: EdgeInsets.only(top: 10, bottom: 10), alignment: Alignment.center, height: 44, width: 250, child: ShadowButton().builder(gradientColor, text, addGoods), ); } shareToOther(url) { fluwx.WeChatShareWebPageModel model = fluwx.WeChatShareWebPageModel( scene: fluwx.WeChatScene.SESSION, webPage: url, title: '点我代付[${widget.money}元]', thumbnail: "http://datasm.chengyouhd.com/Upload/default/icon120.png", description: '[${UserData().basicInfo.nickName}]正在购买H币,点击帮我支付吧'); fluwx.share(model); } Widget _buildPayWay() { Widget wxButton = Radio( value: PayWay.WxH5, groupValue: payWay, //当value和groupValue一致的时候则选中 onChanged: (T) { setState(() { payWay = PayWay.WxH5; }); }); Widget shareToOther = Radio( value: PayWay.ShareToOther, groupValue: payWay, //当value和groupValue一致的时候则选中 onChanged: (T) { setState(() { payWay = PayWay.ShareToOther; }); }); // Widget paypalButton = Radio( // value: PayWay.Paypal, // groupValue: payWay, //当value和groupValue一致的时候则选中 // onChanged: (T) { // setState(() { // payWay = PayWay.Paypal; // }); // }); return Container( margin: EdgeInsets.only(left: 30, right: 20), child: Column( children: [ _buildPayWayItem('assets/images/login/vip_wx.png', I18n.of(context).wechat_pay, wxButton, () { setState(() { payWay = PayWay.WxApp; }); }), UserData().deviceLanguage == LanguageType.SimplifiedChinese || UserData().deviceLanguage == LanguageType.TraditionalChinese ? _buildPayWayItem('assets/images/login/vip_other.png', I18n.of(context).other_pay, shareToOther, () { setState(() { payWay = PayWay.ShareToOther; }); }) : Container(), //todo paypal // _buildPayWayItem('assets/images/login/vip_wx.png', // 'Paypal', paypalButton, () { // setState(() { // payWay = PayWay.paypal.index; // }); // }), _buildConfirmButton(), ], ), ); } @override void initState() { super.initState(); } @override Widget build(BuildContext context) { return new Material( type: MaterialType.transparency, child: new Center( child: new SizedBox( width: 288.25, height: 286.6, child: new Container( decoration: ShapeDecoration( color: Color(0xffffffff), shape: RoundedRectangleBorder( borderRadius: BorderRadius.all( Radius.circular(8.0), ), ), ), child: new Column( children: [ _buildTip(), _buildPayWay(), ], ), ), ), ), ); } } class ChargeMoney { static List goodsList = []; ///钱包-充值 item static Widget _buildItem( leftStr, rightStr, goodsId, appleProductId, BuildContext context) { return Container( height: 55, margin: EdgeInsets.only(left: 15, right: 15), decoration: BoxDecoration( border: Border( bottom: BorderSide(width: 1, color: Constants.DividerLineColor))), child: Row( children: [ Container( alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(20))), height: 30, width: 30, child: Image.asset('assets/images/coin.png'), ), Padding( padding: EdgeInsets.only(left: 15), child: Text( '$leftStr', textScaleFactor: 1.0, style: TextStyle(fontSize: 14.41), ), ), Expanded( child: Align( alignment: Alignment.centerRight, child: InkWell( highlightColor: Colors.transparent, radius: 0, onTap: () { if (Platform.isIOS && UserData().bannerZF == 1) { /// 是ios并且苹果内购开关打开 MoneyPageState.currentGoodsId = goodsId.toString(); // showToast('goodsId:$goodsId money:$rightStr appleProductId: $appleProductId'); ChargeMoney.requestPurchase( appleProductId.toString(), context); } else { showDialog( context: context, builder: (BuildContext context) { return PayWayContent( goodsId: goodsId, money: rightStr, ); }); } }, child: Container( alignment: Alignment.center, decoration: BoxDecoration( color: Constants.BlueTextColor, boxShadow: [ BoxShadow( color: Constants.BlueTextColor.withOpacity(0.3), offset: Offset(0, 1.5), blurRadius: 4.5), ], borderRadius: BorderRadius.all( Radius.circular(Constants.LittleButtonRadius))), width: 75.85, height: 28.1, child: Text( UserData().language == LanguageType.SimplifiedChinese ? '¥$rightStr' : '₫ ${(rightStr / 1000).toInt()}K', textScaleFactor: 1.0, textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 13.45), ), ), ))), ], ), ); } ///苹果必须先拉取商品列表FlutterInappPurchase.instance.getProducts ,才可以进行购买 static void initIAP(List iosProductIdList) async { print('initIAP ---初始化ios 内购商品列表-start'); List iapItem = await FlutterInappPurchase.instance.getProducts(iosProductIdList); print('initIAP ---初始化ios 内购商品列表-end'); print('iosProductIdList length ${iapItem.length}'); } static void showChargeSheet(BuildContext context, callback) { HttpUtil().getMoneyInfo(context, (data) { goodsList = data; List list = []; List iosProductIdList = []; list.addAll(goodsList.map((data) { print('iosProductIdList data #### : data'); iosProductIdList.add(data['Id'].toString()); return _buildItem( data['Amount'], data['Price'], data['Gid'], data['Id'], context); }).toList()); if (Platform.isIOS) { initIAP(iosProductIdList); ///初始化ios内购 } showModalBottomSheet( context: context, builder: (BuildContext context) { return StatefulBuilder( builder: (BuildContext context, setBottomSheetState) { return SafeArea( child: Container( height: 390, child: Stack( children: [ Container( margin: EdgeInsets.only(top: 55), child: ListView( children: list, ), ), Container( height: 55, child: Row( children: [ Padding( padding: EdgeInsets.only(left: 20), child: Text( I18n.of(context).recharge, textScaleFactor: 1.0, style: TextStyle(fontSize: 18), ), ), Expanded( child: Align( alignment: Alignment.centerRight, child: Text( I18n.of(context).mask_coin + I18n.of(context) .available_balance .replaceFirst('/s1', ': '), textScaleFactor: 1.0, style: TextStyle( color: const Color(0xFF595959), fontSize: 13), ), ), ), Container( alignment: Alignment.centerRight, child: Consumer( builder: (context, MoneyChangeProvider counter, child) => Text( counter.money.toString(), textScaleFactor: 1.0, style: TextStyle( color: Colors.red, fontSize: 15), ), ), ), Container( height: 15, margin: EdgeInsets.only(left: 12, right: 10), decoration: BoxDecoration( border: Border( left: BorderSide( color: const Color(0xFF595959)))), ), InkWell( onTap: () { HttpUtil().getWealth(context, (data) { callback(); Provider.of(context) .initMoney(data['CoinValue']); setBottomSheetState(() {}); }); }, child: Container( padding: EdgeInsets.only(right: 6), child: Icon( Icons.refresh, size: 20, ), )), ], ), ), ], )), ); }, ); }, ).then((val) {}); }); } /// ios内购 请求支付 static void requestPurchase(String productId, BuildContext context) async { showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return LoadingDialog(); }); print('_requestPurchase >>>>>>>>>> -- $productId'); // productId = '1488435000'; await FlutterInappPurchase.instance .requestPurchase(productId) .then((value) { print('支付结果: $value'); }).catchError((e) { print('支付出错结果::: $e'); }); } }