生活随笔
收集整理的這篇文章主要介紹了
swift调用支付宝
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
開發環境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形式打開)
<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>
查找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源代碼
總結
以上是生活随笔為你收集整理的swift调用支付宝的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。