ApplePay

class ApplePay extends CI_Controller {
    /*
     * @function        __construct
     * @author          Acclea
     * @date            2017.10.27
     */
    public function __construct(){
        parent::__construct();
        $this->load->model('Home_model', 'home');
        $this->load->model('Agent_model', 'agent');

        if(@strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || @strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
            $this->usys     = 'IOS';
        }else if(@strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
        $this->usys     = 'Android';
        }else{
            $this->usys     = 'other';
        }
    }


    /*
     * @function        formatAppleJson
     * @author          Acclea
     * @date            2017.10.27
     */
    private  function formatAppleJson(&$data){
        $result = explode(";", str_replace(" = ", ":", base64_decode($data)));
        $last = array_pop($result);
        $result = json_decode(implode(",", $result) . $last, true);
        unset($data);
        return $result;
    }


    /*
     * @function        index
     * @author          Acclea
     * @date            2017.10.27
     */
    public function index(){
        if(!$_POST){
            echo json_encode(array('code'=>-3,'msg'=>'非法請求'));
            die;
        }

        $trade        = $this->input->post('receipt-data');
        $requestData        = json_encode(array('receipt-data'=>$trade));
        $formalUrl          = "https://buy.itunes.apple.com/verifyReceipt";
        $testUrl            = "https://sandbox.itunes.apple.com/verifyReceipt";

        $resultJson     = $this->appleServer($formalUrl,$requestData);
        $result         = json_decode($resultJson,true);
        if($resultJson && $result['status'] == 21007){
            $resultJson     = $this->appleServer($testUrl,$requestData);
            $result         = json_decode($resultJson,true);
        }


        if($resultJson && $result['status'] == 0){
            $receipt           = $result['receipt'];
            $dataRow         = $this->formatAppleJson($trade);
            $clientData          = $this->formatAppleJson($dataRow['purchase-info']);
           
            //驗證訂單數據自有發揮
            if($receipt['unique_identifier'] == $clientData['unique-identifier'] ){

                //修改訂單狀態本身發揮
                
                echo json_encode(array('code'=>-2,'msg'=>'支付成功'));
                die;
            }else{
                echo json_encode(array('code'=>-4,'msg'=>'非法訂單'));
                die;
            }
        }else{
            echo json_encode(array('code'=>-1,'msg'=>"錯誤代碼:".$result['status']));
            die;
        }
    }


    /*
     * @function        appleServer
     * @author          Acclea
     * @date            2017.10.27
     */
    private function appleServer($url,$data){
        $curl      = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS, ($data));
        curl_setopt($curl,CURLOPT_HEADER,0);
        curl_setopt($curl,CURLOPT_NOBODY,0);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
本站公眾號
   歡迎關注本站公眾號,獲取更多信息