ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
1
2
|
import 'dart:io';
|
4224b3f8
吴启风
feat:支付详情页ui
|
3
|
import 'package:bloc/bloc.dart';
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
4
|
import 'package:flutter/cupertino.dart';
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
5
6
7
|
import 'package:fluwx/fluwx.dart';
import 'package:tobias/tobias.dart';
import 'package:wow_english/generated/json/base/json_convert_content.dart';
|
4224b3f8
吴启风
feat:支付详情页ui
|
8
9
|
import 'package:wow_english/models/product_entity.dart';
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
10
11
12
|
import '../../common/request/dao/shop_dao.dart';
import '../../common/request/exception.dart';
import '../../utils/loading.dart';
|
4224b3f8
吴启风
feat:支付详情页ui
|
13
|
import '../../utils/log_util.dart';
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
14
|
import '../../utils/toast_util.dart';
|
4224b3f8
吴启风
feat:支付详情页ui
|
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import 'event.dart';
import 'state.dart';
class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> {
ProductEntity? _productData;
ProductEntity? get productData => _productData;
PaymentChannel _curPaymentChannel = PaymentChannel.wechatPay;
PaymentChannel get curPaymentChannel => _curPaymentChannel;
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
28
29
30
31
32
|
Fluwx? fluwx;
Function(WeChatResponse)? wxPayResponseListener;
bool _isWxPayListenerInitialized = false;
|
4224b3f8
吴启风
feat:支付详情页ui
|
33
|
|
07599105
吴启风
feat:商品列表请求&路由
|
34
|
ShoppingBloc(this._productData) : super(ShoppingState().init()) {
|
4224b3f8
吴启风
feat:支付详情页ui
|
35
36
37
|
//页面初始化时刻
on<InitEvent>(_init);
on<ChangePaymentChannelEvent>(_changePaymentChannel);
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
38
|
on<DoPayEvent>(_startPay);
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
39
40
41
|
on<WxPaySuccessEvent>((event, emit) {
emit(PaySuccessState());
});
|
4224b3f8
吴启风
feat:支付详情页ui
|
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
}
void _init(InitEvent event, Emitter<ShoppingState> emit) async {
//处理一些初始化操作,然后刷新界面
emit(state.clone());
}
/// 更换支付渠道
void _changePaymentChannel(ChangePaymentChannelEvent event,
Emitter<ShoppingState> emitter) async {
Log.d("_curPaymentChannel=$_curPaymentChannel, event.paymentChannel=${event.paymentChannel}");
if (_curPaymentChannel != event.paymentChannel) {
_curPaymentChannel = event.paymentChannel;
emitter(PaymentChannelChangeState());
}
}
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
59
60
|
void _startPay(DoPayEvent event,
Emitter<ShoppingState> emitter) async {
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
61
62
63
64
65
66
67
68
|
//如果event.productEntity为空,中断流程并toast提示
if (event.productEntity == null) {
showToast("商品信息为空");
return;
}
final productEntitySafely = event.productEntity!;
try {
await loading(() async {
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
69
70
|
final Map<String, dynamic>? orderInfo = await ShopDao.createOrder(productEntitySafely);
final String? orderNo = orderInfo?.getOrNull("orderNo");
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
71
72
73
74
|
if (orderNo == null) {
showToast("订单创建失败");
return;
}
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
75
76
|
if (event.paymentChannel == PaymentChannel.wechatPay) {
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
77
78
79
|
if (_isWxPayListenerInitialized == false) {
_isWxPayListenerInitialized = true;
fluwx = Fluwx();
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
80
|
await fluwx?.registerApi(appId: "wx365e5a79956a450a",
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
81
82
|
universalLink: "https://app-api.wowenglish.com.cn/app/");
wxPayResponseListener = (WeChatResponse response) {
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
83
|
debugPrint("WqfPay wxPayResponseListener $response");
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
84
85
|
if (response is WeChatPaymentResponse) {
if (response.errCode == 0) {
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
86
|
debugPrint("WqfPay wxPayResponseListener response=${response.errCode}");
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
87
88
|
showToast("支付成功");
// Log.d("emitter isDone=${emitter.isDone}");
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
89
90
|
/// 报错!_isCompleted emit was called after an event handler completed normally
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
91
|
// emitter(PaySuccessState());
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
92
93
|
add(WxPaySuccessEvent());
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
} else {
showToast("支付失败");
}
}
};
fluwx?.addSubscriber(wxPayResponseListener!);
}
bool installed = await fluwx?.isWeChatInstalled == true;
if (!installed) {
// 未安装微信,请前去下载
String name = Platform.isIOS ? "AppStore" : "应用商店";
showToast("您未安装微信,请前往$name下载~");
return;
}
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
110
111
|
final Map<String, dynamic>? wxPayOrderInfo = await ShopDao.getWxPayToken(orderNo);
if (wxPayOrderInfo == null) {
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
showToast("微信订单创建失败");
return;
}
await fluwx?.pay(
which: Payment(
appId: wxPayOrderInfo['appId'].toString(),
partnerId: wxPayOrderInfo['partnerId'].toString(),
prepayId: wxPayOrderInfo['prepayId'].toString(),
packageValue: wxPayOrderInfo['packageValue'].toString(),
nonceStr: wxPayOrderInfo['nonceStr'].toString(),
timestamp: int.parse(wxPayOrderInfo['timeStamp']),
sign: wxPayOrderInfo['sign'].toString(),
));
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
126
|
} else {
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
127
128
129
|
final Map<String, dynamic>? aliPayOrderInfo = await ShopDao.getAliPayToken(orderNo);
debugPrint("aliPayOrderInfo=$aliPayOrderInfo type=${aliPayOrderInfo?.runtimeType}");
final String? aliPayInfo = aliPayOrderInfo?.getOrNull("token");
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
130
131
132
133
|
if (aliPayInfo == null) {
showToast("支付宝订单创建失败");
return;
}
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
134
|
debugPrint("aliPayInfo=$aliPayInfo");
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
135
136
137
|
///打印aliPayOrderInfo的type
Tobias tobias = Tobias();
final Map aliPayResult = await tobias.pay(aliPayInfo);
|
e0766888
吴启风
feat:解决微信支付异步回调里e...
|
138
|
debugPrint("aliPayResult=$aliPayResult");
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
139
140
141
|
// 判断resultStatus 为9000则代表支付成功
if (aliPayResult.getOrNull("resultStatus") == "9000") {
showToast("支付成功");
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
142
|
emitter(PaySuccessState());
|
2879454a
吴启风
feat:调通支付宝支付&游戏列表页
|
143
144
145
146
147
148
149
150
151
152
|
} else {
showToast("支付失败");
}
}
});
} catch (e) {
if (e is ApiException) {
showToast(e.message ?? '请求失败,请检查网络连接');
}
}
|
4224b3f8
吴启风
feat:支付详情页ui
|
153
|
}
|
ad37b653
吴启风
feat:1、完成微信支付;2、商...
|
154
155
156
157
158
159
|
void dispose() {
fluwx?.clearSubscribers();
// 释放资源的逻辑
debugPrint('BLoC disposed');
}
|
4224b3f8
吴启风
feat:支付详情页ui
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
}
enum PaymentChannel {
wechatPay(1, "微信支付"),
aliPay(2, "支付宝支付");
const PaymentChannel(this.payChannelType, this.payChannelName);
final int payChannelType;
final String payChannelName;
}
|