<?php

namespace app\common\controller;

use app\ros\model\Order as OrderModel;
use app\http\model\User as UserModel;
use app\http\model\Recharge as RechargeModel;

class Wxpay {

     /**
     * @description: 获取支付宝是否支付成功
     * @param {*}
     * @return {*}
     */    
    public function wxpayVerify() {
        

        //接收数据
        $xmlData = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        $order_model = new OrderModel();
        $order_no = $data['out_trade_no'];
        $wx_no = $data['transaction_id'];

        //获取充值信息
        $order_info = $order_model->getOne(['order_no'=>$order_no]);

        if (!empty($order_info['ali_wx_no'])) {
            die;
        }

        //判断算出的签名和通知信息的签名是否一致
        if($data['result_code'] == 'SUCCESS' && $data['appid'] == 'wx18e5b4f42773c3ec' && $data['mch_id'] == '1571608411'){
            
            $update_data = [];
            
            //更新充值订单
            $update_data['ali_wx_no'] = $wx_no;
            $update_data['status'] = 1;
            $order_model->updateOne(['order_no'=>$order_no],$update_data);


            //处理完成之后，告诉微信成功结果
            echo '<xml>
                    <return_code><![CDATA[SUCCESS]]></return_code>
                    <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
            exit();
        }
    }

    public function httpwxpayVerify() {
        

        //接收数据
        $xmlData = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        $order_model = new RechargeModel();
        $order_no = $data['out_trade_no'];
        $wx_no = $data['transaction_id'];

        //获取充值信息
        $order_info = $order_model->getOne(['order_no'=>$order_no]);

        if (!empty($order_info['ali_wx_no'])) {
            die;
        }

        //判断算出的签名和通知信息的签名是否一致
        if($data['result_code'] == 'SUCCESS' && $data['appid'] == 'wx18e5b4f42773c3ec' && $data['mch_id'] == '1571608411'){
            
            $update_data = [];
            
            //更新充值订单
            $update_data['ali_wx_no'] = $wx_no;
            $update_data['pay_status'] = 1;
            $order_model->updateOne(['order_no'=>$order_no],$update_data);
            $user_model = new UserModel();
            $user_info = $user_model->getOne(['Id' => $order_info['user_id']]);
            $user_update = [];
            $user_update['ju_money'] = $user_info['ju_money'] + $order_info['ju_money'];
            $user_model->updateOne(['Id'=>$order_info['user_id']],$user_update);


            //处理完成之后，告诉微信成功结果
            echo '<xml>
                    <return_code><![CDATA[SUCCESS]]></return_code>
                    <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
            exit();
        }
    }
}