Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

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

Sunday, January 30, 2011

Installing Mercurial on RHEL 5.5

Finally after a few attempts, I could install mercurial on RHEL.

Here are the steps I followed and thanks to a few forum posts, I found out my way through it.

So I tried all kinds of ways but what eventually worked was easy_install.

So this is what you need.

easy_install
python-setuptools

Downloaded the latest mercurial rpm.  Untar it.

Now if you dont have easy_install, install it by running the below command

wget http://pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.egg#md5=bd639f9b0eac4c42497034dec2ec0c2b

Once you are done downloading run the below command

ronak@ronak ~ $ sudo sh setuptools-0.6c11-py2.4.egg
[sudo] password for ronak:
Processing setuptools-0.6c11-py2.4.egg
removing '/usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg' (and everything under it)
creating /usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg
Extracting setuptools-0.6c11-py2.4.egg to /usr/lib/python2.4/site-packages
setuptools 0.6c11 is already the active version in easy-install.pth
Installing easy_install script to /usr/bin
Installing easy_install-2.4 script to /usr/bin

Installed /usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11




If you see this, easy_install is installed on your system.

Now, remember you have the untar directory mercurial1.7 (or whatever you have).  Make sure you are at that location.

Run the below command

ronak@ronak ~ $ sudo easy_install mercurial-1.7/
ronak@ronak ~ $ sudo easy_install mercurial-1.7/
Processing
Running setup.py -q bdist_egg --dist-dir /usr/local/home/rpatel/mercurial-1.7/egg-dist-tmp-bHHAXk
zip_safe flag not set; analyzing archive contents...
mercurial.help: module references __file__
mercurial.lsprof: module references __file__
mercurial.commands: module references __file__
mercurial.extensions: module references __file__
mercurial.i18n: module references __file__
mercurial.templater: module references __file__
hgext.mq: module references __file__
Adding mercurial unknown to easy-install.pth file
Installing hg script to /usr/bin

Installed /usr/lib/python2.4/site-packages/mercurial-unknown-py2.4-linux-i686.egg
Processing dependencies for mercurial==unknown
Finished processing dependencies for mercurial==unknown


ronak@ronak ~ $ hg
Mercurial Distributed SCM

basic commands:

 add        add the specified files on the next commit
 annotate   show changeset information by line for each file
 clone      make a copy of an existing repository
 commit     commit the specified files or all outstanding changes
 diff       diff repository (or selected files)
 export     dump the header and diffs for one or more changesets
 forget     forget the specified files on the next commit
 init       create a new repository in the given directory
 log        show revision history of entire repository or files
 merge      merge working directory with another revision
 pull       pull changes from the specified source
 push       push changes to the specified destination
 remove     remove the specified files on the next commit
 serve      start stand-alone webserver
 status     show changed files in the working directory
 summary    summarize working directory state
 update     update working directory (or switch revisions)

use "hg help" for the full list of commands or "hg -v" for details
ronak@ronak ~ $

Hurray... You are done..!!!!


Yum

YUM (Yellow Dog Updater, Modified)

The Yellowdog Updater, Modified (YUM) is an open-source command-line package-management utility for RPM-compatible Linux operating systems and has been released under the GNU General Public License. It was developed by Seth Vidal and a group of volunteer programmers. Though yum has a command-line interface, several other tools provide graphical user interfaces to yum functionality.


Installing and Upgrading Packages with Yum

Install Packages, Upgrade packages and System upgrades
Update your package list
# yum check-update
Install a new package
# yum -y install compiz
-y flag allows unattended installation.
Update a single Package
# yum update gnome
Update several packages
# yum update nbserver nbweb-billing nginx
Update all installed packages
# yum update
Search for a package to install
# yum search ImageMagick
If you need a particular library, but don't know what package provides it, yum will find it
# yum provides compiz

Removing Packages through Yum

# yum remove compiz
# yum remove compiz gnome ImageMagick

Obtaining information of installed packages through Yum

# yum list compiz
# yum list | grep gnome
# yum info compiz
# yum list updates

Maintain Yum

# yum clean packages -- to delete all cached packages
# yum clean headers -- to force fresh download of package headers
# yum clean oldheaders -- to remove old headers that the system no longer needs
# yum clean all == remove cached packages and old headers, preserving all the current headers

Thursday, May 7, 2009

Print all prime number upto a certain Range in C++ and Python

I have tried developing in C++ and Python. I like scripting in python and C++ is my favorite programming language.


#!/usr/bin/env python




#Author : Ronak Patel

#Date : 05/07/2009

#Purpose : Write a script to get all prime numbers upto a certain range.



x = int(raw_input("Enter the range: "))

for i in range(2,x):

isprime = 1

for j in range(2,i):

if(i==j):

continue

elif(i%j==0):

isprime=0

if(isprime):

print "%d\t" % i




The logic is the same but only the syntax is different. Here we have two for loops. The first one starts from 2 and goes till the last number you want. The other loop starts with a 2 and end with the first loop digit. So we make sure all the numbers are verified. Let me explain you in a bit detail.

In the above script, Initially i will be 2 and j will also be 2. It will end up exiting the loop because it satisfies the condition i==j and it will be printed since isprime remains 1. Secondly i will be 2 and j will be 3 which wont satisfy the condition i==j and also i%j==0. So it will move forward with i=3 and j=3 which then shows that it is also a prime number.

Now when i=2 and j=4, it satisfies the condition i%j==0 which means it will not be printed since isprime=0. So this is how this works.

Below is the C++ implementation.


// prime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include


//using namespace std;

void prime(int a)
{
int isprime;

for(int i=2;i<=a;i++)
{
printf("i in the first loop %d\n", i);
isprime =1;
for(int j =2;j<=i;j++)
{
printf("\tj in the second loop %d\n", j);

if(i==j)
{
continue
;
}
else if (i %j == 0)
isprime = 0;
}
if(isprime)
printf("%d\n",i);
}
getchar(); }
void main()
{
int n;
std::cout<<"Enter range: ";
std::cin >> n;

prime(n);

getchar();

}


Hope this helps. I will try and post Perl script too for this. I believe I did not find more implementations of this particular program. You can find many implementations for odd even programs but not prime ones.


Here goes the perl script for the same purpose


#!/usr/bin/perl -w
#use strict;
#Author : Ronak Patel
#Date : 05/07/2009
#Purpose : Write a script in perl to print all prime numbers

print("Enter the range: ");
my $max = <>;
chomp($max);
$i = 0;
$j =0;
$isprime = 0;


for($i = 2;$i <= $max; $i++) { $isprime=1; for($j = 2; $j <= $i; $j++) { if($i == $j){} # continue; elsif($i%$j==0) { $isprime = 0; } } if($isprime) { print("$i\n"); } }