<?php

namespace app\common\controller;

use extend\alipay\Alipay as AlipaySdk;
use app\ros\model\Order as OrderModel;
use app\http\model\User as UserModel;
use app\http\model\Recharge as RechargeModel;
use app\jinyou\model\ProductOrder as ProductOrderModel;
use app\jinyou\model\Account as AccountModel;
use app\jinyou\model\Package as PackageModel;
use app\jinqiao\model\Order as JqOrderModel;
use app\jinqiao\model\Key as KeyModel;


class Alipay {

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

        $ali = new AlipaySdk();

        $flag = $ali->payVerify2($_POST);
        
        if($flag){
            
            $status = $_POST['trade_status'];

            $flag = false;

            if ($status == 'TRADE_SUCCESS') {
                $flag = true;
            } 

            if ($flag) {

                $order_model = new OrderModel();
                $order_no=$_POST['out_trade_no'];
                $alipay_no = $_POST['trade_no'];

                

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

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

                $update_data = [];
            
                //更新充值订单
                $update_data['ali_wx_no'] = $alipay_no;
                $update_data['status'] = 1;
                $order_model->updateOne(['order_no'=>$order_no],$update_data);
            }
        } 
        
        echo 'success';
    }
    public function httpalipayVerify() {

        $ali = new AlipaySdk();

        $flag = $ali->payVerify2($_POST);
        
        if($flag){
            
            $status = $_POST['trade_status'];

            $flag = false;

            if ($status == 'TRADE_SUCCESS') {
                $flag = true;
            } 

            if ($flag) {

                $recharge_model = new RechargeModel();
                $order_no=$_POST['out_trade_no'];
                $alipay_no = $_POST['trade_no'];

                

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

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

                $update_data = [];
            
                //更新充值订单
                $update_data['ali_wx_no'] = $alipay_no;
                $update_data['pay_status'] = 1;
                if($recharge_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 'success';
    }

    public function jinyoualipayverify() {

        $ali = new AlipaySdk();

        $flag = $ali->payVerify2($_POST);
        
        if($flag){
            
            $status = $_POST['trade_status'];

            $flag = false;

            if ($status == 'TRADE_SUCCESS') {
                $flag = true;
            } 

            if ($flag) {

                $order_model = new ProductOrderModel();
                $order_no=$_POST['out_trade_no'];
                $alipay_no = $_POST['trade_no'];

                

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

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

                $update_data = [];
            
                //更新充值订单
                $update_data['TradeNo'] = $alipay_no;
                $update_data['OrderState'] = 90;
                if($order_model->updateOne(['OrderNo'=>$order_no],$update_data)) {
                    $account_info = json_decode($order_info['remark2'],true);
                    
                    $user_model = new UserModel();
                    $user_info = $user_model->getOne(['Id' => $order_info['UserId']]);
                    
                    $package_model = new PackageModel;//套餐详情
                    $package_info = $package_model->getOne(['Id' => $account_info['package_id']]);

                    $account_model = new AccountModel;
                    
                    $package_info['ProductName'] = $order_info['ProductName'];
                    $account_model->open_account($account_info,$user_info,$package_info);
                }
            }
        } 
        
        echo 'success';
    }

    public function jinqiaoalipayverify() {

        $ali = new AlipaySdk();

        $flag = $ali->payVerify2($_POST);
        
        if($flag){
            
            $status = $_POST['trade_status'];

            $flag = false;

            if ($status == 'TRADE_SUCCESS') {
                $flag = true;
            } 

            if ($flag) {

                $order_model = new JqOrderModel();
                $order_no=$_POST['out_trade_no'];
                $alipay_no = $_POST['trade_no'];

                

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

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

                $update_data = [];
            
                //更新充值订单
                $update_data['ali_wx_no'] = $alipay_no;
                $update_data['status'] = 1;
                if($order_model->updateOne(['order_no'=>$order_no],$update_data)) {
                    
                    $key_model = new KeyModel();
                    $key_model->where(['package_id' => $order_info['package_id'],'isused' =>0])->limit(1)->update(['isused' => 1,'user_id' => $order_info['user_id']]);
                    
                }
            }
        } 
        
        echo 'success';
    }
    
}