蘋果IAP內付費二次驗證程式碼

峻峰飛陽發表於2016-12-28
1. 驗證邏輯在客戶端實現:
  1. // iapData 使用者購成功的transactionReceipt
  2. -(BOOL)putStringToItunes:(NSData*)iapData
  3. {
  4.     NSString*encodingStr = [iapData base64EncodedString];
  5.     
  6.     NSString *URL=@URL_VerifyReceipt;
  7.     
  8.     // https://sandbox.itunes.apple.com/verifyReceipt 測試地址
  9.     // https://buy.itunes.apple.com/verifyReceipt 正式釋出驗證地址
  10.     
  11.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];// autorelease];
  12.     [request setURL:[NSURL URLWithString:URL]];
  13.     [request setHTTPMethod:@"POST"];
  14.     //設定contentType
  15.     [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  16.     //設定Content-Length
  17.     [request setValue:[NSString stringWithFormat:@"%d", [encodingStr length]] forHTTPHeaderField:@"Content-Length"];
  18.     
  19.     NSDictionary* body = [NSDictionary dictionaryWithObjectsAndKeys:encodingStr, @"receipt-data", nil];
  20.     SBJsonWriter *writer = [SBJsonWriter new];
  21.     [request setHTTPBody:[[writer stringWithObject:body] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
  22.     NSHTTPURLResponse *urlResponse=nil;
  23.     NSError *errorr=nil;
  24.     NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
  25.                                                  returningResponse:&urlResponse
  26.                                                              error:&errorr];
  27.     
  28.     //解析
  29.     NSString *results=[[NSString alloc]initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSUTF8StringEncoding];
  30.     
  31.     printf(" \n MKStoreManager::putStringToItunes: %s \n", [results UTF8String]);
  32.     
  33.     NSDictionary*dic = [results JSONValue];
  34.     if([[dic objectForKey:@"status"] intValue]==0) //注意,status=@"0" 是驗證收據成功
  35.     {
  36.         return true;
  37.     }
  38.     return false;
  39. }

  1. // 交易成功
  2. -(void) provideContent: (SKPaymentTransaction *)transaction
  3. {
  4.     if(delegate && [delegate respondsToSelector:@selector(productPurchasedSuccess:)])
  5.     {
  6.         if (!isNetworkOK()) {
  7.             [delegate productPurchasedFailed:transaction.payment.productIdentifier];
  8.             return;
  9.         }
  10.         if([self putStringToItunes:transaction.transactionReceipt])
  11.         {
  12.             printf("putStringToItunes check success!!! \n");
  13.             [delegate productPurchasedSuccess:transaction.payment.productIdentifier];
  14.         }else{
  15.             [delegate productPurchasedFailed:transaction.payment.productIdentifier];
  16.             
  17.         }
  18.     }
  19. }


2. 驗證邏輯放在伺服器端。

實現程式碼如下,

需要上傳客戶端得到的  NSString*encodingStr = [iapData base64EncodedString]; 資料


  1. <?php
  2. /**
  3.  * @說明:        iap 購買伺服器驗證邏輯
  4.  * @作者:        linux_wuliqiang@163.com
  5.  *
  6.  * @data:         2013-05-06
  7.  *
  8.  * @備註:        客戶端進行 iap 購買後,需要伺服器再次進行驗證。確定玩家是否購買成功
  9.  *
  10.  *
  11.  */

  12. class BaseIapCheck
  13. {
  14.     // 是否為沙盒測試環境
  15.     const IapCheck_IsSandBox = true;
  16.     
  17.     
  18.     /**
  19.      * 得到 iap 購買的單據資料,如果成功購買了,返回正常的購買資料,否則返回 null
  20.      * string $receipt, 客戶端 iap 購買時,返回的單據資料, 此資料是在客戶端經過 NSString*encodingStr = [iapData base64EncodedString]; 處理後的資料
  21.      *
  22.      * return ,驗證成功,返回正常的購買資料,驗證失敗,返回 null
  23.      * 
  24.      * 備註:可以通過 product_id 來判定具體購買的是哪一個收費道具
  25.      */
  26.     public static function GetReceiptData($receipt)
  27.     {
  28.         if (self::IapCheck_IsSandBox) 
  29.         {
  30.             $url = 'https://sandbox.itunes.apple.com/verifyReceipt';
  31.         }
  32.         else 
  33.         {
  34.             $url = 'https://buy.itunes.apple.com/verifyReceipt';
  35.         }
  36.     
  37.         $postDataJson = json_encode(array('receipt-data' => $receipt));
  38.         $opts = array
  39.         (
  40.                 'http' => array
  41.                 (
  42.                         'method' => 'POST',
  43.                         'header'=> "Content-type: application/json" .                        // 必須設定為 application/json 格式
  44.                         "Content-Length: " . strlen($postDataJson) . "\r\n",
  45.                         'content' => $postDataJson
  46.                 )
  47.         );
  48.         
  49.         //生成請求的控制程式碼檔案
  50.         $context = stream_context_create($opts);
  51.         $html = file_get_contents($url, false, $context);
  52.         $data = json_decode($html);

  53. //         echo '
    ';

  54. //         echo '$html 
    ';

  55. //         var_dump($html);
  56. //         echo '
    ';

  57. //         echo 'data 
    ';

  58. //         var_dump($data);
  59. //         echo '
    ';

  60.         
  61.         //判斷返回的資料是否是物件
  62.         if (!is_object($data)) 
  63.         {
  64.             return null;
  65.         }
  66.         
  67.         //判斷是否購買成功
  68.         if (!isset($data->status) || $data->status != 0) 
  69.         {
  70.             return null;
  71.         }
  72.     
  73.         //返回產品的資訊
  74.         return array(
  75.                 'quantity' => $data->receipt->quantity,
  76.                 'product_id' => $data->receipt->product_id,
  77.                 'transaction_id' => $data->receipt->transaction_id,
  78.                 'purchase_date' => $data->receipt->purchase_date,
  79.                 'item_id' => $data->receipt->item_id,
  80.                 'bid' => $data->receipt->bid,
  81.                 'bvrs' => $data->receipt->bvrs
  82.         );
  83.     }
  84. }


  85. ?>

相關文章