<?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;


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' && $_POST['auth_app_id'] == '2021002102631089' && $_POST['seller_id'] = '2088431603383955') {
                $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' && $_POST['auth_app_id'] == '2021002102631089' && $_POST['seller_id'] = '2088431603383955') {
                $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';
    }
    
}