2013-03-08 148 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; 
    } 
} 

希望得到这个帮助。

相关问题