如何在应用程序购买收据中读取 iOS

How to read iOS in app purchase receipt

我能够阅读应用内购买(在沙盒中)的收据。我想验证它们的自动更新订阅。但问题是,当我阅读收据时,purchase_dateexpires_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:订阅期为一个月,我今天(即 2018 年 3 月 26 日)购买了这个。所以我认为 expires_date 应该是 22April2018.

使用https://sandbox.itunes.apple.com/verifyReceipturl验证收货。

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

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 个月,而是 5 分钟。详情见官方文档:https://help.apple.com/itunes-connect/developer/#/dev7e89e149d

另外,就像@jaba-odishelashvili 指出的那样,您应该使用不同的 URL 来验证 Sandbox/Production 收据。