当前位置: 首页 > 工具软件 > PHP Sandbox > 使用案例 >

苹果内购支付验签(php)

逄嘉木
2023-12-01

苹果支付成功后走php验证支付结果

传receipt支付凭证

自动订阅:所以需要增加一个参数: password: 秘钥, 就可以了, 但是官方文档说秘钥仅仅用在自动续订上面
大家叫后台加个验证,如果苹果验证返回21004的话(21004 你提供的共享密钥和账户的共享密钥不一致),就加上password字段去验证,可以成功


经过验证:购买过自动续期订阅后,验证内购时(即使是消耗型商品)必须带上password字段。

use think\Exception;    

    /**
     * 苹果内购验证
     */
    public function ifReceiptValid($receipt, $isSandbox = false,$password='')
    {
        if ($isSandbox) {
            $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
        } else {
            $endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
        }

        $postData = json_encode(
            array('receipt-data' => $receipt,'password'=>$password)
        );

        $ch = curl_init($endpoint);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

        $response = curl_exec($ch);
        $errno    = curl_errno($ch);
        $errmsg   = curl_error($ch);
        curl_close($ch);

        if ($errno != 0) {
            throw new Exception($errmsg, $errno);
        }

        $data = json_decode($response);

        if (!is_object($data)) {
            throw new Exception('Invalid response data');
        }

        if (isset($data->status) && $data->status == 0) {
            //watchdog('services_vip_resource_update verifyReceipt', '  status=' . $data->status . ', result=true');
            $return = true;
        } else if (isset($data->status) && $data->status == 21007) {
            //watchdog('services_vip_resource_update verifyReceipt', ' status=' . $data->status . ', result=to sandbox');
            $return = $this->ifReceiptValid($receipt, true,$password);
        } else {
            $return = false;
        }
        return $return;
    }


//使用
$this->ifReceiptValid($post['receipt'])
$this->ifReceiptValid($post['receipt'], false, '4a0e8d5d349147cd9a2836fd94e7fe23')

 类似资料: