欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗證方法(使用thinkphp)

 更新時間:2022年12月17日 09:57:08   投稿:yin  
這篇文章主要介紹了IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗證方法(使用thinkphp),需要的朋友可以參考下

這篇文章主要介紹了IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗證方法(使用thinkphp)。AppStore內(nèi)購在app中支付的過程那是由前端IOS程序猿完成的;
IOS會把支付憑證發(fā)給后端服務(wù)器;使用php需要做的就是對支付結(jié)果的驗證;這篇文章使用thinkphp整合,其實脫離thinkphp別的框架也能很便利的使用。

/**
 * 驗證AppStore內(nèi)付
 * @param  string $receipt_data 付款后憑證
 * @return array                驗證是否成功
 */
function validate_apple_pay($receipt_data){
    /**
     * 21000 App Store不能讀取你提供的JSON對象
     * 21002 receipt-data域的數(shù)據(jù)有問題
     * 21003 receipt無法通過驗證
     * 21004 提供的shared secret不匹配你賬號中的shared secret
     * 21005 receipt服務(wù)器當前不可用
     * 21006 receipt合法,但是訂閱已過期。服務(wù)器接收到這個狀態(tài)碼時,receipt數(shù)據(jù)仍然會解碼并一起發(fā)送
     * 21007 receipt是Sandbox receipt,但卻發(fā)送至生產(chǎn)系統(tǒng)的驗證服務(wù)
     * 21008 receipt是生產(chǎn)receipt,但卻發(fā)送至Sandbox環(huá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;
    }
    // 驗證參數(shù)
    if (strlen($receipt_data)<20){
        $result=array(
            'status'=>false,
            'message'=>'非法參數(shù)'
            );
        return $result;
    }
    // 請求驗證
    $html = acurl($receipt_data);
    $data = json_decode($html,true);

    // 如果是沙盒數(shù)據(jù) 則驗證沙盒模式
    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發(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)購的驗證收據(jù)
        $receipt_data = I('post.apple_receipt');
        // 驗證支付狀態(tài)
        $result=validate_apple_pay($receipt_data);
        if($result['status']){
            // 驗證通過 此處可以是修改數(shù)據(jù)庫訂單狀態(tài)等操作

        }else{
            // 驗證不通過
        }
    }

}

到此這篇關(guān)于IOS蘋果AppStore內(nèi)購付款的服務(wù)器端php驗證方法(使用thinkphp)的文章就介紹到這了,更多相關(guān)IOS 內(nèi)購服務(wù)器端thinkphp驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論