文章詳情頁
IOS蘋果AppStore內購付款的服務器端php驗證方法(使用thinkphp)
瀏覽:94日期:2022-06-06 11:10:05
這篇文章主要介紹了IOS蘋果AppStore內購付款的服務器端php驗證方法(使用thinkphp)。AppStore內購在app中支付的過程那是由前端IOS程序猿完成的;
IOS會把支付憑證發給后端服務器;使用php需要做的就是對支付結果的驗證;這篇文章使用thinkphp整合,其實脫離thinkphp別的框架也能很便利的使用。
/** * 驗證AppStore內付 * @param string $receipt_data 付款后憑證 * @return array驗證是否成功 */ function validate_apple_pay($receipt_data){ /** * 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; } // 驗證參數 if (strlen($receipt_data)<20){ $result=array( "status"=>false, "message"=>"非法參數" ); return $result; } // 請求驗證 $html = acurl($receipt_data); $data = json_decode($html,true); // 如果是沙盒數據 則驗證沙盒模式 if($data["status"]=="21007"){ // 請求驗證 $html = acurl($receipt_data, 1); $data = json_decode($html,true); $data["sandbox"] = "1"; } if (isset($_GET["debug"])) { exit(json_encode($data)); } // 判斷是否購買成功 if(intval($data["status"])===0){ $result=array( "status"=>true, "message"=>"購買成功" ); }else{ $result=array( "status"=>false, "message"=>"購買失敗 status:".$data["status"] ); } return $result; }
使用方法也非常簡單,就是把IOS發過來的支付憑證作為參數傳入validate_apple_pay()函數即可。
<?php namespace Api\Controller; use Common\Controller\HomeBaseController; /** * paypal支付 */ class AppstoreController extends HomeBaseController{ // 支付回調 public function result(){ //蘋果內購的驗證收據 $receipt_data = I("post.apple_receipt"); // 驗證支付狀態 $result=validate_apple_pay($receipt_data); if($result["status"]){ // 驗證通過 此處可以是修改數據庫訂單狀態等操作 }else{ // 驗證不通過 } } }
到此這篇關于IOS蘋果AppStore內購付款的服務器端php驗證方法(使用thinkphp)的文章就介紹到這了,更多相關IOS 內購服務器端thinkphp驗證內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!
標簽:
PHP
上一條:基于PHP做個圖片防盜鏈下一條:php字符串使用詳細了解
相關文章:
排行榜
