Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException when using Curl to verify App Store receipt?

Tags:

ios

iphone

ios5

I have an in-app purchase for which I want to verify the store receipt. I'd like to verify this from a random machine on the Internet by using Apple's itunes API. The receipt is stored in Parse after the transaction is completed. I'm following the guide on the Apple developer website. First I get the transaction from Parse:

curl -X GET \
  -H "X-Parse-Application-Id: [...]" \
  -H "X-Parse-REST-API-Key: [...]" \
  https://api.parse.com/1/classes/Transactions/123456789

which looks like:

{
    "transactionReceipt":{"__type":"Bytes","base64":"asdfqwertyASDFQWERTY="},
    "transactionType":"Purchased",
    "transactionIdentifier":"[...]",
    "transactionDate":{"__type":"Date","iso":"2012-09-10T06:58:44.071Z"},
    "createdAt":"2012-09-10T06:58:37.234Z",
    "updatedAt":"2012-09-10T06:58:37.234Z",
    "objectId":"HyPWJBlWzt"
 }

I then take the base64 value inside transactionReceipt and curl it against the Apple endpoint to get the receipt:

curl -H "Accept: application/json" \
     -H "Content-Type: application/json" \ 
     -X POST 
     -d '{"receipt-data":"asdfqwertyASDFQWERTY="}' \
     https://buy.itunes.apple.com/verifyReceipt

And all I get back is a not tremendously helpful:

{"status":21002, "exception":"java.lang.IllegalStateException"}

which I believe corresponds to a "The data in the receipt-data property was malformed.". Getting curl to dump the entire operation with --trace-ascii didn't reveal anything I thought was relevant, I'm sure the issue is not with the connection itself.

Slightly stumped here. It does look like the transaction was found on their end (tweaking a few bytes in the receipt-data throws a java.lang.IllegalArgumentException), so I'm guessing it's got something to do with the transaction itself. Has anybody seen this before?

Thanks!

like image 773
Alexandr Kurilin Avatar asked Sep 11 '12 02:09

Alexandr Kurilin


1 Answers

I landed here after searching for the same error message. I eventually solved it - the best advice I can give is to double-check that the receipt is valid and that you're posting it to the correct URL. I got your exact error when I was using an invalid receipt (or perhaps just the wrong kind - it was an app receipt, not an in-app-purchase receipt), and a similar error when using a valid sandbox receipt posted to the 'production' verification url.

I was originally using the example receipt data from http://images.worldofapple.com/validating_051110.pdf, after uudecoding it and reencoding it as base64 . I tried posting to:

  • https://sandbox.itunes.apple.com/verifyReceipt
  • https://buy.itunes.apple.com/verifyReceipt

Both gave the same error {"status":21002, "exception":"java.lang.IllegalStateException"}. I now suspect the root cause is that this is an app receipt, not an in-app purchase receipt.

I then got another example receipt from https://gist.github.com/sauloarruda/2559455

At https://buy.itunes.apple.com/verifyReceipt I got a similarly useless response: {"status":21007}

Finally at https://sandbox.itunes.apple.com/verifyReceipt I got the expected response:

{ "receipt":{"original_purchase_date_pst":"2012-04-30 08:05:55 America/Los_Angeles", "original_transaction_id":"1000000046178817", "original_purchase_date_ms":"1335798355868", "transaction_id":"1000000046178817", "quantity":"1", "product_id":"com.mindmobapp.download", "bvrs":"20120427", "purchase_date_ms":"1335798355868", "purchase_date":"2012-04-30 15:05:55 Etc/GMT", "original_purchase_date":"2012-04-30 15:05:55 Etc/GMT", "purchase_date_pst":"2012-04-30 08:05:55 America/Los_Angeles", "bid":"com.mindmobapp.MindMob", "item_id":"521129812"}, "status":0}

like image 156
sporksmith Avatar answered Nov 03 '22 00:11

sporksmith