Created
July 13, 2012 20:01
-
-
Save thetzel/3107056 to your computer and use it in GitHub Desktop.
In App Purchase Verification PHP Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function verifyPurchase($transactionid, $productid, $uuid, $sandbox=false) { | |
$body = @file_get_contents('php://input'); | |
//encode receipt | |
$body = base64_encode($body); | |
$receipt = json_encode(array("receipt-data" => $body)); | |
// NOTE: use "buy" vs "sandbox" in production. | |
if ($sandbox) { | |
$url = "https://sandbox.itunes.apple.com/verifyReceipt"; | |
} else { | |
$url = "https://buy.itunes.apple.com/verifyReceipt"; | |
} | |
//send receipt to Apple | |
$curl_handle = curl_init(); | |
curl_setopt($curl_handle,CURLOPT_URL,$url); | |
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); | |
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$receipt); | |
$buffer = curl_exec($curl_handle); | |
curl_close($curl_handle); | |
//getting answer from Apple | |
$object = json_decode($buffer); | |
$array = json_decode($buffer,true); | |
$status = $object->{'status'}; | |
if ($status == 0 && //additional sanity checks) { | |
//receipt is OK | |
$userslug = $uuid; | |
$tid = $transactionid; | |
echo "{\"status\" : 0 } | |
} else {die("error");} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why $object->{'status'} rather than $object->status?