swift 集成支付寶遇到的問題

移動端作加密,安卓和ios的私鑰必定要匹配。好比安卓先生成了私鑰和公鑰,而且把公鑰上傳到支付寶管理平臺了。這時候ios端沒法經過公鑰生成私鑰,只能拿安卓的私鑰轉換成ios私鑰,才能和支付寶上的應用公鑰匹配ios

ios段接支付寶能夠,將uiappdelegate中的這個方法先寫好,等支付寶支付失敗後跳轉到app時,就會打印錯誤日誌,方便調試json

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

    }
相關文章
相關標籤/搜索