苹果支付校验

    /** 校验通过后,苹果返回的状态码 */
     Const code=[
         21000=>'App Store无法读取你提供的JSON数据',
         21002=>'收据数据不符合格式',
         21003=> '收据无法被验证',
         21004=> '你提供的共享密钥和账户的共享密钥不一致',
         21005=> '收据服务器当前不可用',
         21006=> '收据是有效的,但订阅服务已经过期。当收到这个信息时,解码后的收据信息也包含在返回内容中',
         21007=>'收据信息是测试用(sandbox),但却被发送到产品环境中验证',
         21008=>'收据信息是产品环境中使用,但却被发送到测试环境中验证'
     ];
    /**  
     *@By Vs Code  
     *@Description: IOS内购验证票据  
     *@param $receipt_data 付款后凭证  
     *@param $sandbox 是否为沙盒  
     *@param $transactions 仅对包含自动续订的应用收据使用此字段  
     *@functionAuthor: Be Drowned Fish  
     *@Date: 2019-06-09 15:17:12  
     */
    public function validate_applepay($receipt_data, $sandbox = true, $transactions = false)
    {
        $jsonData = array('receipt-data' => $receipt_data, 'password' => config('apple_pay.password'), 'exclude-old-transactions' => $transactions);
        $post_json = json_encode($jsonData);
        $url = $sandbox === true ? "https://sandbox.itunes.apple.com/verifyReceipt" : "https://buy.itunes.apple.com/verifyReceipt";
        $client = new Client(['verify' => false]);
        $response = $client->post($url, ['body' => $post_json, 'headers' => ['Content-Type' => 'application/json']]);
        $result = $response->getBody()->getContents();
        $res = json_decode($result, true);
        if (intval($res['status']) == 0) { //验证成功
            $apple_pay = config('apple_pay');
            $products = array_keys($apple_pay['products']);
            $product_id = $res['receipt']['in_app'][0]['product_id'];
            if (!in_array($product_id, $products)) {
                $this->error = '不是系统内部的商品ID';
                return false;
            }
            if ($res['receipt']['bundle_id'] != $apple_pay['bundle_id']) {
                $this->error = 'bundle_id 不一致';
                return false;
            }
            $info = $this->getOne($res['receipt']['in_app'][0]['transaction_id']);
            if ($info) {
                $this->error = '交易号重复';
                return false;
            }
            return true;
        } elseif (intval($res['status']) == 21007) {
            return $this->validate_applepay($receipt_data, true);
        } else {
            $this->error = self::code[$res['status']];
            return false;
        }
    }

Read More

发表回复