PHP教程

iOS应用内支付(IAP)php后端验证代码

本文主要是介绍iOS应用内支付(IAP)php后端验证代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
        $is_sandbox = input('is_sandbox', 0, 'intval');
        $receipt = urlencode($_REQUEST['receipt-data']);
        $receipt = str_replace('%2F','/',$receipt);
        $receipt = str_replace('%5C','',$receipt);
        $receipt = str_replace('%3D','=',$receipt);
        $receipt = str_replace('%2B','+',$receipt);
        if ($is_sandbox == 1) {
            $url = "https://sandbox.itunes.apple.com/verifyReceipt";
        } else {
            $url = "https://buy.itunes.apple.com/verifyReceipt";
        }
        $json = '{"receipt-data":"'.$receipt.'"}';
        $result = $this->http_post_json($url, $json);
        if (isset($result['status']) && $result['status'] == 0 && !empty($result['receipt']['in_app'])) {
            $pay_log_insert = [];
            $time = time();
            foreach ($result['receipt']['in_app'] as $key => $val) {
                $pay_log_insert[] = [
                        'user_id' => $user_id,
                        'order_id' => 0,
                        'title' => $title,
                        'price' => $price,
                        'article_type' => $type,
                        'log_type' => 2,
                        'question_num' => $num,
                        'create_time' => $time,
                        'delete_time' => 0,
                        'receipt_data' => $receipt,
                    ];
            }
            
    private function http_post_json($url, $json)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($json))
        );
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($ch, CURLOPT_POST, 1);
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  //这两行一定要加,不加会报SSL 错误
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $response = curl_exec($ch);
        $errno = curl_errno($ch);
        $errmsg = curl_error($ch);
        curl_close($ch);
        if (!$response) {
            return $errmsg;
        }
        $data = json_decode($response, true);
        return $data;
    }

这篇关于iOS应用内支付(IAP)php后端验证代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!