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文件夹下
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>alipay.com</key> <dict> <!--Includetoallowsubdomains--> <key>NSIncludesSubdomains</key> <true/> <!--IncludetoallowinsecureHTTPrequests--> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <!--IncludetospecifyminimumTLSversion--> <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
importUIKit classViewController:UIViewController{ overridefuncviewDidLoad(){ super.viewDidLoad() letbut=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);} funcclick() { AliplayFunc(); print("click") } varTicketTotalprice:Float=0.01;//支付金额 varseller:String="支付宝申请的seller"; varpartner:String="支付宝申请的partner"; varprivateKey:String="替换支付申请的privet_key"; funcAliplayFunc(){ letOrders=Order() Orders.partner=partner Orders.seller=seller Orders.productName="ProductName"; Orders.productDescription="thisisagoods"; Orders.amount=NSString(format:"%.2f",TicketTotalprice)asString;//(价格必须小数点两位) 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"; letappScheme="GSWAPayDemo";//在 letorderSpec=Orders.description; letsigner=CreateRSADataSigner(privateKey); letsignedString=signer.signString(orderSpec); letorderString="(orderSpec)&sign="(signedString)"&sign_type="RSA""; AlipaySDK.defaultService().payOrder(orderString,fromScheme:appScheme,callback:{(resultDic)->Voidin print("reslut=(resultDic)"); ifletAlipayjson=resultDicas?NSDictionary{ letresultStatus=Alipayjson.valueForKey("resultStatus")as!String ifresultStatus=="9000"{ print("OK") }elseifresultStatus=="8000"{ print("正在处理中") self.navigationController?.popViewControllerAnimated(true) }elseifresultStatus=="4000"{ print("订单支付失败"); self.navigationController?.popViewControllerAnimated(true) }elseifresultStatus=="6001"{ print("用户中途取消") self.navigationController?.popViewControllerAnimated(true) }elseifresultStatus=="6002"{ print("网络连接出错") self.navigationController?.popViewControllerAnimated(true) } } }) } overridefuncdidReceiveMemoryWarning(){ super.didReceiveMemoryWarning() //DispoSEOfanyresourcesthatcanberecreated. } } Demo源代码 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |