支付寶APP支付

一 商家和支付寶簽約,獲取取partnerID

  • 搜索支付寶商家,登錄商家中心
1.png
  • 點擊簽約訂單,即可查看


    2.png

二.進入合作伙伴秘鑰管理,給支付寶平臺設置公鑰,圖中為設置完成樣式,秘鑰的生成在開發文檔中有具體說明,不在闡述.

4.png

三.APP支付是需要簽約才可使用,需到產品大全APP支付中簽約

5.png
![7.png](http://upload-images.jianshu.io/upload_images/2810736-71d983f53de46e6f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  • 簽約需要上傳公司資質還有一份APP的說明文檔,文檔可以問產品要

四 iOS集成

  • 進入支付寶開放平臺中的文檔中心,選擇APP支付
![11.png](http://upload-images.jianshu.io/upload_images/2810736-85449cf1557cf366.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  • 集成時使用新版,進入支付寶頁面時總是提示我系統繁忙,所以用的是老版本的移動支付(主要是一個order文件的不一致,后面會介紹),哪位大神知道什么原因可以留言我.


    8.png

9.png

具體代碼如下

 (void)doAlipayPay
{
    //和新版本的區別是新版班需要到開放平臺注冊應用,獲取appID
    /*
//這里只是為了方便直接向商戶展示支付寶的整個支付流程;所以Demo中加簽過程直接放在客戶端完成;
    //真實App里,privateKey等數據嚴禁放在客戶端,加簽過程務必要放在服務端完成;
    //防止商戶私密數據泄露,造成不必要的資金損失,及面臨各種安全風險;
   { 
    /*============================================================================*/
    /*=======================新版本需要填寫商戶app申請的,括號這部分不用集成===================================*/
    /*============================================================================*/
    NSString *appID = @"2016***24";
    NSString *privateKey = @"***"; //注意由于字符過長換行時需加上轉義字符"/"
    /*============================================================================*/
    }
    /*============================================================================*/
    /*=======================老版本需要填寫商戶app申請的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088***14";
    NSString *seller = @"2088***14";
    NSString *privateKey = @"**";
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller獲取失敗,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私鑰。"
                                                       delegate:self
                                              cancelButtonTitle:@"確定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    /*
     *生成訂單信息及簽名
     */
    //將商品信息賦予AlixPayOrder的成員變量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.sellerID = seller;
    order.outTradeNO = [self generateTradeNO]; //訂單ID(由商家自行制定)
    order.subject = @"1"; //商品標題
    order.body = @"測試"; //商品描述
    order.totalFee = [NSString stringWithFormat:@"%.2f",0.01]; //商品價格
    order.notifyURL =  @"https://www.baidu.com"; //回調URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showURL = @"m.alipay.com";
    
    //應用注冊scheme,在AlixPayDemo-Info.plist定義URL types
    NSString *appScheme = @"AirBk2016";
    
    //將商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //獲取私鑰并將商戶信息簽名,外部商戶可以根據情況存放私鑰和簽名,只需要遵循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);
        }];
    }

}

- (NSString *)generateTradeNO
{
    static int kNumber = 15;
    
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)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;
}

代理方法中添加

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    if ([url.host isEqualToString:@"safepay"]) {
        //跳轉支付寶錢包進行支付,處理支付結果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳轉支付寶錢包進行支付,處理支付結果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容