iOS Question In-App Purchases Receipt validation (SOLVED)

walterf25

Expert
Licensed User
Hello All, i have a need to validate the receipts from in-app purchases, i have found lots of threads here in the forums about this but i don't find a conclusive answer, i have also search google and have found various php scripts, and have tried a few of them, but none of them seem to work.

I followed Ere's code to get the receipt information from the product with the following code:

B4X:
Dim no As NativeObject = Product
                Dim b() As Byte = no.NSDataToArray(no.GetField("transactionReceipt"))
That code gives me the following string after i purchase a subscription that I'm testing using the Sandbox
{
"signature" = "A6THysyqaesU8PrBitz7MC2wCG7TEUJnxRb1qzwBjjiueVj3vFuxEmIxRAjhrBS5bk3ogv8S0fkl61APJHFCh4ILz6gz7P6Zzmzln9ffbvJvzt/QxlqJah8xY2LtA2BFmprxYFRg8va1+8DgXldrGkL0rPSjkCRU95WGTQdrADC942WJ0TXTAiKJPq5FE0ZnCSc/N+BDKApQFXBOhPIQjfl1SzANJcV6vg2iVhQ8ycqX3eXcN12Y0QPwyH1UUm+5z3J6mIVbkLPwfBHtNMB3IvPHwLm+AR3RM5j18aGvODHMmFfTEvpgiGdb97qYTRArUZTgNCF3zz48N4gfoMiEpVAAAAWAMIIFfDCCBGSgAwIBAgIIDutXh+eeCY0wDQYJKoZIhvcNAQEFBQAwgZYxCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSwwKgYDVQQLDCNBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9uczFEMEIGA1UEAww7QXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTUxMTEzMDIxNTA5WhcNMjMwMjA3MjE0ODQ3WjCBiTE3MDUGA1UEAwwuTWFjIEFwcCBTdG9yZSBhbmQgaVR1bmVzIFN0b3JlIFJlY2VpcHQgU2lnbmluZzEsMCoGA1UECwwjQXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMxEzARBgNVBAoMCkFwcGxlIEluY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c2UiOwoJInB1cmNoYXNlLWRhdGUtbXMiID0gIjE1NzcyMzE2MzI4MDIiOwoJInB1cmNoYXNlLWRhdGUiID0gIjIwMTktMTItMjQgMjM6NTM6NTIgRXRjL0dNVCI7CgkiaXMtdHJpYWwtcGVyaW9kIiA9ICJmYWxzZSI7Cgkib3JpZ2luYWwtcHVyY2hhc2UtZGF0ZSIgPSAiMjAxOS0xMi0yNCAwNjozOToyMCBFdGMvR01UIjsKCSJiaWQiID0gImdlbmVzaXMuYTFyZWFkZXIiOwoJInB1cmNoYXNlLWRhdGUtcHN0IiA9ICIyMDE5LTEyLTI0IDE1OjUzOjUyIEFtZXJpY2EvTG9zX0FuZ2VsZXMiOwp9";
"environment" = "Sandbox";
"pod" = "100";
"signing-status" = "0";
}

One of the PHP scripts i found is this one:
B4X:
<?php

$json['receipt-data'] = $_POST['receipt-data'];

$post = json_encode($json);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://sandbox.itunes.apple.com/verifyReceipt");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);

curl_close ($ch);

?>

I have placed that script in my server, and when i call a Post function with the iHTTPUtils library, I get a response but not the expected response, I get the following response:
{"status":21199}
According to Apple's documentation a 21199 response means that there's an Internal data access error.
sometimes I also get a 21002 Response which means The data in the receipt-data property was malformed or missing.

My question is, has any of you guys who has already dealt with this found a practical solution, and how did you guys deal with it, I know there is a post of someone who has already accomplished this but has not posted his solution.

What should the data passed to the php script look like?
In the string received with Erel's code I see that there are some base64 encoded strings, and in one of the php script I see that one of the things they are doing first is base64 encoding the receipt information, but how does that work, if there are already base64 encoded strings, are they re-encoding the same information two times?

I'm so confused, someone Help!

Thanks,
Walter
 

hatzisn

Well-Known Member
Licensed User
If you try with http get (httpjob.download) sometimes a huge amount of data gets cut. If I were you I would encodebase64 the json with stringutils, use post in the http job, decodebase64 in php side and then follow the given path... Please respond if this solved your problem. I would be really interested...
 
Upvote 0

walterf25

Expert
Licensed User
If you try with http get (httpjob.download) sometimes a huge amount of data gets cut. If I were you I would encodebase64 the json with stringutils, use post in the http job, decodebase64 in php side and then follow the given path... Please respond if this solved your problem. I would be really interested...
the method needs to be a POST method, not Download, i have already tried everything you mentioned, the issue is not in my code, at least I don't think, thanks for your reply.

Walter
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
I am not familiar with php but it would be useful to check if the communication is done using utf8. In .NET this is set manually in web.config. It could be the fact that what you send is translated differently in the php side...
 
Upvote 0

walterf25

Expert
Licensed User
I have solved this issue, please check this tutorial thread if anyone would like know how to verify the in-app purchases transaction receipt.
Receipt Validation tutorial

Walter
 
Upvote 0
Top