// // PayViewController.swift // CommonFrame // // Created by Virgil on 2019/2/19. // Copyright © 2019 Virgil. All rights reserved. // import UIKit class PayShoppingMallViewController: BaseViewController { var money = "" @IBOutlet weak var lblMoney: UILabel! @IBOutlet weak var btnMenu2: Virgil_ListMenuSelectButton! @IBOutlet weak var btnMenu1: Virgil_ListMenuSelectButton! @IBOutlet weak var btnPay: UIButton! @IBOutlet weak var viewContent: UIView! @IBOutlet weak var lblRemark2: UILabel! ///0:创建订单支付 2:订单详情支付 var fromType = 0 /// 0:支付宝支付 1:微信支付 var payType = 0 var orderId = "" override func viewDidLoad() { super.viewDidLoad() self.title = "支付" initNavLeftBackButton() btnPay.setCornerRadius(size: btnPay.height() / 2) let strMoney = "¥\(money)" lblMoney.attributedText = CommonViewUntils.getAttributedStringForFont(str: strMoney, rangs: [NSRange.init(location: 0, length: 1), NSRange.init(location: 1, length: money.length())], fonts: [UIFont.systemFont(ofSize: 12), UIFont.systemFont(ofSize: 30)]) btnMenu1.isSelected = true btnMenu2.isSelected = false } //btnAddTarget(view: self.view, selector: #selector(self.btnMenuClick)) @IBAction func btnMenuClick(_ sender: AnyObject) { switch (sender as! UIButton).tag { case 1001: // 支付宝支付 payType = 0 btnMenu1.isSelected = true btnMenu2.isSelected = false break case 1002: // 微信支付 payType = 1 btnMenu1.isSelected = false btnMenu2.isSelected = true break case 1003: // 支付 paySubmit() break case 1004: // break case 1005: // break case 1006: // break default: break } } // MARK: =============加载数据=============== func paySubmit() { if(payType == 0) //支付宝支付 { if !CommonValidate.checkAlipayIsInstall() { CommonUntils.AlertView(controller: self, title: "提示", message: "您还没有安装支付宝,请先下载安装支付宝客户端", buttons: ["去安装"], style: .alert) { (_, _) in let url22 = "https://itunes.apple.com/cn/app/zhi-fu-bao-zhifubao-kou-bei/id333206289?l=zh&mt=8" UIApplication.shared.openURL(URL(string: url22)!) } return } } else //微信支付 { if !CommonValidate.checkWeiXinIsInstall() { CommonUntils.AlertView(controller: self, title: "提示", message: "您还没有安装微信,请先下载安装微信客户端", buttons: ["去安装"], style: .alert) { (_, _) in let url22 = "https://itunes.apple.com/cn/app/wechat/id414478124?l=zh&mt=8" UIApplication.shared.openURL(URL(string: url22)!) } return } } var url = RequestURL.payProductOrder let params = NSMutableDictionary() if(payType == 0) //支付宝支付 { url = RequestURL.alipaySignT params.setValue(orderId, forKey: "id") } else //微信支付 { url = RequestURL.wxSignProductT params.setValue(orderId, forKey: "id") } let http = AFHTTPSessionManager() http.get(url, parameters: params, progress: { (_) in }, success: { (operation, json) in print(json as Any) let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int if success == 200 { if self.payType == 0 { let sign = (json as! NSDictionary).object(forKey: "data") as! String let appScheme = "xingchuangkealipay" AlipaySDK.defaultService().payOrder(sign, fromScheme: appScheme, callback: { (result) in print("-----支付宝回调\n\(result)\n------") if result?["resultStatus"] as! String == "9000" { } else if result?["resultStatus"] as! String == "6001" { //用户取消支付 不处理 } else { } }) } else {//orderId String "be9a06e052d74fa1933603819240cdef" let dataInfo = (json as! NSDictionary).object(forKey: "data") as! NSDictionary let boo = TestSign.pay_demo(dataInfo as [NSObject: AnyObject]) if boo { } else { CommonUntils.alert(message: "支付失败,请重新支付!") MBProgressHUD.hide(for: self.view, animated: true) } } } }) { (_, _) in } // submitData(url: url, params: params, tag: 1001) } // override func returnData(tag:Int) { // if(tag == 1001) // { // // ///t余额支付走接口,其他走微信支付 // if(payType == 0) // { // AlipaySDK.defaultService().payOrder(self.dicBackDataAnyObject as! String, fromScheme: "xingchuangkealipay", callback: { (resultBack) in // }) // } // else // { // let boo = TestSign.pay_demo(self.dicBackData as [NSObject : AnyObject]) // if boo // { // // } // else // { // CommonUntils.alert(message: "支付失败,请重新支付!") // MBProgressHUD.hide(for:self.view, animated: true) // } // } // } // } // MARK: ======支付回调=== @objc func alipayresult(notity: NSNotification) { print("=====支付页面===") let isWin = notity.object as! String if isWin == "9000" { SVProgressHUD.showSuccess(withStatus: "支付成功") self.perform(#selector(backView), with: nil, afterDelay: 1) print("==支付宝====支付成功") // let selectImage = LGAlertView(title: "提示", message: "支付成功!", style: .alert, buttonTitles: ["确定"], cancelButtonTitle: nil, destructiveButtonTitle: nil ,actionHandler: { (alertView, title, index) -> Void in // self.backView(); // }, cancelHandler: { void in // self.backView(); // }, destructiveHandler: nil)! // selectImage.show(animated: true, completionHandler: nil) } else if isWin == "6001" { print("===支付宝===取消支付") //用户取消支付 不处理 } else { print("===支付宝===支付失败") CommonUntils.alert(message: "支付失败,请重新支付!") } } /** 微信支付回调方法 :param: notity <#notity description#> */ @objc func weixinpayresult(notity: NSNotification) { let isWin = notity.object as! String if isWin == "0" { if fromType == 0 { SVProgressHUD.showSuccess(withStatus: "支付成功") } else if fromType == 1 || fromType == 2 { SVProgressHUD.showSuccess(withStatus: "支付成功") } else if fromType == 3 { SVProgressHUD.showSuccess(withStatus: "支付成功") } self.perform(#selector(backView), with: nil, afterDelay: 1) } else if isWin == "-1" { print("===微信===支付失败") CommonUntils.alert(message: "支付失败,请重新支付!") } else if isWin == "-2" { print("===微信===支付取消") //用户取消支付,不处理 } } @objc func backView() { appDelegate.reloadUserInfo() if fromType == 0 { let vc = MyOrderViewController() vc.fromType = 1 toViewController(viewController: vc) } else { common_back_is_reload = true if !CommonUntils.reBackView(controller: self, aclass: MyOrderViewController.classForCoder()) { self.navigationController?.popToRootViewController(animated: true) } } } override func returnError(tag: Int, type: String) { } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let nc = NotificationCenter.default nc.addObserver(self, selector: #selector(self.weixinpayresult), name: NSNotification.Name(rawValue: "notification_weixinpay"), object: nil) nc.addObserver(self, selector: #selector(self.alipayresult), name: NSNotification.Name(rawValue: "notification_alipay"), object: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(NSNotification.Name(rawValue: "notification_weixinpay")) NotificationCenter.default.removeObserver(NSNotification.Name(rawValue: "notification_alipay")) } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: "PayShoppingMallViewController", bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }