views:

2956

answers:

3

Hi,

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.

+8  A: 

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.

Oleg Andreev
Thank's a lot for your answer!!
Carlos
Thank you so much for pointing this out!
Joe Freeman
A: 

Hi how to send Transition receipt along with product identifier to my server using HTTP Post method weather i have to encode product identifier using base 64...?

i am getting response {"message" : "21002: java.lang.IllegalArgumentException: propertyListFromString parsed an object, but there's still more text in the string. A plist should contain only one top-level object. Line number: 1, column: 2478.", "error" : 1}

even after i am encoding the Transition receipt with base64 encoding...

jeeva
+1  A: 

Re: "21002: java.lang.IllegalArgumentException: propertyListFromString parsed an object, but there's still more text in the string.:"

I fixed a similar issue in my code by wrapping the receipt data in {} before encoding.

The resulting receipt looks like:

{
    "signature" = "A[...]OSzQ==";
    "purchase-info" = "ew[...]fQ==";
    "pod" = "100";
    "signing-status" = "0";
}

Here's the code I use:

receipt = "{%s}" % receipt    // This step was not specified - trial and error
encoded = base64.b64encode(receipt)
fullpost = '{ "receipt-data" : "%s" }' % encoded
req = urllib2.Request(url, fullpost)
response = urllib2.urlopen(req)

Apple's Response:

{"receipt":{"item_id":"371235", "original_transaction_id":"1012307", "bvrs":"1.0", "product_id":"com.foo.cup", "purchase_date":"2010-05-25 21:05:36 Etc/GMT", "quantity":"1", "bid":"com.foo.messenger", "original_purchase_date":"2010-05-25 21:05:36 Etc/GMT", "transaction_id":"11237"}, "status":0}

Good luck!

Phil Lockwood