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;
    }

上一篇:ICP 基本上有两种解法


下一篇:IAP下载