How can I test a cancellation of subscription in Store Kit in-app purchase?

早过忘川 提交于 2019-12-10 02:19:11

问题


Apple's documentation to test a subscription purchase in Sandbox mode says that:

"To check whether a purchase has been canceled, look for the Cancellation Date field in the receipt. If the field has a date in it, regardless of the subscription’s expiration date, the purchase has been canceled—treat a canceled receipt the same as if no purchase had ever been made."

But the receipt does not show a Cancellation Date field:

"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 says not to manage user's subscription, and refer to iTunes instead:

"Rather than needing to code your own subscription management UI, your app can open the following URL: https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/manageSubscriptions Opening this URL launches iTunes or iTunes Store, and then displays the Manage Subscription page."

But this is not possible to be tested with a Sandbox account (requires credit card). And I really need to test for cancelled subscriptions.

Here is the code I use to get the receipt:

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:


Use a real apple account. That is the only way I know of that work. If you make sure that your code is working with the example in the documentation you should be fine.



来源:https://stackoverflow.com/questions/29977795/how-can-i-test-a-cancellation-of-subscription-in-store-kit-in-app-purchase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!