swift调用支付宝

发布时间:2020-07-11 19:51:13 作者:桂素伟
来源:网络 阅读:1189

开发环境xcode7.1 运行环境 IOS9.1

到支付宝面面下载IOS的移动支付功能的SDKAndroidIOS是同一个zip文件下)

http://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1

然后申请商家支付宝,得到相应的private_keypartner,seller

IOS的资料在SDK文件夹的客户端demo”下的IOS文件夹下

<key>NSAppTransportSecurity</key>
   <dict>
       <key>NSExceptionDomains</key>
       <dict>
           <key>alipay.com</key>
           <dict>
                <!--Include to allowsubdomains-->
               <key>NSIncludesSubdomains</key>
                <true/>
                <!--Include to allowinsecure HTTP requests-->
               <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <!--Include to specifyminimum TLS version-->
               <key>NSTemporaryExceptionMinimumTLSVersion</key>
               <string>TLSv1.0</string> 
               <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
                <false/>
           </dict>
       </dict>
</dict>


Util中的base64.hopenssl_wrapper.h添加#import <Foundation/Foundation.h>,给支付宝AlipaySDK.h添加#import <Foundation/Foundation.h>#import<UIKit/UIKit.h>

查找Bitcode,把Yes改成No

查打Header SearchPaths,点小+号,添加$(SRCROOT)/AlipayDemo

 

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let but = UIButton(type: UIButtonType.System);
        but.setTitle("
支付", forState: UIControlState.Normal);
        but.backgroundColor = UIColor.greenColor();
        but.frame = CGRect(x: 10, y: 100, width: 100, height: 30);
        but.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview(but);    }
    func click()
    {
        AliplayFunc();
       print("click")
    }
    var TicketTotalprice:Float=0.01;//支付金额
    var seller:String="支付宝申请的seller";
    var partner:String="支付宝申请的partner";
    var privateKey:String = "替换支付申请的privet_key";
    
    
    func AliplayFunc(){        
        let Orders = Order()        
        Orders.partner = partner        
        Orders.seller = seller        
        Orders.productName = "ProductName";        
        Orders.productDescription = "this is a goods";        
        Orders.amount = NSString(format: "%.2f",TicketTotalprice) as String ;//(价格必须小数点两位)        
        Orders.tradeNO = "DJ0000000001" ;       
        Orders.notifyURL = "http://selftweb.com";        
        Orders.service = "mobile.securitypay.pay";        
        Orders.paymentType = "1";        
        Orders.inputCharset = "utf-8";        
        Orders.itBPay = "30m";        
        Orders.showUrl = "m.alipay.com";        
        let appScheme = "GSWAPayDemo";//在        
        let orderSpec = Orders.description;        
        let signer = CreateRSADataSigner(privateKey);        
        let signedString = signer.signString(orderSpec);        
        let orderString = "\(orderSpec)&sign=\"\(signedString)\"&sign_type=\"RSA\"";     
        AlipaySDK.defaultService().payOrder(orderString, fromScheme: appScheme, callback: { (resultDic) -> Void in            
            print("reslut = \(resultDic)");            
            if let Alipayjson = resultDic as? NSDictionary{                
                let resultStatus = Alipayjson.valueForKey("resultStatus") as! String 
                if resultStatus == "9000"{                    
                    print("OK")
                }else if resultStatus == "8000" {                    
                    print("正在处理中")                    
                    self.navigationController?.popViewControllerAnimated(true) 
                }else if resultStatus == "4000" {
                    print("订单支付失败");
                    self.navigationController?.popViewControllerAnimated(true)
                }else if resultStatus == "6001" {
                    print("用户中途取消")
                    self.navigationController?.popViewControllerAnimated(true)
                }else if resultStatus == "6002" {
                    print("网络连接出错")
                    self.navigationController?.popViewControllerAnimated(true)
                }
            }
        })
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

 Demo源代码

推荐阅读:
  1. Swift2.0(1)Swift简介
  2. Java调用支付宝接口的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios 支付宝 partner

上一篇:JSONP 简洁交互(PHP)

下一篇:grub启动程序设置加密 虚拟机vmware8.0.4 CentOS 6.3

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》