開發iOS最重要的就是支付了,天朝之內最常用的就是支付寶了,下面就以自己的經歷說明如何整合支付寶+遇見的坑.

首先,整合支付寶最好別使用Cocoapods,很多人都說使用起來很方便,可是我每次只要使用Cocoapods匯入支付寶SDK,總是出現各種莫名其妙的錯誤,並且還每次都不一樣,最終,我只能手動匯入.

其實可以使用ping++和其他更為方便.如

http://winann.blog.51cto.com/4424329/1601654

https://www.pingxx.com/

以自己整合支付寶為例:

1.在支付寶開放平臺下載支付寶SDK,把以下檔案直接拷入工程.

2.新增相應的依賴庫.選擇"target"->"Link Binary With Libraries"

3.編譯,坑隨之而來,開始填坑.

解決:在相應檔案中,匯入

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

3.1如果出現

就把官方demo中的下面兩個檔案拷進工程即可,原因不知.

3.2一般也會出現

,接著匯入這個庫

 

4.

解決:出現類似找不到檔案的情況,Targets->Build Settings->Header Search Path新增路徑.

直接將專案中的相應檔案拖入即可.也可以$(SRCROOT)/檔案路徑.

至此,基本的工作完成.下面開始整合程式碼.

 

首先,在appDelegate.m中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    //跳轉支付寶支付,處理支付結果
//    [[AlipaySDK defaultService]processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//        NSLog(@"result = %@",resultDic);
//    }];
     
    if ([url.host isEqualToString:@"safepay"]) {
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    if ([url.host isEqualToString:@"platformapi"]){//支付寶錢包快登授權返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

 這兩個檔案直接用官方的即可.

接著,在Controller中,新增另外一個類,這樣方便.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
//  OrderViewController.h
//  app
//
//  Created by shaoting on 16/1/20.
//  Copyright © 2016年 9elephas. All rights reserved.
//
 
#import <UIKit/UIKit.h>
@interface Product : NSObject{
@private
    float     _price;
    NSString *_subject;
    NSString *_body;
    NSString *_orderId;
}
 
@property (nonatomic, assign) float price;
@property (nonatomiccopyNSString *subject;
@property (nonatomiccopyNSString *body;
@property (nonatomiccopyNSString *orderId;
+(id)sharedUserDefault;
 
@end
 
@interface OrderViewController : UIViewController
 
@property(nonatomic, strong)NSMutableArray *productList;
 
@end

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
//  OrderViewController.m
//  app
//
//  Created by shaoting on 16/1/20.
//  Copyright © 2016年 9elephas. All rights reserved.
//
 
#import "OrderViewController.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
 
 
@implementation Product
static  Product * product = nil;
 
+(id)sharedUserDefault
{
    
        @synchronized(self)
        {
            if(product==nil)
            {
                product=[[Product alloc] init];
            }
        }
    return product;
}
#pragma mark 通過HTML5介面獲取product.subject body price
-(void)getProductInfo{
     
}
 
@end
 
 
 
 
@interface OrderViewController ()
 
@end
 
@implementation OrderViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
     
    [[Product sharedUserDefault] getProductInfo]; //通過該方法呼叫其他類的單例方法,接著呼叫物件方法,該物件方法會獲取到HTML5介面上的資訊
    // Do any additional setup after loading the view from its nib.
}
 
 
#pragma mark   隨機產生訂單號
-(NSString *)generateTradeNO{
    static int kNumber = 15;
    NSString * sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString * resultStr = [[NSMutableString alloc]init];
    srand(time(0)); //time(0)得到當前的時間值.srand(time(0))可以保證每次到可以得到不一樣的種子值.計算機中沒有真正的隨機值
    //如果不用srand(time(0))的話,第二次的訂單號就和第一次一樣了
    for (int i = 0; i<kNumber; i++) {
        unsigned index = rand()%[sourceStr length];
        NSString * oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}
-(void)goPay{
  NSString * partner = @"從後臺獲取,保證安全";
  NSString * seller = @"從後臺獲取,保證安全";
  NSString * privateKey = @"從後臺獲取,保證安全";
   
    if ([partner length] == 0 || [seller length] == 0 || [privateKey length] == 0) {
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"發生錯誤" preferredStyle:UIAlertControllerStyleAlert];
         
        UIAlertAction * alert = [UIAlertAction actionWithTitle:@"缺少partner或者seller或者私鑰" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //之後做相應處理
        }];
        [alertC addAction:alert];
        [self presentViewController:alertC animated:YES completion:nil];
    }
    //生成訂單資訊以及簽名
    Order * order = [[Order alloc]init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = [self generateTradeNO];  //訂單ID(隨機產生15位)
    order.productName = product.subject;//商品名
    order.productDescription = product.body;//商品描述
    order.amount = [NSString stringWithFormat:@"%.2f",product.price];//價格
    order.notifyURL = @""///回撥URL
     
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
     
    //應用註冊scheme,在Info.plist定義URL types
    NSString *appScheme = @"alisdkdemo";
     
    //將商品資訊拼接成字串
    NSString * orderSpec = [order description];
    //獲取私鑰並將商戶資訊簽名,外部商戶可以根據情況存放私鑰和簽名,只需要遵循RSA簽名規範,並將簽名字串base64編碼和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    //將簽名成功字串格式化為訂單字串,請嚴格按照該格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
         
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            //支付完成
            NSLog(@"reslut = %@",resultDic);
        }];
    }
    //取消選中
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end

 因為我的是點選h5介面上的購買按鈕跳轉至iOS源生實現購買流程的,所以一些程式碼可能不同,但是大同小異.

另:因為iOS9的原因,需要配置下專案,如:

https:

白名單:

更多白名單http://www.cnblogs.com/shaoting/p/5148323.html

URL types:

這就是基本的支付寶整合了.

 http://download.csdn.net/detail/shaoting19910730/9419368  demo下載