quantumpotato
quantumpotato

Reputation: 9767

Validating App store receipt gives DrmInvalidArgumentException

I'm following the documentation here.

https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html

I have a receipt-data from my team that I'm trying to validate, they're getting error code 21002 which was malformed JSON. It looks like they had extra parameters appended to the base64 data, so I tried removing those and sending:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *receipt; // Sent to the server by the device



    // Create the JSON object that describes the request

    NSError *error;

    NSDictionary *requestContents = @{
                                      @"receipt-data": @"<<$mybase64data>>", @"password" : @"<<$thepassword>>"};

    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents options:0 error:&error];



    if (!requestData) { /* ... Handle error ... */ }



    // Create a POST request with the receipt data.

    NSURL *storeURL = [NSURL URLWithString:@"https://buy.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) {

                                   /* ... Handle error ... */

                                   NSLog(@"conerror %@", connectionError);
                               } else {

                                   NSError *error;

                                   NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

                                   NSLog(@"hello %@", jsonResponse);
                                   NSLog(@"error %@", error);

                                   if (!jsonResponse) {

                                   }

                               }

                           }];



}

result:

2017-03-03 22:45:47.454 receipttest[89851:352604] hello {
    exception = "com.apple.jingle.mzfairplay.validators.DrmInvalidArgumentException";
    status = 21002;
}
2017-03-03 22:45:47.455 receipttest[89851:352604] error (null)

Upvotes: 3

Views: 4500

Answers (4)

starry
starry

Reputation: 1

Pay attention to URLENCODE:

- (NSString *)URLEncodedString
{
    // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
    // CharactersToLeaveUnescaped = @"[].";

    NSString *unencodedString = self;
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));

    return encodedString;
}

Upvotes: 0

WickedW
WickedW

Reputation: 2601

I had this issue when using a receipt from a test user that was a couple days old with a yearly auto-renewable subscription.

I checked the above helpful responses about extra characters etc (I also checked I had supplied my app secret for auto-renewable) with no joy.

In the end I tried creating A NEW SANDBOX user and it worked first time 
with no other changes other than the new Receipt!

Hope this helps someone.

Upvotes: 2

Larcho
Larcho

Reputation: 546

Something to keep in mind: In this example the data is sent to Apple straight from the App, but you might do this from a server too. When you testing your server App don't use NSLog() to print your base64 data, NSLog truncates data.

Upvotes: 2

Timo
Timo

Reputation: 166

I also received the same error response for serveral receipts. The solution for me was to remove all occurrences of \r\n.

Maybe you have the same issue. I still haven't figured out when and why these chars are inserted.

Upvotes: 1

Related Questions