<?php
//使用者發來的引數
$receipt_data = $_POST['receipt-data'];
//驗證引數
if (strlen($receipt_data)<20){
$echo = array('buy'=>'0','message'=>'非法引數');
exit(json_encode($echo));
}
//請求驗證
$html = acurl($receipt_data);
$data = json_decode($html,1);
//如果是沙盒資料 則驗證沙盒模式
if($data['status']=='21007'){
//請求驗證
$html = acurl($receipt_data, $sandbox=1);
$data = json_decode($html,1);
$data['sandbox'] = '1';
}
if (isset($_GET['debug'])) {
exit(json_encode($data));
}
if($data['status']==0){
$echo = array('buy'=>'1','message'=>'購買成功');
}else{
$echo = array('buy'=>'0','message'=>'購買失敗','status'=>$data['status']);
}
//是否是沙盒模式
if ($data['sandbox']) $echo['sandbox'] = '1';
echo json_encode($echo);
/**
* 21000 App Store不能讀取你提供的JSON物件
* 21002 receipt-data域的資料有問題
* 21003 receipt無法通過驗證
* 21004 提供的shared secret不匹配你賬號中的shared secret
* 21005 receipt伺服器當前不可用
* 21006 receipt合法,但是訂閱已過期。伺服器接收到這個狀態碼時,receipt資料仍然會解碼並一起傳送
* 21007 receipt是Sandbox receipt,但卻傳送至生產系統的驗證服務
* 21008 receipt是生產receipt,但卻傳送至Sandbox環境的驗證服務
*/
function acurl($receipt_data, $sandbox=0){
//小票資訊
$POSTFIELDS = array("receipt-data" => $receipt_data);
$POSTFIELDS = json_encode($POSTFIELDS);
//正式購買地址 沙盒購買地址
$url_buy = "https://buy.itunes.apple.com/verifyReceipt";
$url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
$url = $sandbox ? $url_sandbox : $url_buy;
//簡單的curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>