1. 程式人生 > >php處理蘋果支付介面回撥

php處理蘋果支付介面回撥

需求:公司開發了一個應用,接入了蘋果支付(其實是AppStore內支付,因為是虛擬商品,所有不能直接接入app pay),客戶端做好了,要PHP(我)對接支付回撥介面,處理支付後的業務邏輯(修改訂單狀態,傳送簡訊通知客戶,修改使用者購買的套餐)。

接入AppStore內支付,一波三折,因為之前沒有做過這方面的開發。看文件也全部是英文的,看的很費勁,乘著這次我把用php的經歷寫出來,寫的不好的地方,請大家多多指正批評。

下面是我的程式碼:

// 蘋果支付驗證 介面  
    //   
    // @param string $receipt 收到的資料  
    // @param boolean $isSandbox
是否是沙盒模式,true,false function getreceiptdata(){ $receipt = $_REQUEST['strReceipt']; $username = addslashes($_REQUEST['username']);//使用者名稱 $tc = $_REQUEST['pid'];//套餐型別 $isSandbox = true; //如果是沙盒模式,請求蘋果測試伺服器,反之,請求蘋果正式的伺服器 if ($isSandbox) { $endpoint
= 'https://sandbox.itunes.apple.com/verifyReceipt'; } else { $endpoint = 'https://buy.itunes.apple.com/verifyReceipt'; } $postData = json_encode( array('receipt-data' => $receipt) ); $ch = curl_init($endpoint
); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); //這兩行一定要加,不加會報SSL 錯誤 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); $response = curl_exec($ch); $errno = curl_errno($ch); $errmsg = curl_error($ch); curl_close($ch); $data = json_decode($response); //判斷時候出錯,丟擲異常 if ($errno != 0) { //throw new \Exception($errmsg, $errno); //$arr = array('status'=>'fail','code'=>'-3','msg'=>'判斷時候出錯,丟擲異常'); //echo json_encode($arr);exit; $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= ''; $xml .= "fail"; $xml .= "<code>3</code>"; $xml .= "判斷時候出錯,丟擲異常"; $xml .= ''; echo $xml;exit; } //判斷返回的資料是否是物件 if (!is_object($data)) { //E('Invalid response data');//無效的響應資料 //$arr = array('status'=>'fail','code'=>'-2','msg'=>'無效的響應資料'); //echo json_encode($arr);exit; $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= ''; $xml .= "fail"; $xml .= "<code>-2</code>"; $xml .= "無效的響應資料"; $xml .= ''; echo $xml;exit; } //判斷購買時候成功 if (!isset($data->status) || $data->status != 0) { //E('Invalid receipt');//無效的收據 //$arr = array('status'=>'fail','code'=>'-1','msg'=>'無效的收據'); //echo json_encode($arr);exit; $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= ''; $xml .= "fail"; $xml .= "<code>-1</code>"; $xml .= "無效的收據"; $xml .= ''; echo $xml;exit; } $order = $data->receipt->in_app;//所有的訂單的資訊 $k = count($order) -1; $need = $order[$k];//需要的那個訂單 //下面進行業務處理了,根據使用者購買的套餐,給他開通相應的套餐、時間 //新建一個訂單,已支付狀態, //把購買的套餐的時間加到該賬號 }

我這裡的是客戶端(ios)呼叫我的介面,客戶端傳使用者名稱,套餐型別,還有蘋果傳送的資料過來。返回的是xml格式的資料,其實最好是用json返回給客戶端,我這裡是客戶端要求返回xml,程式碼寫的很爛。沒有做其他的任何驗證,如果你們自己驗證的話,一定要驗證全面一些。我只是把驗證思路貼出來。

Status codes

Status Code Description
21000 The App Store could not read the JSON object you provided.
21002 The data in the receipt-data property was malformed or missing.
21003 The receipt could not be authenticated.
21004 The shared secret you provided does not match the shared secret on file for your account.
21005 The receipt server is not currently available.
21006 This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.
21007 This receipt is from the test environment, but it was sent to the production environment for verification. Send it to the test environment instead.
21008 This receipt is from the production environment, but it was sent to the test environment for verification. Send it to the production environment instead.
21010 This receipt could not be authorized. Treat this the same as if a purchase was never made.
21100-21199 Internal data access error.