溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

swift調(diào)用支付寶

發(fā)布時(shí)間:2020-07-11 19:51:13 來(lái)源:網(wǎng)絡(luò) 閱讀:1189 作者:桂素偉 欄目:移動(dòng)開發(fā)

開發(fā)環(huán)境xcode7.1 運(yùn)行環(huán)境 IOS9.1

到支付寶面面下載IOS的移動(dòng)支付功能的SDKAndroidIOS是同一個(gè)zip文件下)

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

然后申請(qǐng)商家支付寶,得到相應(yīng)的private_keypartner,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文件作為頭文件)

    swift調(diào)用支付寶

  • 如果在基于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.hopenssl_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源代碼

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI