您好,登錄后才能下訂單哦!
開發(fā)環(huán)境xcode7.1 運(yùn)行環(huán)境 IOS9.1
到支付寶面面下載IOS的移動(dòng)支付功能的SDK(Android和IOS是同一個(gè)zip文件下)
http://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1
然后申請(qǐng)商家支付寶,得到相應(yīng)的private_key和partner,seller
IOS的資料在SDK文件夾的“客戶端demo”下的IOS文件夾下
復(fù)制AlipaySDK.bundle和AlipaySDK.framework到項(xiàng)目下
復(fù)制IOS Demo下的兩個(gè).a文件到項(xiàng)目下
復(fù)制iOS Demo下的openssl文件夾,Util文件夾,Order.h,Order.m(省得自己在swift中定義訂單)文件到項(xiàng)目中
在xcode中創(chuàng)建一個(gè)項(xiàng)目AlipayDemo,在項(xiàng)目中Add Files to AlipayDemo所有的.a文件和openssl文件夾,Util文件夾,Order.h,Order.m,此時(shí)系統(tǒng)提示創(chuàng)建頭文件,選擇允許創(chuàng)建(可以手動(dòng)添加一個(gè).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>
設(shè)置Build Settings
查找Bitcode,把Yes改成No
查打Header SearchPaths,點(diǎn)小+號(hào),添加$(SRCROOT)/AlipayDemo
在info的URL Types中添加一個(gè)GSWAlipayDemo的節(jié)點(diǎn),以備代碼中Order的appScheme使用。
代碼實(shí)現(xiàn)
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="支付寶申請(qǐng)的seller"; var partner:String="支付寶申請(qǐng)的partner"; var privateKey:String = "替換支付申請(qǐng)的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 ;//(價(jià)格必須小數(shù)點(diǎn)兩位) 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("網(wǎng)絡(luò)連接出錯(cuò)") self.navigationController?.popViewControllerAnimated(true) } } }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Demo源代碼
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。