<?php
defined('IN_WEB') or die('Include Error!');
/**
 * 支付相关
 */
class Payment
{
    private $_securityKey = '$sa2019@sz^6688$'; //下单的安全码
    /**
     *	验证码,用作下单的验证
     */
    protected function verify($param){
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);
        $device = oo::functions()->escape($param['device']);
        $ggameid = oo::functions()->uint($param['ggameid']);
        $sign = oo::functions()->escape($param['sign']);
        $verify = md5($uid.'|'.$gid.'|'.$device.'|'.$ggameid.'|'.$this->_securityKey);
        return $verify == $sign ? true : false;
    }

    /**
     *	发货验证
     *	uid_order_securityKey
     */
    protected function verifySuc($param){
        $sign = oo::functions()->escape($param['sign']);
        if (!$sign) {
            oo::logs()->debug3('result: sign为空', 'Paymentverify.php');
            return false;
        }
        $uid = oo::functions()->uint($param['uid']);
        $time = oo::functions()->uint($param['time']);
        $orderId = oo::functions()->escape($param['orderId']);
        $transactionId = '';
        if(isset($param['transactionId'])){
            $transactionId = $param['transactionId'];
        }
        $str = $uid.'$2019$'.$orderId.'$'.$time.'&^%'.$transactionId;
        $verifyStr = md5($str);
        return $sign == $verifyStr ? true : false;
    }

    /**
     *	下单
     */
    public function createOrder($param)
    {
        $data = array('code' => -1);
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);//商品ID
        $ggameid = oo::functions()->uint($param['ggameid']);//游戏平台ID
        $flag = oo::functions()->uint($param['flag']);//支付标识 1-商城  2-破产弹框  3-快捷支付  4-首冲|

        if( empty($uid) || empty($gid) || empty($ggameid) ){
            return json_encode($data);
        }

        //非客户端真实请求,验证码不对
        if( !$this->verify($param) ){
            $data['code'] = -10;
            return json_encode($data);
        }
        $data = oo::commonOprModel('payment')->createOrder($param);
        return json_encode($data);
    }

    /**
     * 支付
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/11/14 18:20
     */
    public function crazyGodSuccess($param){
        if ($param['sign'] && !$this->verifySuc($param)) {//如果sign不为空，并且验证不成功。则返回失败
            oo::logs()->debug3('result: 参数验证失败  param:'.json_encode($param), 'Paymentverify.php');
            return json_encode(['code'=>-1,'msg'=>'sign error']);
        }
        $data = oo::commonOprModel('payment')->payment($param);
        return $data;
    }

    public function BuyCard($param){
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);
        if(empty($uid) || empty($gid)){
            return json_encode(['code'=>-1]);
        }
        $tb = otable::goodsCrazy();
        $sql = "SELECT * FROM {$tb} WHERE gid={$gid}";
        $goodInfo = oo::commonOprDb('payment')->getOne($sql,1);
        if(empty($goodInfo) || $goodInfo['gtype'] != 3){
            return json_encode(['code'=>-2]);
        }
        $property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        $level = intval($property['levelId']);
        $tb = otable::CardChest();
        $sql = "SELECT * FROM {$tb} WHERE levelId={$level}";
        $coinInfo = oo::commonOprDb('payment')->getOne($sql,1);
        $goodMoney = intval($coinInfo[$goodInfo['gsku']]);

        if($goodMoney > intval($property['money'])){
            return json_encode(['code'=>-3,'msg'=>'money not enough']);
        }
      	oo::commonOprRedis('common')->incr(okeys::incr($goodInfo['gsku']));
        oo::commonOprModel('member')->optProperty($uid,['money'=>$goodMoney],'-',400,"购买宝箱:".$goodInfo['gsku']);
        $type =  intval($goodInfo['gimg']);
        oo::commonOprModel('props')->addUserProps($uid,$type,1,0,400);
        return json_encode(['code'=>1]);
    }


    /**
     *	发货
     */
    public function success($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        if ($param['sign'] && !$this->verifySuc($param)) {//如果sign不为空，并且验证不成功。则返回失败
            oo::logs()->debug3('result: 参数验证失败  param:'.json_encode($param), 'Paymentverify.php');
            $data['code'] = -1;
            return json_encode($data);
        }
        $data = oo::commonOprModel('payment')->success($param);
        return json_encode($data);
    }

    /**
     * 新版支付
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/4/26 17:13
     */
    public function successNew($param){
        $sign = oo::functions()->escape($param['sign']);
        if (empty($sign)) {
            return oo::response(-2,[],'签名异常');
        }
        $uid       = oo::functions()->uint($param['uid']);
        $time      = oo::functions()->uint($param['time']);
        $extraData = oo::functions()->escape($param['extraData']);
        if(isset($param['transactionId'])){
            $transactionId = $param['transactionId'];
        }
        $str = $uid.'$2019$'.$extraData.'$'.$time.'&^%'.$transactionId;
        $verifyStr = md5($str);
        if ($verifyStr != $sign) {
            return oo::response(-2,[],'签名异常');
        }
        return oo::commonOprModel('payment')->payment2($param);
    }


    /**
     * 进行越南支付
     * @param $param
     * @return string
     */
    public function createPaymentVN($param)
    {
        $orderId = oo::functions()->uint($param['orderId']);
        $channel = oo::functions()->escape($param['channel']);
        $serial = oo::functions()->uint($param['cardNo']);
        $passwd = oo::functions()->escape($param['serialNo']);
        $data = oo::commonOprModel('paymentvnpt')->createPayment($orderId, $channel, $serial, $passwd);
        return json_encode($data);
    }


    /**
     * 华为支付回调地址
     * @param $param
     * @return string
     */
    public function successPaymentHW($param)
    {
        $data = oo::commonOprModel('paymenthw')->deliveryHuaweiPay($param);
        return json_encode($data);
    }


    /**
     * 靠谱支付回调地址
     * @param $param
     * @return string
     */
    public function successPaymentKP($param)
    {
        $data = oo::commonOprModel('paymentkp')->deliveryKaopuPay($param);
        return json_encode($data);
    }

  	/**
     * 购买喇叭
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/11/20 20:00
     */
    public function buySpeaker($param){
        $uid = oo::functions()->uint($param['uid']);
        $coins =  oo::commonOprModel('goods')->getGoodCoins($uid,'coins01');
        $coins = ceil($coins/2);
        $property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        $money = intval($property['money']);
        if($money<$coins){
            return oo::response(-1,[],'金币不足');
        }
        oo::commonOprModel('member')->optProperty($uid,array('money'=>$coins),'-',401,"购买喇叭");
        $num = oo::commonOprRedis('common')->hGet(okeys::chatMaxNum(),$uid);
        oo::commonOprRedis('common')->hSet(okeys::chatMaxNum(),$uid,intval($num)-10);
        return oo::response(1,['num'=>1-intval($num)+10]);
    }


    /**
     * 购买矿工小游戏
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/5/12 12:21
     */
    public function miningGame($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('payment')->buyMiningGameCount($uid);
    }

    /**
     * 购买矿工小游戏
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/5/12 12:21
     */
    public function buyGift($param){
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);
        if(empty($uid) || empty($uid)){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('payment')->buyGift($uid,$gid);
    }
}