2013-03-08 150 views
0

我正在用IAP開發應用程序的服務器端,我知道當用戶進行訂閱時,我需要將收據從應用程序發送到我的服務器,然後驗證與應用程序商店有關的狀態,日期的收據到期等,&給用戶的內容。我應驗證哪些收據用於自動續訂訂閱?

但我的問題是,我需要什麼收據檢查續約狀態?我的意思是,我第一次檢查收據時應用商店給我的收據,狀態和最新的收據,這個最新的收據是我應該在下一次檢查狀態時使用的收據,還是應該始終使用原始收據?我一直在測試他們兩人,他們給我的應用程序商店相同的地位,但我不知道什麼是正確的做法。

感謝

回答

0

您的交易完成後,你會含有交易得到& trasaction.reciept該交易收據,你要在這裏提供Base64是這個

NSString *jsonObjectString = [self encodeBase64:(uint8_t*)transaction.transactionReceipt.bytes 
             length:transaction.transactionReceipt.length]; 

這jsonObjectString將其保存到代碼服務器,同時驗證你想提供共享祕密爲此verify this link

+0

感謝您的鏈接,我已經驗證了收據和工作正常的PHP腳本,但是當我從應用程序商店收到回覆時,每次訂閱續訂時我都會收到狀態,收據和latest_receitp,我不知道該如何處理最後一張收據,我應該將它保存在我的服務器中,並且每次需要檢查訂閱的狀態或原始收據(在交易完成時生成的收據) 足夠? – 2013-03-10 05:41:56

+0

您可以簡單地存儲原始收據,並將其每次發送給蘋果公司進行驗證。對於ARS,Apple會始終以該產品ID的最新收據作爲迴應。所以你不一定要存儲新的收據 – 2013-03-11 11:39:21

0

今天,我有這個問題的麻煩。

跟着Apple doc在這裏,我用這種方式來查詢訂閱是否過期。

+ (BOOL)checkInAppPurchaseStatus 
{ 
    // Load the receipt from the app bundle. 
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; 
    NSData *receipt = [NSData dataWithContentsOfURL:receiptURL]; 
    if (receipt) { 
     BOOL sandbox = [[receiptURL lastPathComponent] isEqualToString:@"sandboxReceipt"]; 
     // Create the JSON object that describes the request 
     NSError *error; 
     NSDictionary *requestContents = @{ 
              @"receipt-data": [receipt base64EncodedStringWithOptions:0],@"password":@"SHARE_SECRET_CODE" 
              }; 
     NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents 
                   options:0 
                   error:&error]; 

     if (requestData) { 
      // Create a POST request with the receipt data. 
      NSURL *storeURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"]; 
      if (sandbox) { 
       storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]; 
      } 
      NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL]; 
      [storeRequest setHTTPMethod:@"POST"]; 
      [storeRequest setHTTPBody:requestData]; 

      BOOL rs = NO; 
      //Can use sendAsynchronousRequest to request to Apple API, here I use sendSynchronousRequest 
      NSError *error; 
      NSURLResponse *response; 
      NSData *resData = [NSURLConnection sendSynchronousRequest:storeRequest returningResponse:&response error:&error]; 
      if (error) { 
       rs = NO; 
      } 
      else 
      { 
       NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:resData options:0 error:&error]; 
       if (!jsonResponse) { 
        rs = NO; 
       } 
       else 
       { 
        NSLog(@"jsonResponse:%@", jsonResponse); 

        NSDictionary *dictLatestReceiptsInfo = jsonResponse[@"latest_receipt_info"]; 
        long long int expirationDateMs = [[dictLatestReceiptsInfo valueForKeyPath:@"@max.expires_date_ms"] longLongValue]; 
        long long requestDateMs = [jsonResponse[@"receipt"][@"request_date_ms"] longLongValue]; 
        NSLog(@"%lld--%lld", expirationDateMs, requestDateMs); 
        rs = [[jsonResponse objectForKey:@"status"] integerValue] == 0 && (expirationDateMs > requestDateMs); 
       } 
      } 
      return rs; 
     } 
     else 
     { 
      return NO; 
     } 
    } 
    else 
    { 
     return NO; 
    } 
} 

希望得到這個幫助。

相關問題