苹果内购流程:

1、前六步有IOS端处理,最终获取购买凭证

2、POST请求,发送购买凭证receipt-data到服务端接口

    /**
     * 苹果内购
     * @param receipt-data 购买凭证(必传)
     * @param is_test 是否沙盒数据(选填,1是 0否,默认否)
     * @return json
     **/
    public function actionsApple_pay(){

        if (!framework::post('is_test')) {    //沙盒购买地址
            $url = "https://sandbox.itunes.apple.com/verifyReceipt";
        }else{  //正式购买地址
            $url = "https://buy.itunes.apple.com/verifyReceipt";
        }

        $receipt_data = framework::post('receipt-data');
        // 验证参数
        if (strlen($receipt_data) < 20){
            $result = array(
                'status'=>false,
                'message'=>'非法参数'
            );
            echo json_encode($result);
            return false;
        }

        $post_data = json_encode(array("receipt-data" => $receipt_data));
        $response = https::curlHttps($url, $post_data);
        $res = json_decode($response, true);

        $err_msg = array(
            '21000' => 'App Store不能读取你提供的JSON对象',
            '21002' => 'receipt-data域的数据有问题',
            '21003' => 'receipt无法通过验证',
            '21004' => '提供的shared secret不匹配你账号中的shared secret',
            '21005' => 'receipt服务器当前不可用',
            '21006' => 'receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送',
            '21007' => 'receipt是Sandbox receipt,但却发送至生产系统的验证服务',
            '21008' => 'receipt是生产receipt,但却发送至Sandbox环境的验证服务'
        );

        // 判断是否购买成功   
        if(intval($res['status']) === 0){
            $result = ['status'=>true, 'message'=>'正式购买成功'];
        }else{
            $result = ['status'=>false, 'message' => '购买失败 status:'.$res['status'].' - '.@$err_msg[$res['status']] ];
            framework::logWrite('苹果支付失败 ---res---'.json_encode($result));
        }

        echo json_encode($result);
        return false;
    }

测试阶段返回 21007,默认为沙盒数据,传is_test参数即可查看响应结果

01-21 18:51