<?php

namespace Callback\Controller;

use Org\SwiftpassSDK\Swiftpass;
use Org\UcenterSDK\Ucservice;


/**
 * 新版支付宝回调控制器(推广后台平台币充值)
 * @author ZCL
 */
class Notify3Controller extends BaseController
{
    /**
     *通知方法
     */
    public function alipay_notify()
    {
        //C(api('Config/lists'));
        if (IS_POST && !empty($_POST)) {
            $notify = $_POST;
        } elseif (IS_GET && !empty($_GET)) {
            $notify = $_GET;
            unset($notify['methodtype']);
            unset($notify['apitype']);
        } else {
            $notify = file_get_contents("php://input");
            if (empty($notify)) {
                $this->record_logs("Access Denied");
                exit('Access Denied');
            }
        }

        /*Vendor('Alipay.AopSdk');
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = file_get_contents("./Application/Home/SecretKey/alipay/alipay2_public_key.txt");
        $result = $aop->rsaCheckV1($notify,'','RSA2');

        if ($result) {*/
        //获取回调订单信息
        $order_info = $notify;
        if($order_info['trade_status'] == 'TRADE_SUCCESS'){
            $order_number = $order_info['out_trade_no'];
            $order = M('coin_pay_order', 'tab_')->where(array('order_status'=>0,"order_number"=>$order_number))->find();
            if($order){
                $data['pay_order_number']=$_POST['trade_no'];//支付宝交易号
                $data['pay_way']=1;
                $data['pay_callback_time']=time();
                $data['collection_account']='2019091067222198';
                $data['order_status'] = "1";

                M('coin_pay_order', 'tab_')->where("order_number='$order_number'")->save($data);
                $balance_map['promote_id'] = $order['promote_id'];
                $balance_map['game_id'] = 0;
                $balance = M('promote_balance_coin', 'tab_')->where($balance_map)->find();
                $this->balance_coin_update($order,$balance);//更新汇总
                $this->coin_record_add($order,$balance); //添加流水
                $this->promote_balance_coin_update($order); //更新推广员平台币余额
            }
            echo "success";
        }else{
            $this->record_logs("支付失败!");
            echo "fail";
        }

        /*} else {
            $this->record_logs("支付验证失败");
            redirect('http://' . $_SERVER['HTTP_HOST'] . '/media.php', 3, '支付验证失败');
        }*/
    }


    /**
     *微信充值回调
     */
    public function wxpay_notify(){
        $xml = file_get_contents('php://input');
        $Swiftpass=new Swiftpass("1555280551","wDst6e8d0nGUh4hcwBgv8caJmDE1Mxql");
        $Swiftpass->resHandler->setContent($xml);
        $Swiftpass->resHandler->setKey("wDst6e8d0nGUh4hcwBgv8caJmDE1Mxql");
        if($Swiftpass->resHandler->isTenpaySign()){
            if($Swiftpass->resHandler->getParameter('status') == 0 && $Swiftpass->resHandler->getParameter('result_code') == 0){
                $trade_no=$Swiftpass->resHandler->getParameter('transaction_id');
                $out_trade_no = $Swiftpass->resHandler->getParameter('out_trade_no');
                $order = M('coin_pay_order', 'tab_')->where(array('order_status'=>0,"order_number"=>$out_trade_no))->find();
                if($order){
                    $data['pay_order_number']=$trade_no;//微信交易号
                    $data['pay_way']=2;
                    $data['pay_callback_time']=time();
                    $data['collection_account']='1555280551';
                    $data['order_status'] = "1";

                    M('coin_pay_order', 'tab_')->where("order_number='$out_trade_no'")->save($data);
                    $balance_map['promote_id'] = $order['promote_id'];
                    $balance_map['game_id'] = 0;
                    $balance = M('promote_balance_coin', 'tab_')->where($balance_map)->find();
                    $this->balance_coin_update($order,$balance);//更新汇总
                    $this->coin_record_add($order,$balance); //添加流水
                    $this->promote_balance_coin_update($order); //更新推广员平台币余额
                }
                echo 'success';
            }else{
                echo 'failure';
            }
        }else{
            echo 'failure';
        }

    }

