【问题标题】:what receipt for auto-renewable subscription should i validate?我应该验证什么自动续订订阅收据?
【发布时间】:2013-03-08 07:04:31
【问题描述】:

我正在使用 IAP 开发应用程序的服务器端,我知道当用户进行订阅时我需要将收据从应用程序发送到我的服务器,然后通过应用商店验证收据的状态,日期到期等,并为用户提供内容。

但我的问题是,我需要什么收据来检查续订状态?我的意思是,我第一次检查收据时,应用商店会给我一张收据、状态和最新收据,这张最新收据是我下次应该用来检查状态的收据,还是我应该始终使用原始收据?我一直在对他们两个进行测试,他们在应用商店中给了我相同的状态,但我不确定正确的做法是什么。

谢谢

【问题讨论】:

    标签: ios in-app-purchase server-side receipt


    【解决方案1】:

    今天,我遇到了这个问题。

    在这里关注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;
        }
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您的交易完成后,您将获得此交易的 tansaction 和 trasaction.reciept 您想要提供 base64 的收据,这是此代码

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

      此 jsonObjectString 会将其保存到服务器,并在验证收据时为该 verify this link 提供 sharedsecret

      【讨论】:

      • 感谢您的链接,我已经有用于验证收据的 php 脚本并且工作正常,但是当我收到来自应用商店的回复时,我每次订阅时都会收到状态、收据和 latest_receitp更新了,所以我对如何处理最后一张收据感到困惑,我应该将它保存在我的服务器中并在每次需要检查订阅状态时使用它还是使用原始收据(生成的那个交易时间)就够了吗?
      • 您可以简单地存储原始收据,然后每次将其发送给 Apple 进行验证。对于 ARS,Apple 将始终回复该产品 ID 的最新收据。所以你不必存储新的收据
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 2023-03-29
      • 2015-08-09
      相关资源
      最近更新 更多