前提
①:已在微信开放平台创建好应用
②:已在微信支付开通APP支付
1、准备
微信支付
微信开放平台
APP签名工具
Flutter fluwx SDK
2、Flutter前端部分
添加fluwx包
dependencies:
flutter:
sdk: flutter
fluwx: ^3.6.1+4 // 引用对应版本
调用微信支付
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:fluwx/fluwx.dart';
void wxPay() async {
final payInfo = await Dio().post('后台接口请求');
final Map<String, dynamic> pay = convert.jsonDecode(payInfo.toString());
// 拼装对应的参数
payWithWeChat(
appId: pay['appid'],
partnerId: pay['partnerid'],
prepayId: pay['prepayid'],
packageValue: pay['package'],
nonceStr: pay['noncestr'],
timeStamp: int.parse(pay["timestamp"]),
sign: pay['sign'],
);
// 监听微信支付回调
var lis =
fluwx.weChatResponseEventHandler.listen((BaseWeChatResponse res) async {
if (res is fluwx.WeChatPaymentResponse && res.errCode == 0) {
console.log('支付成功!');
} else {
console.log('支付失败!');
}
});
}
3、Node.js后端部分
config.js
const pay_photo_app = {
appid: '应用APPID',
mchid: '商户ID',
partnerKey: '应用AppSecret',
pfx: require('fs').readFileSync('./lib/apiclient_cert.p12'), // 签名证书
notify_url: '支付回调地址',
spbill_create_ip: ''
}
module.exports = {
wxpay
}
- pay.js ```javascript const config = require(‘../config/config’) const payApp = tenpay.init(config.wxpay);
// 证件照APP下单--微信
async wxPay(ctx) { const out_trade_no = utils.getDate() + Math.random().toString().substring(2, 10) const price = 298 const result = await payApp.getAppParams({ out_trade_no, //内部订单号 body: ‘商品名’, // 商品名 total_fee: price //价格 单位(分) }); ctx.body = result } ```
结语
自此 Flutter 接入微信支付已经OK了