Skip to content

Instantly share code, notes, and snippets.

@thetzel
Created July 13, 2012 20:01
Show Gist options
  • Save thetzel/3107056 to your computer and use it in GitHub Desktop.
Save thetzel/3107056 to your computer and use it in GitHub Desktop.
In App Purchase Verification PHP Code
<?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");}
}
?>
@briancline
Copy link

Why $object->{'status'} rather than $object->status?

@zapjonny
Copy link

Looks like nonfinished code? :-P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment