swift调用支付宝
2015-10-27 17:29
423 查看
开发环境xcode7.1 运行环境 IOS9.1
到支付宝面面下载IOS的移动支付功能的SDK(Android和IOS是同一个zip文件下)
http://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1
然后申请商家支付宝,得到相应的private_key和partner,seller
IOS的资料在SDK文件夹的“客户端demo”下的IOS文件夹下
复制AlipaySDK.bundle和AlipaySDK.framework到项目下
复制IOS Demo下的两个.a文件到项目下
复制iOS Demo下的openssl文件夹,Util文件夹,Order.h,Order.m(省得自己在swift中定义订单)文件到项目中
在xcode中创建一个项目AlipayDemo,在项目中Add Files to AlipayDemo所有的.a文件和openssl文件夹,Util文件夹,Order.h,Order.m,此时系统提示创建头文件,选择允许创建(可以手动添加一个.h文件作为头文件)
如果在基于IOS9.0编译,在info.list中添加如下xml代码(info.list以SourceCode形式打开)
增加头文件
Util中的base64.h,openssl_wrapper.h添加#import <Foundation/Foundation.h>,给支付宝AlipaySDK.h添加#import <Foundation/Foundation.h>和#import<UIKit/UIKit.h>设置Build Settings
查找Bitcode,把Yes改成No查打Header SearchPaths,点小+号,添加$(SRCROOT)/AlipayDemo
在info的URL Types中添加一个GSWAlipayDemo的节点,以备代码中Order的appScheme使用。
代码实现
到支付宝面面下载IOS的移动支付功能的SDK(Android和IOS是同一个zip文件下)
http://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1
然后申请商家支付宝,得到相应的private_key和partner,seller
IOS的资料在SDK文件夹的“客户端demo”下的IOS文件夹下
复制AlipaySDK.bundle和AlipaySDK.framework到项目下
复制IOS Demo下的两个.a文件到项目下
复制iOS Demo下的openssl文件夹,Util文件夹,Order.h,Order.m(省得自己在swift中定义订单)文件到项目中
在xcode中创建一个项目AlipayDemo,在项目中Add Files to AlipayDemo所有的.a文件和openssl文件夹,Util文件夹,Order.h,Order.m,此时系统提示创建头文件,选择允许创建(可以手动添加一个.h文件作为头文件)
如果在基于IOS9.0编译,在info.list中添加如下xml代码(info.list以SourceCode形式打开)
<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.h,openssl_wrapper.h添加#import <Foundation/Foundation.h>,给支付宝AlipaySDK.h添加#import <Foundation/Foundation.h>和#import<UIKit/UIKit.h>设置Build Settings
查找Bitcode,把Yes改成No查打Header SearchPaths,点小+号,添加$(SRCROOT)/AlipayDemo
在info的URL Types中添加一个GSWAlipayDemo的节点,以备代码中Order的appScheme使用。
代码实现
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源代码本文出自 “桂素伟” 博客,请务必保留此出处http://axzxs.blog.51cto.com/730810/1706890
相关文章推荐
- OC与Swift混编
- swift算法实践(2)
- swift算法实践(1)
- SwiftSocket 接收服务端byte[] 本地转换
- swift 初级 断言
- Swift函数
- iOS开发~Swift与Cocoapods的使用
- swift中利用闭包进行反向传值
- swift中label内容显示不同的颜色
- Developing iOS 8 Apps with Swift 学习笔记
- swift 学习记录(三)
- 用C#完成Swift远程推送通知
- 用C#完成Swift远程推送通知
- Swift学习 day1
- swift 初级 函数
- 可以匹配就匹配咯: 详解 Swift 的模式匹配
- swift-工具方法
- swift(三)
- Carthage 包管理工具,另一种敏捷轻快的 iOS & MAC 开发体验 | SwiftCafe 咖啡时光
- Swift-使用NSJSONSerialization生成或解析JSON数据