如何在Store Kit应用内购买中测试订阅取消?

10

苹果公司的沙盒模式下测试订阅购买的文档指出:

“要检查购买是否已取消,请查找收据中的取消日期字段。如果该字段包含日期,无论订阅到期日期如何,购买都已取消--将取消的收据视为从未进行过购买。”

但是收据中没有显示取消日期字段:

"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);

你能找到一种方法在“沙盒测试”账户中测试升级、降级和取消吗? - jose920405
不,Jose。StoreKit 真的不是为此而建立的。您必须解析收据并编写代码来处理这些情况。这是关于此的最新文档:StoreKit。这另一个文档已归档,但我认为它有助于理解整个过程:IAP programming guide - Rodrigo Pinto
1
你好,你找到检查订阅是否取消的方法了吗? - EmBeCoRau
1个回答

0

使用真实的苹果账户。这是我知道的唯一可行的方法。如果您确保您的代码与文档中的示例一起工作,那么应该没问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接