苹果公司的沙盒模式下测试订阅购买的文档指出:
“要检查购买是否已取消,请查找收据中的取消日期字段。如果该字段包含日期,无论订阅到期日期如何,购买都已取消--将取消的收据视为从未进行过购买。”
但是收据中没有显示取消日期字段:
"expires_date" = "2015-04-29 16:46:26 Etc/GMT";
"expires_date_ms" = 1430325986000;
"expires_date_pst" = "2015-04-29 09:46:26 America/Los_Angeles";
"is_trial_period" = false;
"original_purchase_date" = "2015-04-29 16:27:40 Etc/GMT";
"original_purchase_date_ms" = 1430324860000;
"original_purchase_date_pst" = "2015-04-29 09:27:40 America/Los_Angeles";
"original_transaction_id" = 1000000153387111;
"product_id" = ...;
"purchase_date" = "2015-04-29 16:31:26 Etc/GMT";
"purchase_date_ms" = 1430325086000;
"purchase_date_pst" = "2015-04-29 09:31:26 America/Los_Angeles";
quantity = 1;
"transaction_id" = ...;
"web_order_line_item_id" = ...;
苹果公司表示不要管理用户的订阅,应该转而使用iTunes:
“你的应用程序可以打开以下网址,而无需编写自己的订阅管理UI:https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/manageSubscriptions。打开此网址会启动iTunes或iTunes Store,并显示“管理订阅”页面。”
但是这无法使用沙盒账户进行测试(需要信用卡)。我真的需要测试取消的订阅。
这是我用来获取收据的代码:
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:receiptURL];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *receipt = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (receipt) {
// Create the JSON object that describes the request
NSError *error;
NSDictionary *requestContents = @{
@"receipt-data": [receipt base64EncodedStringWithOptions:0],
@"password": @"...",
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:&error];
if (!requestData) {
NSLog(@"Unable to serialise json data. Possible user/password mistake. Error: %@", error);
}
// Create a POST request with the receipt data.
NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];
// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"Connection error: %@", connectionError);
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray *receiptInfo = jsonResponse[@"latest_receipt_info"];
NSInteger total = receiptInfo.count;
NSDictionary *receiptDict = receiptInfo[total-1];
NSLog(@"Json: %@", receiptInfo);