flutter对接微信支付(fluwx)

在这里插入图片描述

后台调用微信预支付接口获取prepay_id和nonce_str供前段唤醒微信使用。需二次签名,不可直接使用返回值中的sign。

private SfwDataOut wXPayInit(String ip, String order_id, String description, String tradeType, int totalFee) throws Exception {
        SfwDataOut sfwOut = new SfwDataOut();
        Map<String, String> paramMap = new HashMap<String, String>();
        String key = "4E5D171E22BF2B975ED7F410EC54EF75";
        String appId = "wx066b1f5851da7dd5";
        String mchId = "1610832735";

        paramMap.put("appid", appId); // 商家平台ID
        paramMap.put("body", description); // 商家名称-销售商品类目、String(128)
        paramMap.put("mch_id", mchId); // 商户ID
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); // UUID
        paramMap.put("out_trade_no", order_id);// 订单号,每次都不同
        paramMap.put("spbill_create_ip", ip);
        paramMap.put("total_fee", "1"); // 支付金额,单位分
        paramMap.put("notify_url", "http://www.zoni8.cn/app/wx/callback");// 此路径是微信服务器调用支付结果通知路径随意写
        paramMap.put("trade_type", tradeType); // 支付类型
        String sign = WXPayUtil.generateSignature(paramMap, key);
        paramMap.put("sign", sign);
        String xml = WXPayUtil.mapToXml(paramMap);// 将所有参数(map)转xml格式

        URL realUrl = new URL("https://api.mch.weixin.qq.com/pay/unifiedorder");
        HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(xml);
        out.flush();

        BufferedReader in = null;
        Map<String, Object> vals = new HashMap<String, Object>();
        try {
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            String line = "";
            String result = "";
            while ((line = in.readLine()) != null) {
                result += line;
            }
            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);


            GregorianCalendar gc = new GregorianCalendar();
            int timestamp = (int) gc.getTimeInMillis();
            Map newMap = new HashMap();
            newMap.put("appid", resultMap.get("appid"));
            newMap.put("partnerid", resultMap.get("mch_id"));
            newMap.put("prepayid", resultMap.get("prepay_id"));
            newMap.put("package", "Sign=WXPay");
            newMap.put("noncestr", resultMap.get("nonce_str"));
            newMap.put("timestamp",String.valueOf(timestamp));
            String newSign = WXPayUtil.generateSignature(newMap, key);

            System.out.println("返回值:"  + result);
            System.out.println("准备id:" + resultMap.get("prepay_id"));
            String returnCode = resultMap.get("return_code");
            if (returnCode != null && "SUCCESS".equals(returnCode)) {
                vals.put("sign", newSign);
                vals.put("prepay_id", resultMap.get("prepay_id"));
                vals.put("appid", resultMap.get("appid"));
                vals.put("mch_id", resultMap.get("mch_id"));
                vals.put("package", "Sign=WXPay");
                vals.put("nonce_str", resultMap.get("nonce_str"));
                vals.put("timestamp", timestamp);
            } else {
                vals.put("error", "true");
            }
        } catch (Exception e) {
            // in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
            vals.put("error", e.getMessage());
        }
        sfwOut.addData(vals);
        return sfwOut;
    }

前端flutter(fluwx插件)

		//微信注册APP程序
      registerWxApi(
          appId: Config.weichat_merchant_id, // zbml appid
          doOnAndroid: true,
          doOnIOS: true,
          universalLink: Config.UNIVERSAL_LINK);

		//唤醒微信支付
      payWithWeChat(
        appId: payInfo['appid'],
        partnerId: Config.weichat_mch_id,
        prepayId: payInfo['prepay_id'],
        packageValue: payInfo['package'],
        nonceStr: payInfo['nonce_str'],
        timeStamp: payInfo['timestamp'],
        sign: payInfo['sign'],
      );
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
通过flutter的webview_flutter插件,我们可以在应用中嵌入支付宝和微信支付,并实现支付功能。使用webview_flutter的步骤如下: 1. 首先,在`pubspec.yaml`文件中引入webview_flutter插件。可以在dependencies部分添加`webview_flutter: ^2.0.0`,然后运行`flutter pub get`命令进行依赖安装。 2. 在需要嵌入支付功能的页面中,导入webview_flutter插件。在页面的顶部引入`import 'package:webview_flutter/webview_flutter.dart';`。 3. 在页面的主体中,创建一个WebView组件,并指定需要加载的URL。例如,在Container中使用WebView组件,可以使用如下代码: ```dart Container( child: WebView( initialUrl: 'https://www.alipay.com/', javascriptMode: JavascriptMode.unrestricted, ), ) ``` 4. 在支付宝和微信支付的URL中,传递相关的支付参数,例如订单号、支付金额等。 5. 在WebView组件中,可以通过注册一个JavaScript channel来监听网页中的支付回调信息。例如,在页面初始加载完成后,可以通过使用`onPageFinished`回调方法来执行一段JavaScript代码,监听支付结果。在该代码中,可以通过调用与原生平台交互的方法,将支付结果返回到Flutter中进行处理。 6. 在Flutter中,可以根据支付结果展示相关的提示信息,例如支付成功、支付失败等。 需要注意的是,支付宝和微信支付的具体接口和参数可能会根据版本的更新而有所变化,所以在代码实现中需要根据最新的文档进行调整。同时,为了确保支付过程的安全性,建议在应用中对支付接口进行适当的安全设置,例如使用HTTPS协议,并对支付参数进行加密处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值