如何在应用购买收据中阅读iOS

时间:2018-03-26 12:18:09

标签: ios iphone in-app-purchase itunesconnect auto-renewable

我能够阅读应用内购买的收据(在沙箱中)。我想验证它们是否可以自动续订。但问题是,当我阅读收据时, purchase_date expires_date 的日期相同。以下是我得到的答复:

{
    "expires_date" = "2018-03-26 09:34:11 Etc/GMT";
    "expires_date_ms" = 1522056851000;
    "expires_date_pst" = "2018-03-26 02:34:11 America/Los_Angeles";
    "is_in_intro_offer_period" = false;
    "is_trial_period" = false;
    "original_purchase_date" = "2018-03-26 09:29:15 Etc/GMT";
    "original_purchase_date_ms" = 1522056555000;
    "original_purchase_date_pst" = "2018-03-26 02:29:15 America/Los_Angeles";
    "original_transaction_id" = 1000000385357059343435656;
    "product_id" = <product id>;
    "purchase_date" = "2018-03-26 09:29:11 Etc/GMT";
    "purchase_date_ms" = 1522056551000;
    "purchase_date_pst" = "2018-03-26 02:29:11 America/Los_Angeles";
    quantity = 1;
    "transaction_id" = 1000000385357059343435656;
    "web_order_line_item_id" = 1000000385357059343435656;
}

如果我做错了,请告诉我。任何帮助表示赞赏。

PS:订阅是一个月,我今天购买了这个(即2012年3月26日)。所以我认为 expires_date 应该是22年2月22日。

2 个答案:

答案 0 :(得分:1)

使用https://sandbox.itunes.apple.com/verifyReceipt网址验证收据。

以下是您可以使用的示例代码:

NSString *appStoreReceiptPath = NSBundle.mainBundle.appStoreReceiptURL.path;
if(appStoreReceiptPath) {
    NSData *receiptData = [NSData dataWithContentsOfFile:appStoreReceiptPath];
    if(receiptData == nil) return;
    NSDictionary *receiptDictionary = @{@"receipt-data": [receiptData base64Encoding],
                                        @"password": SharedSecret};

    NSData *requestData = [NSJSONSerialization dataWithJSONObject:receiptDictionary options:0 error:nil];
    // ReceiptURL is - @"https://sandbox.itunes.apple.com/verifyReceipt"
    NSURL *storeURL = [NSURL URLWithString:ReceiptURL];
    NSMutableURLRequest *storeRequest = [[NSMutableURLRequest alloc] initWithURL:storeURL];
    storeRequest.HTTPMethod = @"POST";
    storeRequest.HTTPBody = requestData;

    NSURLSession *session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
    [[session dataTaskWithRequest:storeRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(!error) {
            NSError *jsonError = nil;
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];

            if(!jsonError) {
                for(NSDictionary *in_app in dictionary[@"receipt"][@"in_app"]) {
                    NSString *purchaseDateString = in_app[@"purchase_date"];
                    NSString *expiresDateString = in_app[@"expires_date"];

                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss VV"];

                    NSString *productId = in_app[@"product_id"];
                    NSString *purchaseDate = [dateFormatter dateFromString:purchaseDateString];
                    NSString *expiresDate = [dateFormatter dateFromString:expiresDateString];


                }
            }
        }
    }] resume];
}

并使用此方法检测订阅是否有效。

- (BOOL) isActiveSubscriptionBought:(NSDate *) purchaseDate expires:(NSDate*) expiresDate  {
    if(self.expiresDate == nil) return false;

    NSDate *date = [NSDate date];
    return (([date compare:purchaseDate] != NSOrderedAscending) &&
            ([date compare:expiresDate] != NSOrderedDescending));
}

答案 1 :(得分:1)

一切都是正确的:在沙盒中持续时间不同,所以不是1个月就会得到5分钟。有关详细信息,请参阅官方文档:https://help.apple.com/itunes-connect/developer/#/dev7e89e149d

此外,与@ jaba-odishelashvili指出的一样,您应该使用不同的URL来验证沙箱/生产收据。