Search code examples
objective-cjsoniphone-sdk-3.0app-store-connectstorekit

Generate JSON object with transactionReceipt


I've been the past days trying to test my first in-app purchse iphone application. Unfortunately I can't find the way to talk to iTunes server to verify the transactionReceipt.

Because it's my first try with this technology I chose to verify the receipt directly from the iPhone instead using server support. But after trying to send the POST request with a JSON onbject created using the JSON api from google code, itunes always returns a strange response (instead the "status = 0" string I wait for).

Here's the code that I use to verify the receipt:

- (void)recordTransaction:(SKPaymentTransaction *)transaction {
    NSString *receiptStr = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"algo mas",@"receipt-data",nil];

    NSString *jsonString = [jsonDictionary JSONRepresentation];
    NSLog(@"string to send: %@",jsonString);

    NSLog(@"JSON Created");
    urlData = [[NSMutableData data] retain];

    //NSURL *sandboxStoreURL = [[NSURL alloc] initWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"will create connection");
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

maybe I'm forgetting something in the request's headers but I think that the problem is in the method I use to create the JSON object.

HEre's how the JSON object looks like before I add it to the HTTPBody :

    string to send: {"receipt-data":"{\n\t\"signature\" = \"AUYMbhY

       ...........

D0gIjEuMCI7Cn0=\";\n\t\"pod\" = \"100\";\n\t\"signing-status\" = \"0\";\n}"}

The responses I've got:

complete response { exception = "java.lang.IllegalArgumentException: Property list parsing failed while attempting to read unquoted string. No allowable characters were found. At line number: 1, column: 0."; status = 21002; }

Thanks a lot for your guidance.


Solution

  • I have just fixed that after 2 days of struggling. You have to encode receipt using Base64 before inserting into json object. Like that (Ruby):

    dataForVerification = {"receipt-data" => Base64.encode64(receipt)}.to_json
    

    Base64 is not mentioned anywhere in the official docs (at least for SDK 3.0), only on a couple of blogs.

    For instance, here the guy encodes the receipt in Base64 before passing it to the PHP server, but does not decode it back in PHP, thus sending Base64-encoded string to iTunes.