    /**
     * 双乾扫码支付回调
     */
    public function sq_alipay_scancode_notify(){
        $merNo = I('MerNo');
        $amount = I('Amount');
        $out_trade_no = I('BillNo');   // 平台订单号
        $trade_no = I('Orderno'); // 支付平台订单号
        $succed = I('Succeed');
        $result = I('Result');
        $bankOrderNo = I('bankOrderNo');
        $merRemark = I('MerRemark');
        $md5Info = I('MD5info');
        //$this->record_logs(var_export($_REQUEST,1));
        //$secret = strtoupper(md5(C('sqpay.key')));//支付秘钥
        $secret = strtoupper(md5('12345678'));

        //$this->record_logs("MD5:".$md5Info);
        //$this->record_logs("签名串:Amount={$amount}&BillNo={$out_trade_no}&MerNo={$merNo}&Succeed={$succed}&{$secret}");
        // 参与校验
        $sign = strtoupper(md5("Amount={$amount}&BillNo={$out_trade_no}&MerNo={$merNo}&Succeed={$succed}&{$secret}"));
        //$this->record_logs("签名值:$sign");
        if ($md5Info == $sign){
            if($succed==88){ //88表示充值成功
                $order = M('coin_pay_order', 'tab_')->where(array('order_status'=>0,"order_number"=>$out_trade_no))->find();
                $this->record_logs(var_export($order,1));
                if($order){
                    $data['pay_order_number']=$trade_no;//支付平台订单号
                    $data['pay_way']=4;
                    $data['pay_callback_time']=time();
                    $data['collection_account']=$merNo;
                    $data['order_status'] = "1";
                    M('coin_pay_order', 'tab_')->where("order_number='$out_trade_no'")->save($data);

                    $balance_map['promote_id'] = $order['promote_id'];
                    $balance_map['game_id'] = 0;
                    $balance = M('promote_balance_coin', 'tab_')->where($balance_map)->find();
                    $this->balance_coin_update($order,$balance);//更新汇总
                    $this->coin_record_add($order,$balance); //添加流水
                    $this->promote_balance_coin_update($order); //更新推广员平台币余额
                }
            }
            echo "success";
        }else{
            $this->record_logs("双乾验证失败!!");
            echo "fail";
        }
    }
    /**
     *判断平台币充值是否存在
     */

    protected function order_is_exist($out_trade_no)
    {
        $map['order_status'] = 0;
        $map['order_number'] = $out_trade_no;
        $res = M('coin_pay_order', 'tab_')->where($map)->find();
        if (empty($res)) {
            return false;
        } else {
            return true;
        }
    }

    //平台币汇总否存在

    public function promote_balance_coin_is_exist($promote_id)
    {
        $map['promote_id'] = $promote_id;
        $map['game_id'] = 0;
        $res = M('promote_balance_coin', 'tab_')->where($map)->find();
        if (empty($res)) {
            return false;
        } else {
            return true;
        }
    }

    // 添加流水
    public function coin_record_add($order, $balance){
        $promote = M('promote', 'tab_')->field(['id', 'level'])->where(['id' => $order['promote_id']])->find();
        $data['sn']=date('Ymd') . date('His') . sp_random_num(6);
        $data['type'] = 1;
        $data['sub_type'] = 1;
        $data['target_id'] = $promote['id'];
        $data['promote_id'] = $promote['id'];
        $data['target_level'] = $promote['level'];
        $data['target_type'] = 1;
        $data['ref_id'] = $order['id'];
        $data['game_id'] = 0;
        $data['coin'] = $order['real_amount'];
        $data['balance_coin'] = $balance['num']+$order['coin_num'];
        $data['remark'] = $order['remark'];
        $data['create_time'] = time();
        $data['description'] = '线上充值';
        M('promote_coin_record', 'tab_')->data($data)->add();
    }

    //更新汇总
    public function balance_coin_update($order,$balance){
        if(!empty($balance)){
            $map['promote_id'] = $order['promote_id'];
            $map['game_id'] = 0;
            M('promote_balance_coin', 'tab_')->where($map)->setInc('num',$order['coin_num']);
        }else{
            $balance['promote_id']= $order['promote_id'];
            $balance['game_id'] = 0;
            $balance['num']=$order['coin_num'];
            M('promote_balance_coin', 'tab_')->data($balance)->add();
        }
    }

    //更新推广员平台币余额
    public function promote_balance_coin_update($order){
        $map['id'] = $order['promote_id'];
        M('promote', 'tab_')->where($map)->setInc('balance_coin',$order['coin_num']);
    }
}