Featured

Iphone In App purchase receipt verification – Sandbox

March 19, 2013 by Sarath DR in Featured, Iphone Apps with 4 Comments

Sometimes some minor mistakes lead us to a big trouble and waste of time. This article will help you to get some idea for iPhone in app purchase and receipt verification in PHP. This post is prior to my new phonegap plugin for In App purchase. Actually I was referring the following link and used the library to develop the plugin.

http://www.raywenderlich.com/2797/introduction-to-in-app-purchases

The main part is to get the receipt data as string when transaction finishes. You can use the below code in finish transaction function to get the receipt data and pass the data to your PHP application to verify it.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
- (void)finishTransaction:(SKPaymentTransaction *)transaction
{
 
NSData *receiptData = [NSData dataWithData:transaction.transactionReceipt];
 
// This is the string you should pass to server for verification
 NSString *receiptStr = [[NSString alloc] initWithData:receiptData encoding:NSUTF8StringEncoding];
 
// Use json to pass this value. 
 
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

You can use the below code to verify the receipt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// This is not an original  receipt - Only an example format
 
$receipt = '{{
	"signature" = "AoC/7Y1Woi2cOL4SFvS6";
        "purchase-info" = "ewoJIm9yaWdpbmFsLXB1cmNoYXNlLWRhd";
        "environment" = "Sandbox";
	"pod" = "100";
	"signing-status" = "0";}';
 
$sandBoxUrl     = 'https://sandbox.itunes.apple.com/verifyReceipt';
$base64Receipt  = base64_encode( $receipt );
 
// Password is only for Auto renewable subscription.
 
$postData   = json_encode( array( 
	        'receipt-data' => $base64Receipt  ,
		'password' => 'sdfdsf'
	         ) );
	// POST data
 
	try {
 
 
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $sandBoxUrl  );
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_POST, true);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
	    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json',                                                                                
			    'Content-Length: ' . strlen($postData )) 
        	);                                                                                                                   
 
	    $jsonResponse = curl_exec($ch);		
	    curl_close($ch);
 
 
	}
	catch ( Exception $e )
	{
		var_dump($e);
	}
 
	    $response = json_decode(  $jsonResponse  );
	    var_dump( $response->status );
	    var_dump( $response->receipt );

I have created a tool to verify the receipt. Click the below link and submit your receipt. The password field is only for auto renewable subscription.

In App purchase receipt verification Sandbox

Tagged , , ,

Share this to

Related Posts

4 Comments

  1. iPhone Phonegap plugin for In app Auto Renewable SubscriptionMar 27, 2013 at 1:19 pmReply

    [...] iPhone In App purchase receipt verification – Sandbox In App purchase receipt verification Sandbox [...]

  2. VaibhavNov 1, 2013 at 6:59 amReply

    what are things in PHP code I would have to change if I am using this code?
    And Which password i have mention there? I am creating auto renewable subscription in my app.

  3. VaibhavNov 1, 2013 at 10:57 amReply

    the code works well for non-consumable receipt 64bit data. But not working for Auto-Renewable Subscription.

  4. VaibhavNov 1, 2013 at 11:07 amReply

    AAAAAAH….. :)
    It is done. Actually the password is ‘In-App Purchase Shared Secret’

    Thanks you boss. Please approve my all queries sothat other can get help. :)

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close