文章詳情頁(yè)
IOS蘋果AppStore內(nèi)購(gòu)付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)
瀏覽:10日期:2022-06-06 11:10:05
這篇文章主要介紹了IOS蘋果AppStore內(nèi)購(gòu)付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)。AppStore內(nèi)購(gòu)在app中支付的過(guò)程那是由前端IOS程序猿完成的;
IOS會(huì)把支付憑證發(fā)給后端服務(wù)器;使用php需要做的就是對(duì)支付結(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對(duì)象 * 21002 receipt-data域的數(shù)據(jù)有問(wèn)題 * 21003 receipt無(wú)法通過(guò)驗(yàn)證 * 21004 提供的shared secret不匹配你賬號(hào)中的shared secret * 21005 receipt服務(wù)器當(dāng)前不可用 * 21006 receipt合法,但是訂閱已過(guò)期。服務(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); //正式購(gòu)買地址 沙盒購(gòu)買地址 $url_buy = "https://buy.itunes.apple.com/verifyReceipt"; $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt"; $url = $sandbox ? $url_sandbox : $url_buy; //簡(jiǎn)單的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; } // 請(qǐng)求驗(yàn)證 $html = acurl($receipt_data); $data = json_decode($html,true); // 如果是沙盒數(shù)據(jù) 則驗(yàn)證沙盒模式 if($data["status"]=="21007"){ // 請(qǐng)求驗(yàn)證 $html = acurl($receipt_data, 1); $data = json_decode($html,true); $data["sandbox"] = "1"; } if (isset($_GET["debug"])) { exit(json_encode($data)); } // 判斷是否購(gòu)買成功 if(intval($data["status"])===0){ $result=array( "status"=>true, "message"=>"購(gòu)買成功" ); }else{ $result=array( "status"=>false, "message"=>"購(gòu)買失敗 status:".$data["status"] ); } return $result; }
使用方法也非常簡(jiǎn)單,就是把IOS發(fā)過(guò)來(lái)的支付憑證作為參數(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)購(gòu)的驗(yàn)證收據(jù) $receipt_data = I("post.apple_receipt"); // 驗(yàn)證支付狀態(tài) $result=validate_apple_pay($receipt_data); if($result["status"]){ // 驗(yàn)證通過(guò) 此處可以是修改數(shù)據(jù)庫(kù)訂單狀態(tài)等操作 }else{ // 驗(yàn)證不通過(guò) } } }
到此這篇關(guān)于IOS蘋果AppStore內(nèi)購(gòu)付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)的文章就介紹到這了,更多相關(guān)IOS 內(nèi)購(gòu)服務(wù)器端thinkphp驗(yàn)證內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
標(biāo)簽:
PHP
上一條:基于PHP做個(gè)圖片防盜鏈下一條:php字符串使用詳細(xì)了解
相關(guān)文章:
排行榜
