IOS 13 Swift 5 接入支付宝支付 应用回调 不走 application 走 scene

第一步:到支付宝开放平台下载最新SDK代码

              这里用到的是 15.7.4 版本

第二步:导入到项目中

              2.1、在Xcode项目中,File -》 Add Files to "项目名称" ,找到刚刚下载的支付宝SDK

              2.2、安照下图操作

                      勾选Copy items if needed,选择AlipaySDK.framework,点击add添加,再重复第一步将AlipaySDK.bundle添加进入项目

                      导入后效果为

                   2.3、导入必要架包。在项目General - > Framework,Libraries,and Embedded Content下添加如下架包

libc++.tbd
libz.tbd
SystemConfiguration.framework
CoreTelephony.framework
QuartzCore.framework
CoreText.framework
CoreGraphics.framework
UIKit.framework
Foundation.framework
CFNetwork.framework
CoreMotion.framework

 

第三步:scheme

              应用间通信用scheme,调起支付宝支付回调标识

              3.1、在Info -》 URL Types 中添加一个标识

              3.2、在 Info.plist 也添加一个 URL Types,内容和3.1一样(不加跳转到支付宝支付完成或者取消支付后就会留在支付宝应用)

第四步:可以代码实现支付了

               4.1、调起支付宝支付

                        注意,这里的回调只是针对网页的回调,支付宝应用支付回调不会走这里

//支付宝支付
    func AliplayFunc(information:String){
        //information-支付订单信息字串-请求后台返回的
        //"调用支付的app注册在info.plist中的scheme  第三步"
        AlipaySDK.defaultService()?.payOrder(information, fromScheme: "注册在info.plist中的scheme", callback: { (resultDic) in
            //print("-------- = \(resultDic)");
            print(resultDic)
            if let Alipayjson = resultDic as NSDictionary?{
                let resultStatus = Alipayjson.value(forKey: "resultStatus") as! String
                if resultStatus == "9000"{
                    //支付成功处理
                    print("支付成功")
                }else if resultStatus == "8000" {
                    print("正在处理中")
                    self.navigationController?.popViewController(animated: true)
                }else if resultStatus == "4000" {
                    print("订单支付失败");
                    self.navigationController?.popViewController(animated: true)
                }else if resultStatus == "6001" {
                    print("用户中途取消")
                    self.navigationController?.popViewController(animated: true)
                }else if resultStatus == "6002" {
                    print("网络连接出错")
                    self.navigationController?.popViewController(animated: true)
                }
            }

        })
        
    }

               4.2、实现支付宝应用支付回调

                        在IOS 13之前,这个回调可以写在 AppDelegate 的 application 中:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if (url.host! as String) == "safepay" {
            AlipaySDK.defaultService().processOrder(withPaymentResult: url){
                value in
                let code = value!
                let resultStatus = code["resultStatus"] as!String
                var content = ""
                switch resultStatus {
                case "9000":
                    content = "支付成功"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPaySucceess"), object: content)
                case "8000":
                    content = "订单正在处理中"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayUnknowStatus"), object: content)
                case "4000":
                    content = "支付失败"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "5000":
                    content = "重复请求"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "6001":
                    content = "中途取消"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "6002":
                    content = "网络连接出错"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefault"), object: content)
                case "6004":
                    content = "支付结果未知"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayUnknowStatus"), object: content)
                default:
                    content = "支付失败"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                    break
                }
            }
        }
        return true
    }

               4.3、IOS 13之后,发现回调不会走 application 了,而是走 SceneDelegate 的 scene 方法,如下:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        print("scene7")
        print(URLContexts.first?.url)
        if let context = URLContexts.first {
            var options = [UIApplication.OpenURLOptionsKey: Any]()
            options[.sourceApplication] = context.options.sourceApplication
            options[.annotation] = context.options.annotation
            options[.openInPlace] = context.options.openInPlace
            if ((URLContexts.first?.url.host!)! as String) == "safepay" {
                AlipaySDK.defaultService()?.processOrder(withPaymentResult: context.url, standbyCallback: { (resultDic:[AnyHashable : Any]?) in
                    var content = ""
                    if let Alipayjson = resultDic as NSDictionary?{
                    let resultStatus = Alipayjson.value(forKey: "resultStatus") as! String
                        switch resultStatus {
                        case "9000":
                            content = "支付成功"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPaySucceess"), object: content)
                        case "8000":
                            content = "订单正在处理中"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayUnknowStatus"), object: content)
                        case "4000":
                            content = "支付失败"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                        case "5000":
                            content = "重复请求"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                        case "6001":
                            content = "中途取消"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                        case "6002":
                            content = "网络连接出错"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefault"), object: content)
                        case "6004":
                            content = "支付结果未知"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayUnknowStatus"), object: content)
                        default:
                            content = "支付失败"
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                            break
                        
                        }
                        print("\(content) \(resultStatus)")
                    }
                    
                })
            }
        }
    }

               4.4、实现了4.2或4.3之后,就能够获取到同步的回调数据了,从上面的代码中可以看到,获取到结果之后,通过通知中心通知界面。

                       4.4.1、发送通知,如上4.2和4.3,获取结果之后发送通知给界面

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)

                       4.4.2、接收通知,在相关界面接收通知

//支付宝支付获取通知-失败的通知
NotificationCenter.default.addObserver(self, selector: #selector(self.aliPayDefeat(notification:)), name: Notification.Name("aliPayDefeat"), object: nil)
//支付宝支付获取通知-成功的通知
NotificationCenter.default.addObserver(self, selector: #selector(self.aliPaySucceess(notification:)), name: Notification.Name("aliPaySucceess"), object: nil)

                        4.4.3、接收通知时定义了方法处理通知数据

                                    #selector(self.aliPayDefeat(notification:))

                                   #selector(self.aliPaySucceess(notification:))

    @objc func aliPayDefeat(notification: Notification){
        print(notification.name)
        print("支付失败")
    }
    
    @objc func aliPaySucceess(notification: Notification){
        print(notification.name)
        print("支付成功")
    }

 

 

 

以上就是最新实现的 ios 接入支付宝支付,如有不足,请留言指出 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值