前提

①:已在微信开放平台创建好应用
②:已在微信支付开通APP支付

1、准备

微信支付
微信开放平台
APP签名工具
Flutter fluwx SDK

2、Flutter前端部分

  • 添加fluwx包

    1. dependencies:
    2. flutter:
    3. sdk: flutter
    4. fluwx: ^3.6.1+4 // 引用对应版本
  • 调用微信支付

    1. import 'package:fluwx/fluwx.dart' as fluwx;
    2. import 'package:fluwx/fluwx.dart';
    3. void wxPay() async {
    4. final payInfo = await Dio().post('后台接口请求');
    5. final Map<String, dynamic> pay = convert.jsonDecode(payInfo.toString());
    6. // 拼装对应的参数
    7. payWithWeChat(
    8. appId: pay['appid'],
    9. partnerId: pay['partnerid'],
    10. prepayId: pay['prepayid'],
    11. packageValue: pay['package'],
    12. nonceStr: pay['noncestr'],
    13. timeStamp: int.parse(pay["timestamp"]),
    14. sign: pay['sign'],
    15. );
    16. // 监听微信支付回调
    17. var lis =
    18. fluwx.weChatResponseEventHandler.listen((BaseWeChatResponse res) async {
    19. if (res is fluwx.WeChatPaymentResponse && res.errCode == 0) {
    20. console.log('支付成功!');
    21. } else {
    22. console.log('支付失败!');
    23. }
    24. });
    25. }

    3、Node.js后端部分

  • config.js

  1. const pay_photo_app = {
  2. appid: '应用APPID',
  3. mchid: '商户ID',
  4. partnerKey: '应用AppSecret',
  5. pfx: require('fs').readFileSync('./lib/apiclient_cert.p12'), // 签名证书
  6. notify_url: '支付回调地址',
  7. spbill_create_ip: ''
  8. }
  9. module.exports = {
  10. wxpay
  11. }
  • pay.js ```javascript const config = require(‘../config/config’) const payApp = tenpay.init(config.wxpay);
  1. // 证件照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了