美文网首页
flutter 支付(1)- 支付宝支付

flutter 支付(1)- 支付宝支付

作者: iOS_我更专业 | 来源:发表于2020-04-29 17:24 被阅读0次

    首先,大家可以看下flutter的一些支付宝库

    flutter_alipay: ^0.1.2

    运行一下,flutter pub get

    首先在iOS项目下配置一些设置:

    1、Runner-Bridging-Header.h中导入头文件

    #import <flutter_alipay/FlutterAlipayPlugin.h>

    2、在AppDelegate里添加如下配置

    override func application(_application:UIApplication, open url:URL, sourceApplication:String?, annotation:Any) ->Bool{

     if(url.host=="safepay")//支付宝支付

            {

     return FlutterAlipayPlugin.handleOpen(url);

            }

     return true;

        }

     override func application(_application:UIApplication, handleOpen url:URL) ->Bool{

    if(url.host=="safepay")//支付宝支付

            {

     return FlutterAlipayPlugin.handleOpen(url);

            }

     return true;

        }

     override func application(_app:UIApplication, open url:URL, options: [UIApplication.OpenURLOptionsKey:Any] = [:]) ->Bool{

    if(url.host=="safepay")//支付宝支付

            {

     return FlutterAlipayPlugin.handleOpen(url);

            }

     return true;

        }

    3、在info.plist中加入如下配置

    <key>CFBundleURLTypes</key>

    <array>

    <dict>

    <key>CFBundleTypeRole</key>

    <string>Editor</string>

    <key>CFBundleURLName</key>

    <string>alipay</string>

    <key>CFBundleURLSchemes</key>

    <array>

    <string>***alipay</string>

    </array>

    </dict>

    </array>

    <key>CFBundleVersion</key>

    4、在Flutter项目下可以创建一个ALiPayHelper的类,用来管理阿里支付,返回Future<bool>,成功为true,不成功为false

    class AliPayHelper {

      static Future<bool> aliPay(String sign) async {

        if (sign == null || sign.length == 0) {

          return false;

        }

        FlutterAlipay.pay(sign).then((payResult) {

          print('>>>>>  ${payResult.toString()}');

          /*

            /// 支付状态,参考支付宝的文档https://docs.open.alipay.com/204/105695/

      /// 返回码,标识支付状态,含义如下:

      /// 9000——订单支付成功        上面的result有值

      /// 8000——正在处理中

      /// 4000——订单支付失败

      /// 5000——重复请求

      /// 6001——用户中途取消

      /// 6002——网络连接出错

          */

          String payResultStatus = payResult.resultStatus;

          if (payResultStatus == "9000") {

            return true;

            print("支付成功");

            CommonToast.showToast('支付成功');

          } else if (payResultStatus == "6001") {

            return false;

            print("支付取消");

            CommonToast.showToast('支付取消');

          } else if (payResultStatus == "4000") {

            return false;

            print("支付失败");

            CommonToast.showToast('支付失败');

          } else if (payResultStatus == "8000") {

            return false;

            print("等待支付");

            CommonToast.showToast('等待支付');

          } else if (payResultStatus == "6002") {

            return false;

            print("无网络");

            CommonToast.showToast('无网络');

          } else if (payResultStatus == "5000") {

            return false;

            print("重复支付");

            CommonToast.showToast('重复支付');

          }else{

            return false;

            print("支付失败,未知错误");

            CommonToast.showToast('支付失败,未知错误');

          }

        }).catchError((e) {

          return false;

          print("支付失败");

          CommonToast.showToast('支付失败');

        });

      }

    }

    相关文章

      网友评论

          本文标题:flutter 支付(1)- 支付宝支付

          本文链接:https://www.haomeiwen.com/subject/vcenwhtx.html