iOS 開發中關於支付功能的學習心得 ----支付寶

 .支付寶支付

      我學習的方法是:1.下載SDK 和Demo 然後運行一遍

                                   2.運用在自己實際的項目中

      學習結果:          1.在需要用到支付寶進行支付功能的地方寫:

                                 1) (我不需要處理訂單信息,因爲我們公司會讓我們調用接口獲得處理後的訂單信息,所以下面的payOrder是調用接口獲取到的)

                                 2)   下面的KURLscheme 就是支付寶 要返回能找到你的APP的重要信息。他就是 你APP的bundle ID

                                  注意的是要添加一個東西,就是要在target-》info -》URL Types 裏面增加 URLScheme:就是你的bundleID 那個identify:alipay 

                                     增加完你就會發現 info.plist裏面也有了

 [[         [[AlipaySDK defaultService] payOrder:payOrder  fromScheme:kURLscheme callback:^(NSDictionary                         *resultDic)
             {
                 
                 
                 NSLog(@"reslut = %@",resultDic);
                 
                 NSNumber *resultStatus = [resultDic objectForKey:@"resultStatus"];
                
                 if([resultStatus intValue] == 9000){
                     NSLog(@"支付寶支付------------");
                     [self alipaysuccess];
                     
                 }else{
                     
                     [SVProgressHUD showErrorWithStatus:@"交易失敗"];
                     
                 }
                 
                 
             }];


           重要提醒: 如果你的手機沒有裝支付寶客戶端,那麼就會用網頁支付,然後返回回來就是調用callback裏面的方法

                    如果你的手機裝了支付寶客戶端,就是下面我會寫的

             3.你如果調用了上面的方法,而且信息正確的話就可以跳轉到支付寶客戶端或者支付寶的網頁進行支付

             4.支付完,返回到你的APP,如果你是支付寶客戶端返回回來,那麼你要在你的AppDelegate.m文件裏面增加相關的方法

                1)handleOpenURL 

  •                                     (AlixPayResult )handleOpenURL:(NSURL )url { 

                                                AlixPayResult * result = nil;

                                             if (url != nil && [[url host] compare:@"safepay"] == 0) {

                                                       result = [self resultFromURL:url];

                                               }

                                                 return result;

                                               }


                               2)resultFromURL

                                (AlixPayResult )resultFromURL:(NSURL )url { 

                                               NSString * query = [[url query] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

                                               return    [[AlixPayResult alloc] initWithString:query];

                                        }

                               3)// NOTE: 9.0以後使用新API接口 openURL  (微信支付返回也會進入這個方法,所以要做個判斷)

                                - (BOOL)application:(UIApplication )app openURL:(NSURL )url options:(NSDictionary<NSString, id> )options

                                {                

                                           NSString op=options[@"UIApplicationOpenURLOptionsSourceApplicationKey"];

                                           if([op isEqualToString:@"com.tencent.xin"])  //如果是微信的

                                     {

                                              return [WXApi handleOpenURL:url delegate:self];

                                     }

                                         else{ 

                                               if ([url.host isEqualToString:@"safepay"])

                                      { //跳轉支付寶錢包進行支付 處理支付結果

                                                 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary resultDic) 

                                          { 

                                                   NSLog(@"result = %@",resultDic);

                                          }];

                                       [self parse:url application:app]; 

                                         } 

                                           }

                                   return YES;

                                  }

                             4) application

    • (void)parse:(NSURL )url application:(UIApplication )application { 

      [SVProgressHUD dismiss];

      //結果處理 AlixPayResult* result = [self handleOpenURL:url];

      if (result) {

        if (result.statusCode == 9000)
        {
      
            //交易成功
            [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccess" object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"alipaysuccess" object:nil];
      
        }
        else
        {
            //交易失敗
            [[NSNotificationCenter defaultCenter] postNotificationName:@"alipayError" object:nil];
        }
      

      } else { //失敗 [[NSNotificationCenter defaultCenter] postNotificationName:@"alipayError" object:nil]; }

    }

    就是上面幾個方法要寫,最後處理結果的時候,用到了訂閱者和發佈者模式,所以要在你的調用支付功能的頁面加上,(寫在viewDidLoad方法裏面)

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipaysuccess) name:@"alipaysuccess" object:nil]; 

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipayError) name:@"alipayError" object:nil];

 並且要寫上這兩個方法

#pragma  mark 支付結果
-(void)alipaysuccess{
   // NSLog(@"支付成功");
    [SVProgressHUD showSuccessWithStatus:@"支付成功"];
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)alipayError{
    // NSLog(@"支付失敗");
    [SVProgressHUD showErrorWithStatus:@"支付失敗"];
    
}

       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章