文章詳情頁
IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)
瀏覽:47日期:2022-06-06 11:10:05
這篇文章主要介紹了IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)。AppStore內(nèi)購在app中支付的過程那是由前端IOS程序猿完成的;
IOS會(huì)把支付憑證發(fā)給后端服務(wù)器;使用php需要做的就是對支付結(jié)果的驗(yàn)證;這篇文章使用thinkphp整合,其實(shí)脫離thinkphp別的框架也能很便利的使用。
/** * 驗(yàn)證AppStore內(nèi)付 * @param string $receipt_data 付款后憑證 * @return array驗(yàn)證是否成功 */ function validate_apple_pay($receipt_data){ /** * 21000 App Store不能讀取你提供的JSON對象 * 21002 receipt-data域的數(shù)據(jù)有問題 * 21003 receipt無法通過驗(yàn)證 * 21004 提供的shared secret不匹配你賬號中的shared secret * 21005 receipt服務(wù)器當(dāng)前不可用 * 21006 receipt合法,但是訂閱已過期。服務(wù)器接收到這個(gè)狀態(tài)碼時(shí),receipt數(shù)據(jù)仍然會(huì)解碼并一起發(fā)送 * 21007 receipt是Sandbox receipt,但卻發(fā)送至生產(chǎn)系統(tǒng)的驗(yàn)證服務(wù) * 21008 receipt是生產(chǎn)receipt,但卻發(fā)送至Sandbox環(huán)境的驗(yàn)證服務(wù) */ 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; } // 驗(yàn)證參數(shù) if (strlen($receipt_data)<20){ $result=array( "status"=>false, "message"=>"非法參數(shù)" ); return $result; } // 請求驗(yàn)證 $html = acurl($receipt_data); $data = json_decode($html,true); // 如果是沙盒數(shù)據(jù) 則驗(yàn)證沙盒模式 if($data["status"]=="21007"){ // 請求驗(yàn)證 $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發(fā)過來的支付憑證作為參數(shù)傳入validate_apple_pay()函數(shù)即可。
<?php namespace Api\Controller; use Common\Controller\HomeBaseController; /** * paypal支付 */ class AppstoreController extends HomeBaseController{ // 支付回調(diào) public function result(){ //蘋果內(nèi)購的驗(yàn)證收據(jù) $receipt_data = I("post.apple_receipt"); // 驗(yàn)證支付狀態(tài) $result=validate_apple_pay($receipt_data); if($result["status"]){ // 驗(yàn)證通過 此處可以是修改數(shù)據(jù)庫訂單狀態(tài)等操作 }else{ // 驗(yàn)證不通過 } } }
到此這篇關(guān)于IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)的文章就介紹到這了,更多相關(guān)IOS 內(nèi)購服務(wù)器端thinkphp驗(yàn)證內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
標(biāo)簽:
PHP
上一條:基于PHP做個(gè)圖片防盜鏈下一條:php字符串使用詳細(xì)了解
相關(guān)文章:
1. axios和ajax的區(qū)別點(diǎn)總結(jié)2. iOS整個(gè)APP實(shí)現(xiàn)灰色主題的示例代碼3. 如何使用iostat查看linux硬盤IO性能4. 在iOS中給視頻添加濾鏡的方法示例5. iOS獲取設(shè)備唯一標(biāo)識(shí)的實(shí)現(xiàn)步驟6. VMware如何進(jìn)入BIOS方法7. 在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫板的方法8. react axios 跨域訪問一個(gè)或多個(gè)域名問題9. axios基本用法教程示例詳解10. ThinkPHP5中如何實(shí)現(xiàn)模板完全靜態(tài)化詳解
排行榜
