Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

545 wiersze
19 KiB

  1. import 'dart:io';
  2. import 'package:chat/data/UserData.dart';
  3. import 'package:chat/data/constants.dart';
  4. import 'package:chat/generated/i18n.dart';
  5. import 'package:chat/home/MoneyPage.dart';
  6. import 'package:chat/models/money_change.dart';
  7. import 'package:chat/utils/app_navigator.dart';
  8. import 'package:dio/dio.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart';
  11. import 'package:provider/provider.dart';
  12. import 'package:url_launcher/url_launcher.dart';
  13. import 'HttpUtil.dart';
  14. import 'LoadingDialog.dart';
  15. import 'ShadowButton.dart';
  16. import 'TokenMgr.dart';
  17. import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx;
  18. class PayWayContent extends StatefulWidget {
  19. @required
  20. final goodsId;
  21. @required
  22. final money;
  23. PayWayContent({Key key, this.goodsId, this.money}) : super(key: key);
  24. PayWayContentState createState() => PayWayContentState();
  25. }
  26. class PayWay {
  27. static const int WxApp = 0; //微信app支付
  28. static const int ApplePay = 1; //苹果支付
  29. static const int GooglePay = 2; //谷歌支付
  30. static const int WxH5 = 3; //微信h5支付
  31. static const int Paypal = 4; //paypal支付
  32. static const int ShareToOther = 5; //微信他人代付
  33. }
  34. // enum PayWay { wx, apple, google, h5, paypal, shareToOther }
  35. class PayWayContentState extends State<PayWayContent> {
  36. int payWay = PayWay.WxH5;
  37. Widget _buildTip() {
  38. return Container(
  39. margin: EdgeInsets.only(bottom: 5),
  40. padding: EdgeInsets.only(top: 10, left: 10, right: 10),
  41. decoration:
  42. BoxDecoration(border: Border(bottom: Constants.GreyBorderSide)),
  43. child: Column(
  44. children: <Widget>[
  45. InkWell(
  46. onTap: () {
  47. Navigator.of(context).pop();
  48. },
  49. child: Align(
  50. alignment: Alignment.centerRight,
  51. child: Icon(
  52. IconData(
  53. 0xe679,
  54. fontFamily: 'iconfont',
  55. ),
  56. size: 16,
  57. color: const Color(0xFF969696),
  58. )),
  59. ),
  60. Container(
  61. alignment: Alignment.center,
  62. margin: EdgeInsets.only(bottom: 16),
  63. child: Text(
  64. I18n.of(context).confirm_pay,
  65. textScaleFactor: 1.0,
  66. style: TextStyle(fontSize: 15),
  67. ),
  68. ),
  69. Container(
  70. margin: EdgeInsets.only(bottom: 19),
  71. alignment: Alignment.center,
  72. child: Row(
  73. mainAxisAlignment: MainAxisAlignment.center,
  74. children: <Widget>[
  75. Text(
  76. UserData().language == LanguageType.SimplifiedChinese
  77. ? '¥'
  78. : '₫',
  79. textScaleFactor: 1.0,
  80. style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700),
  81. ),
  82. Text(
  83. ' ${widget.money}',
  84. textScaleFactor: 1.0,
  85. style: TextStyle(fontSize: 28, fontWeight: FontWeight.w700),
  86. )
  87. ],
  88. ),
  89. )
  90. ],
  91. ),
  92. );
  93. }
  94. Widget _buildPayWayItem(img, name, radio, callback) {
  95. return InkWell(
  96. onTap: callback,
  97. child: Container(
  98. child: Row(
  99. children: <Widget>[
  100. ClipRRect(
  101. borderRadius: BorderRadius.all(Radius.circular(5)),
  102. child: Image.asset(
  103. img,
  104. height: 26,
  105. )),
  106. Padding(
  107. padding: EdgeInsets.only(left: 15),
  108. child: Text(
  109. name,
  110. textScaleFactor: 1.0,
  111. style: TextStyle(fontSize: 14),
  112. ),
  113. ),
  114. Expanded(
  115. child:
  116. Container(alignment: Alignment.centerRight, child: radio),
  117. )
  118. ],
  119. ),
  120. ));
  121. }
  122. void addGoods() async {
  123. var data = {
  124. "userid": UserData().basicInfo.userId,
  125. "moneytype": 1,
  126. 'gid': widget.goodsId,
  127. "payway": payWay,
  128. };
  129. data['sign'] = TokenMgr().getSign(data);
  130. data['isenable'] = Constants.isSandbox ? 0 : 1;
  131. Response res = await HttpUtil().post('order/add/goods', data: data);
  132. Map resData = res.data;
  133. print('resData: $resData');
  134. if (resData['code'] == 0) {
  135. Navigator.of(context).pop();
  136. var url = resData['data']['Weburl'];
  137. UserData().isBannerStatus = true;
  138. if (payWay == PayWay.Paypal) {
  139. AppNavigator.pushWebview(context, url);
  140. } else if (payWay == PayWay.WxH5) {
  141. if (await canLaunch(url)) {
  142. await launch(url);
  143. } else {
  144. throw 'Could not launch $url';
  145. }
  146. } else if (payWay == PayWay.ShareToOther) {
  147. shareToOther(url);
  148. }
  149. setState(() {});
  150. }
  151. // var data = {
  152. // "userid": UserData().basicInfo.userId,
  153. // "moneytype": 1,
  154. // 'gid': widget.goodsId,
  155. // "payway": payWay,
  156. // };
  157. // data['sign'] = TokenMgr().getSign(data);
  158. // Response res = await HttpUtil().post('order/add/goods', data: data);
  159. // Map resData = res.data;
  160. // if (resData['code'] == 0) {
  161. // fluwx.pay(
  162. // appId: resData['data']['WxPayData']['appid'],
  163. // partnerId: resData['data']['WxPayData']['mch_id'],
  164. // prepayId: resData['data']['WxPayData']['prepay_id'],
  165. // packageValue: 'Sign=WXPay',
  166. // nonceStr: resData['data']['WxPayData']['nonce_str'],
  167. // timeStamp: int.parse(resData['data']['WxPayData']['timestamp']),
  168. // sign: resData['data']['WxPayData']['sign'],
  169. // );
  170. // setState(() {});
  171. // }
  172. }
  173. //确定按钮
  174. Widget _buildConfirmButton() {
  175. Text text = new Text(I18n.of(context).determine,
  176. textScaleFactor: 1.0,
  177. style: TextStyle(
  178. fontSize: 18,
  179. color: Colors.white,
  180. textBaseline: TextBaseline.ideographic));
  181. LinearGradient gradientColor = new LinearGradient(colors: <Color>[
  182. Colors.blueAccent,
  183. Colors.blueAccent,
  184. ]);
  185. // callback() {
  186. // if (payWay == PayWay.WxH5 || payWay == PayWay.Paypal) {
  187. // addGoods();
  188. // }
  189. // if (payWay == PayWay.ShareToOther) {
  190. // shareToOther();
  191. // }
  192. // }
  193. return new Container(
  194. margin: EdgeInsets.only(top: 10, bottom: 10),
  195. alignment: Alignment.center,
  196. height: 44,
  197. width: 250,
  198. child: ShadowButton().builder(gradientColor, text, addGoods),
  199. );
  200. }
  201. shareToOther(url) {
  202. fluwx.WeChatShareWebPageModel model = fluwx.WeChatShareWebPageModel(
  203. scene: fluwx.WeChatScene.SESSION,
  204. webPage: url,
  205. title: '点我代付[${widget.money}元]',
  206. thumbnail:
  207. "http://datasm.chengyouhd.com/Upload/default/icon120.png",
  208. description: '[${UserData().basicInfo.nickName}]正在购买H币,点击帮我支付吧');
  209. fluwx.share(model);
  210. }
  211. Widget _buildPayWay() {
  212. Widget wxButton = Radio(
  213. value: PayWay.WxH5,
  214. groupValue: payWay, //当value和groupValue一致的时候则选中
  215. onChanged: (T) {
  216. setState(() {
  217. payWay = PayWay.WxH5;
  218. });
  219. });
  220. Widget shareToOther = Radio(
  221. value: PayWay.ShareToOther,
  222. groupValue: payWay, //当value和groupValue一致的时候则选中
  223. onChanged: (T) {
  224. setState(() {
  225. payWay = PayWay.ShareToOther;
  226. });
  227. });
  228. // Widget paypalButton = Radio(
  229. // value: PayWay.Paypal,
  230. // groupValue: payWay, //当value和groupValue一致的时候则选中
  231. // onChanged: (T) {
  232. // setState(() {
  233. // payWay = PayWay.Paypal;
  234. // });
  235. // });
  236. return Container(
  237. margin: EdgeInsets.only(left: 30, right: 20),
  238. child: Column(
  239. children: <Widget>[
  240. _buildPayWayItem('assets/images/login/vip_wx.png',
  241. I18n.of(context).wechat_pay, wxButton, () {
  242. setState(() {
  243. payWay = PayWay.WxApp;
  244. });
  245. }),
  246. UserData().deviceLanguage == LanguageType.SimplifiedChinese ||
  247. UserData().deviceLanguage == LanguageType.TraditionalChinese
  248. ? _buildPayWayItem('assets/images/login/vip_other.png',
  249. I18n.of(context).other_pay, shareToOther, () {
  250. setState(() {
  251. payWay = PayWay.ShareToOther;
  252. });
  253. })
  254. : Container(),
  255. //todo paypal
  256. // _buildPayWayItem('assets/images/login/vip_wx.png',
  257. // 'Paypal', paypalButton, () {
  258. // setState(() {
  259. // payWay = PayWay.paypal.index;
  260. // });
  261. // }),
  262. _buildConfirmButton(),
  263. ],
  264. ),
  265. );
  266. }
  267. @override
  268. void initState() {
  269. super.initState();
  270. }
  271. @override
  272. Widget build(BuildContext context) {
  273. return new Material(
  274. type: MaterialType.transparency,
  275. child: new Center(
  276. child: new SizedBox(
  277. width: 288.25,
  278. height: 286.6,
  279. child: new Container(
  280. decoration: ShapeDecoration(
  281. color: Color(0xffffffff),
  282. shape: RoundedRectangleBorder(
  283. borderRadius: BorderRadius.all(
  284. Radius.circular(8.0),
  285. ),
  286. ),
  287. ),
  288. child: new Column(
  289. children: <Widget>[
  290. _buildTip(),
  291. _buildPayWay(),
  292. ],
  293. ),
  294. ),
  295. ),
  296. ),
  297. );
  298. }
  299. }
  300. class ChargeMoney {
  301. static List goodsList = [];
  302. ///钱包-充值 item
  303. static Widget _buildItem(
  304. leftStr, rightStr, goodsId, appleProductId, BuildContext context) {
  305. return Container(
  306. height: 55,
  307. margin: EdgeInsets.only(left: 15, right: 15),
  308. decoration: BoxDecoration(
  309. border: Border(
  310. bottom: BorderSide(width: 1, color: Constants.DividerLineColor))),
  311. child: Row(
  312. children: <Widget>[
  313. Container(
  314. alignment: Alignment.center,
  315. decoration: BoxDecoration(
  316. borderRadius: BorderRadius.all(Radius.circular(20))),
  317. height: 30,
  318. width: 30,
  319. child: Image.asset('assets/images/coin.png'),
  320. ),
  321. Padding(
  322. padding: EdgeInsets.only(left: 15),
  323. child: Text(
  324. '$leftStr',
  325. textScaleFactor: 1.0,
  326. style: TextStyle(fontSize: 14.41),
  327. ),
  328. ),
  329. Expanded(
  330. child: Align(
  331. alignment: Alignment.centerRight,
  332. child: InkWell(
  333. highlightColor: Colors.transparent,
  334. radius: 0,
  335. onTap: () {
  336. if (Platform.isIOS && UserData().bannerZF == 1) {
  337. /// 是ios并且苹果内购开关打开
  338. MoneyPageState.currentGoodsId = goodsId.toString();
  339. // showToast('goodsId:$goodsId money:$rightStr appleProductId: $appleProductId');
  340. ChargeMoney.requestPurchase(
  341. appleProductId.toString(), context);
  342. } else {
  343. showDialog(
  344. context: context,
  345. builder: (BuildContext context) {
  346. return PayWayContent(
  347. goodsId: goodsId,
  348. money: rightStr,
  349. );
  350. });
  351. }
  352. },
  353. child: Container(
  354. alignment: Alignment.center,
  355. decoration: BoxDecoration(
  356. color: Constants.BlueTextColor,
  357. boxShadow: [
  358. BoxShadow(
  359. color: Constants.BlueTextColor.withOpacity(0.3),
  360. offset: Offset(0, 1.5),
  361. blurRadius: 4.5),
  362. ],
  363. borderRadius: BorderRadius.all(
  364. Radius.circular(Constants.LittleButtonRadius))),
  365. width: 75.85,
  366. height: 28.1,
  367. child: Text(
  368. UserData().language == LanguageType.SimplifiedChinese
  369. ? '¥$rightStr'
  370. : '₫ ${(rightStr / 1000).toInt()}K',
  371. textScaleFactor: 1.0,
  372. textAlign: TextAlign.center,
  373. style: TextStyle(color: Colors.white, fontSize: 13.45),
  374. ),
  375. ),
  376. ))),
  377. ],
  378. ),
  379. );
  380. }
  381. ///苹果必须先拉取商品列表FlutterInappPurchase.instance.getProducts ,才可以进行购买
  382. static void initIAP(List<String> iosProductIdList) async {
  383. print('initIAP ---初始化ios 内购商品列表-start');
  384. List<IAPItem> iapItem =
  385. await FlutterInappPurchase.instance.getProducts(iosProductIdList);
  386. print('initIAP ---初始化ios 内购商品列表-end');
  387. print('iosProductIdList length ${iapItem.length}');
  388. }
  389. static void showChargeSheet(BuildContext context, callback) {
  390. HttpUtil().getMoneyInfo(context, (data) {
  391. goodsList = data;
  392. List<Widget> list = [];
  393. List<String> iosProductIdList = [];
  394. list.addAll(goodsList.map((data) {
  395. print('iosProductIdList data #### : data');
  396. iosProductIdList.add(data['Id'].toString());
  397. return _buildItem(
  398. data['Amount'], data['Price'], data['Gid'], data['Id'], context);
  399. }).toList());
  400. if (Platform.isIOS) {
  401. initIAP(iosProductIdList);
  402. ///初始化ios内购
  403. }
  404. showModalBottomSheet(
  405. context: context,
  406. builder: (BuildContext context) {
  407. return StatefulBuilder(
  408. builder: (BuildContext context, setBottomSheetState) {
  409. return SafeArea(
  410. child: Container(
  411. height: 390,
  412. child: Stack(
  413. children: <Widget>[
  414. Container(
  415. margin: EdgeInsets.only(top: 55),
  416. child: ListView(
  417. children: list,
  418. ),
  419. ),
  420. Container(
  421. height: 55,
  422. child: Row(
  423. children: <Widget>[
  424. Padding(
  425. padding: EdgeInsets.only(left: 20),
  426. child: Text(
  427. I18n.of(context).recharge,
  428. textScaleFactor: 1.0,
  429. style: TextStyle(fontSize: 18),
  430. ),
  431. ),
  432. Expanded(
  433. child: Align(
  434. alignment: Alignment.centerRight,
  435. child: Text(
  436. I18n.of(context).mask_coin +
  437. I18n.of(context)
  438. .available_balance
  439. .replaceFirst('/s1', ': '),
  440. textScaleFactor: 1.0,
  441. style: TextStyle(
  442. color: const Color(0xFF595959),
  443. fontSize: 13),
  444. ),
  445. ),
  446. ),
  447. Container(
  448. alignment: Alignment.centerRight,
  449. child: Consumer<MoneyChangeProvider>(
  450. builder: (context,
  451. MoneyChangeProvider counter, child) =>
  452. Text(
  453. counter.money.toString(),
  454. textScaleFactor: 1.0,
  455. style: TextStyle(
  456. color: Colors.red, fontSize: 15),
  457. ),
  458. ),
  459. ),
  460. Container(
  461. height: 15,
  462. margin: EdgeInsets.only(left: 12, right: 10),
  463. decoration: BoxDecoration(
  464. border: Border(
  465. left: BorderSide(
  466. color: const Color(0xFF595959)))),
  467. ),
  468. InkWell(
  469. onTap: () {
  470. HttpUtil().getWealth(context, (data) {
  471. callback();
  472. Provider.of<MoneyChangeProvider>(context)
  473. .initMoney(data['CoinValue']);
  474. setBottomSheetState(() {});
  475. });
  476. },
  477. child: Container(
  478. padding: EdgeInsets.only(right: 6),
  479. child: Icon(
  480. Icons.refresh,
  481. size: 20,
  482. ),
  483. )),
  484. ],
  485. ),
  486. ),
  487. ],
  488. )),
  489. );
  490. },
  491. );
  492. },
  493. ).then((val) {});
  494. });
  495. }
  496. /// ios内购 请求支付
  497. static void requestPurchase(String productId, BuildContext context) async {
  498. showDialog(
  499. context: context,
  500. barrierDismissible: false,
  501. builder: (BuildContext context) {
  502. return LoadingDialog();
  503. });
  504. print('_requestPurchase >>>>>>>>>> -- $productId');
  505. // productId = '1488435000';
  506. await FlutterInappPurchase.instance
  507. .requestPurchase(productId)
  508. .then((value) {
  509. print('支付结果: $value');
  510. }).catchError((e) {
  511. print('支付出错结果::: $e');
  512. });
  513. }
  514. }