Thursday, March 22, 2012

Verify Apple store (In-app purchase) receipts in python

Python is very versatile and dynamic programming language and I use it on a daily basis.

Currently I was working on the In-app purchases from the App store and was wondering how I can verify the app store receipt data on the python prompt.

I found it easy in python to communicate to the app store and verify the app store receipt.  This article talks about how you can do it on the python prompt.  Hopefully this works and helps anybody trying to verify the receipt data from app store through the sandbox/app store.

I was trying to look for examples on the web if somebody had already done it in python but did not find one.  So here it goes.!!!


import urllib, urllib2
import simplejson as json
import datetime
url = 'https://sandbox.itunes.apple.com/verifyReceipt'
receipt_data = 'receipt_data'
password =  'your_password'

data = { "receipt-data": receipt_data, "password": password }
headers = {'Content-Type': 'text/Json; charset=utf-8'}
dataj = json.dumps(data)
request = urllib2.Request(url, dataj, headers)

# In case you are wondering what is being sent and the format
print "request%s" % request.get_data()
# This shows the method either POST or GET (App store wants POST and not GET)
print "request type %s" % request.get_method()
start_time = datetime.datetime.now()
response = urllib2.urlopen(request)
wait_time = datetime.datetime.now() - start_time
print('Wait time for validation call: %s' % wait_time)
response_json = json.loads(response.read())
print response_json




{'status': 21006, 'receipt': {'purchase_date_pst': '2012-03-16 15:33:58 America/Los_Angeles', 'expires_date': '1331937538000', 'product_id': 'your_product_id', 'original_transaction_id': '1222000034404994', 'expires_date_formatted_pst': '2012-03-16 15:38:58 America/Los_Angeles', 'original_purchase_date_pst': '2012-03-16 15:33:59 America/Los_Angeles', 'item_id': 'your_item_id', 'original_purchase_date': '2012-03-16 22:33:59 Etc/GMT', 'expires_date_formatted': '2012-03-16 22:38:58 Etc/GMT', 'bvrs': 'your_version', 'original_purchase_date_ms': '1331937239000', 'hosted_iap_version': '2.718', 'purchase_date': '2012-03-16 22:33:58 Etc/GMT', 'web_order_line_item_id': 'your_id', 'purchase_date_ms': '1331937238000', 'bid': 'your_bid', 'transaction_id': '1222000034419520', 'quantity': '1'}, 'latest_expired_receipt_info': {'purchase_date_pst': '2012-03-17 00:59:55 America/Los_Angeles', 'expires_date': '1331971495000', 'product_id': 'your_product_id', 'original_transaction_id': '1222000034404994', 'expires_date_formatted_pst': '2012-03-17 01:04:55 America/Los_Angeles', 'original_purchase_date_pst': '2012-03-16 15:33:59 America/Los_Angeles', 'item_id': 'your_item_id', 'original_purchase_date': '2012-03-16 22:33:59 Etc/GMT', 'expires_date_formatted': '2012-03-17 08:04:55 Etc/GMT', 'bvrs': 'your_version', 'original_purchase_date_ms': '1331937239000', 'purchase_date': '2012-03-17 07:59:55 Etc/GMT', 'web_order_line_item_id': 'your_item_id', 'purchase_date_ms': '1331971195000', 'bid': 'your_bid', 'transaction_id': '1222000034486923', 'quantity': '1'}}

Below is the URL that has additional details on the error codes and API details.

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html