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

时间:2015-04-30 21:10:11

标签: ios objective-c iphone in-app-purchase storekit

Apple在沙盒模式下的documentation to test a subscription购买说:

  

“要检查购买是否已取消,请在收据中查找取消日期字段。如果该字段中包含日期,则无论订阅的到期日期如何,购买都已取消 - 处理已取消的收据就好像没有购买过一样。“

但收据未显示取消日期字段:

"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" = ...;

Apple表示不会管理用户的订阅,而是转而使用iTunes:

  

“您的应用可以打开以下网址,而不需要编写自己的订阅管理用户界面代码:https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/manageSubscriptions   打开此URL会启动iTunes或iTunes Store,然后显示“管理订阅”页面。“

但是无法使用Sandbox帐户(需要信用卡)进行测试。我真的需要测试已取消的订阅。

以下是我用来收据的代码:

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

1 个答案:

答案 0 :(得分:0)

使用真正的苹果帐户。这是我了解这项工作的唯一方式。如果您确保您的代码正在使用文档中的示例,那么您应该没问题。