Server validation In app purchase receipt of OSX app

364 views Asked by At

I am working on In app Purchase validate on OSX app and get some problem~

My request code like this:

    NSData* data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
    NSString* base64Str = [data base64EncodedString];

    NSDictionary* dict = [NSDictionary dictionaryWithObject:base64Str forKey:@"receipt-data"];
    SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
    NSString* jsonStr = [jsonParser stringWithObject:dict error:nil];
    postUrl = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];

    _verifyRequest = [[NSMutableURLRequest alloc] init];
    [_verifyRequest setTimeoutInterval:_sTimeoutInterval];
    [_verifyRequest setURL:postUrl];
    [_verifyRequest setHTTPBody:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
    [_verifyRequest setHTTPMethod:ASCommon_HttpMethodPost];
    _verifyConnection = [[NSURLConnection alloc] initWithRequest:_verifyRequest delegate:self];
    [_verifyConnection start];

I get the result like this

{
    exception = "java.lang.IllegalArgumentException";
    status = 21002;
}

And I log the jsonStr variable seems have no problem:

{"receipt-data":"..........................................................."}

Apple doc here said "Retrieve the receipt data. ...... On OS X, this is the entire contents of the receipt file inside the application bundle. Encode the receipt data using base64 encoding."

Do I misunderstand the receipt data is entire of contents of file? Or something wrong with my code?

1

There are 1 answers

0
VBK On

I think i found a solution. The base64 encoded string class by default doesnt remove line breaks. You need to change it to remove line breaks. Change this function:

- (NSString *)base64EncodedString
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], true, &outputLength);

    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

to

- (NSString *)base64EncodedString : (BOOL)includeLineBreaks
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], includeLineBreaks, &outputLength);

    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

and pass NO in the parameter.