swift 集成支付宝遇到的问题

移动端做加密,安卓和ios的私钥一定要匹配。比如安卓先生成了私钥和公钥,并且把公钥上传到支付宝管理平台了。这时候ios端无法通过公钥生成私钥,只能拿安卓的私钥转换成ios私钥,才能和支付宝上的应用公钥匹配

ios段接支付宝可以,将uiappdelegate中的这个方法先写好,等支付宝支付失败后跳转到app时,就会打印错误日志,方便调试

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

        if (url.host == "safepay") {

            // 支付跳转支付宝钱包进行支付,处理支付结果

            AlipaySDK.defaultService().processOrder(withPaymentResult: url, standbyCallback: { (resultDic:[AnyHashable : Any]?) in

                

                if let Alipayjson = resultDic as NSDictionary?{

                    let resultStatus = Alipayjson.value(forKey: "resultStatus") as! String

                    if resultStatus == "9000"{

                        print("OK")

                        

                        let resposeStr =  Alipayjson["result"] as! String

                        let arr1 = resposeStr.components(separatedBy: ""out_trade_no":"")

                        let arr2 = arr1.last

                        let tadingno = arr2?.components(separatedBy: ""}").first

                        

                        

                        NotificationCenter.default.post(name: NSNotification.Name.init("paySuccess"), object: nil, userInfo: ["out_trade_no":tadingno ?? ""])

                        

                    }else if resultStatus == "8000" {

                        

                        print("正在处理中")

                        

                    }else if resultStatus == "4000" {

                        NotificationCenter.default.post(name:  NSNotification.Name.init("payFeild"), object: nil, userInfo: ["warning":"订单支付失败"])

                        print("订单支付失败");

                        WWLog(message: resultDic)

                        

                    }else if resultStatus == "6001" {

                        NotificationCenter.default.post(name:  NSNotification.Name.init("payFeild"), object: nil, userInfo: ["warning":"已取消支付"])

                        

                        print("用户中途取消")

                        

                    }else if resultStatus == "6002" {

                        print("网络连接出错")

                        NotificationCenter.default.post(name:  NSNotification.Name.init("payFeild"), object: nil, userInfo: ["warning":"网络连接出错"])

                        

                    }

                }

                

                

                

                

            })

            // 授权跳转支付宝钱包进行支付,处理支付结果

            AlipaySDK.defaultService().processAuth_V2Result(url, standbyCallback: { (resultDic:[AnyHashable : Any]?) in

                

                // 解析 auth code

                let result = resultDic?["result"] as! String;

                var authCode : String? = nil;

                if (result.lengthOfBytes(using: String.Encoding.utf8) > 0) {

                    let resultArr = result.components(separatedBy: "&")

                    

                    for subResult in resultArr {

                        if (subResult.lengthOfBytes(using: String.Encoding.utf8) > 10 && subResult.hasPrefix("auth_code=")) {

                            let index = "auth_code=".endIndex

                            authCode = subResult.substring(from:index )

                            break;

                        }

                    }

                }

                

            })

        }

        

        return true

    }
原文地址:https://www.cnblogs.com/heyode/p/6893075.html