首先,大家可以看下flutter的一些微信库
fluwx: ^2.0.8+3
运行一下,flutter pub get
首先在iOS项目下配置一些设置:
1、在info.plist中加入如下配置
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>weixin</string>
<key>CFBundleURLSchemes</key>
<array>
<string>Your App ID</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
2、在Flutter项目下可以创建一个WeChatPayHelper的类,用来管理微信支付
class WeChatPayHelper{
//微信支付
static void wechatPay(TaoXiOrderWeChatPayModel taoXiOrderWeChatPayModel) {
fluwx.payWithWeChat(
appId: taoXiOrderWeChatPayModel.result.appId,
partnerId: taoXiOrderWeChatPayModel.result.partnerId,
prepayId: taoXiOrderWeChatPayModel.result.prepayId,
packageValue: taoXiOrderWeChatPayModel.result.pPackage,
nonceStr: taoXiOrderWeChatPayModel.result.nonceString,
timeStamp: int.parse(taoXiOrderWeChatPayModel.result.timeStamp),
sign: taoXiOrderWeChatPayModel.result.sign).then((value) {
print("微信支付唤醒${value}");
});
}
}
其中,套系支付的TaoXiOrderWeChatPayModel类,可以直接拿走用
class TaoXiOrderWeChatPayModel {
Result result;
TaoXiOrderWeChatPayModel({this.result});
TaoXiOrderWeChatPayModel.fromJson(Map<String, dynamic> json) {
result =
json['Result'] != null ? new Result.fromJson(json['Result']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.result != null) {
data['Result'] = this.result.toJson();
}
return data;
}
}
class Result {
String appId;
String nonceString;
String pPackage;
String partnerId;
String prepayId;
String sign;
String timeStamp;
Result(
{this.appId,
this.nonceString,
this.pPackage,
this.partnerId,
this.prepayId,
this.sign,
this.timeStamp});
Result.fromJson(Map<String, dynamic> json) {
appId = json['AppId'];
nonceString = json['NonceString'];
pPackage = json['Package'];
partnerId = json['PartnerId'];
prepayId = json['PrepayId'];
sign = json['Sign'];
timeStamp = json['TimeStamp'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['AppId'] = this.appId;
data['NonceString'] = this.nonceString;
data['Package'] = this.pPackage;
data['PartnerId'] = this.partnerId;
data['PrepayId'] = this.prepayId;
data['Sign'] = this.sign;
data['TimeStamp'] = this.timeStamp;
return data;
}
}
3、一些问题
//监听,但是可能不管用(gitHub问题地址)
fluwx.weChatResponseEventHandler.listen((res) {
///回掉不过来,fluwx建议我们向后台要结果
if (res is fluwx.WeChatPaymentResponse) {
print(res);
}
});
在开发中,可能小伙伴根本找不到fluwx,这个类名在哪里,其实他是导入头文件时,重命名了而已
原:import 'package:fluwx/fluwx.dart';
重命名:import 'package:fluwx/fluwx.dart' as fluwx;
网友评论