Flutter 在 Android 中实现微信支付

 

Flutter Android 中实现微信支付 sy_flutter_wechat

Android 微信支付注意事项

// 说明: 微信支付无法直接通过调试工具进行测试。如果直接连接调试器,将返回错误码-1。
// 在这种情况下,我们需要使用之前的签名文件重新生成签名,然后将其发送到手机上进行测试。

// 注意:
// 1. 必须在微信开放平台配置应用的包名和签名
// 2. android应用的包名必须和微信开放平台配置的一致
// 3. 在微信开放平台配置应用签名时使用的 keystore 文件必须和正式打包应用时使用的 keystore 文件一致
// 4. 代码中配置的 Appid 必须和微信开放平台的 Appid 一致
// 5. 必须事先准备好生成预支付信息的服务器API接口
// 6. android 必须正式打包后才能进行微信支付

代码实现

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:sy_flutter_wechat/sy_flutter_wechat.dart';
import 'package:dio/dio.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>{
  @override
  void initState() {
    super.initState();
    _register();
  }

  _register() async {
    bool result = await SyFlutterWechat.register('wx5881fa2638a2ca60');
    print(result);
  }

  _weixinPay() async {
    var apiUrl = 'http://agent.itying.com/wxpay/';
    var myPayInfo = await Dio().get(apiUrl);
    Map myInfo = json.decode(myPayInfo.data);
    var payInfo = {
      "appid": myInfo["appid"].toString(),
      "partnerid": myInfo["partnerid"].toString(),
      "prepayid": myInfo["prepayid"].toString(),
      "package": myInfo["package"].toString(),
      "noncestr": myInfo["noncestr"].toString(),
      "timestamp": myInfo["timestamp"].toString(),
      "sign": myInfo["sign"].toString(),
    };
    SyPayResult payResult = await SyFlutterWechat.pay(SyPayInfo.fromJson(payInfo));
    print(payResult);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("微信"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('分享文字'),
              onPressed: () async {
                bool res = await SyFlutterWechat.shareText('hello world', shareType: SyShareType.session);
                print('分享文字:' + res.toString());
              },
            ),
            RaisedButton(
              child: Text('分享图片'),
              onPressed: () async {
                bool res = await SyFlutterWechat.shareImage('https://avatars0.githubusercontent.com/u/10024776', shareType: SyShareType.timeline);
                print('分享图片:' + res.toString());
              },
            ),
            RaisedButton(
              child: Text('分享网页'),
              onPressed: () async {
                bool res = await SyFlutterWechat.shareWebPage('标题', '描述', 'https://avatars0.githubusercontent.com/u/10024776', 'http://www.example.com', shareType: SyShareType.session);
                print('分享网页:' + res.toString());
              },
            ),
            RaisedButton(
              child: Text('支付'),
              onPressed: _weixinPay,
            ),
          ],
        ),
      ),
    );
  }
}

posted on 2020-04-23 15:52  完美前端  阅读(1571)  评论(0)    收藏  举报

导航