<?php
defined('IN_WEB') or die('Include Error!');
/**
 * Huawei本地支付
 */
/**
 * 靠谱支付
 * User: Administrator
 * Date: 2018/6/19
 * Time: 12:27
 */
class ModelPaymentkp
{

    private $appidEUR = '60001002';//欧版的appid
    private $_signKey = 'EEF1AD1A-9AA0.40D1-926d!11618fbbd7c5!';
    /**
     *	靠谱发货接口
     */
    public function deliveryKaopuPay($param){
//        oo::logs()->debug3('param'.json_encode($param));
        $appid = $param['appid'] ?? 0;
        $username = $param['username'] ?? '';
        $kpordernum = $param['kpordernum'] ?? '';
        $ywordernum = $param['ywordernum'] ?? '';
        $amount = $param['amount'] ?? 0;
        $data['username'] = $username;//靠谱用户在 CP 的账号唯一标识
        $data['kpordernum'] = $kpordernum;//靠谱订单 ID
        $data['ywordernum'] = $ywordernum;//CP 订单 ID
        $data['status'] = intval($param['status'] ?? 0);//订单状态 1 为成功,其他为失败
        $data['paytype'] = $param['paytype'] ?? '';//充值方式
        $data['amount'] = $amount;//成功充值金额(分)
        $data['gameserver'] = urldecode($param['gameserver'] ?? '');//区服名称(url 编码传输)
        $data['errdesc'] = $param['errdesc'] ?? '';//(该字段始终传空)
        $data['paytime'] = $param['paytime'] ?? '';//充值成功时间yyyyMMddHHmmss
        $data['gamename'] = urldecode($param['gamename'] ?? '');//游戏名称(url 编码传输)
        $signKey = $this->_signKey;
        $data['signKey'] = $signKey;

        $md5Sign = $this->md5Str($data);
        $sign = $param['sign'];
        if(empty($username) || empty($ywordernum) || $amount <= 0) {
            oo::logs()->debug3('参数异常  param:'.json_encode($param),'kpPaymentFail.php');
            return $this->responsePayment(1004, '参数异常');
        }

        if(empty($sign) || $sign != $md5Sign){
            oo::logs()->debug3('验签失败  param:'.json_encode($param),'kpPaymentFail.php');
            oo::logs()->debug3('验签的sign:'.$sign.'  md5Sign:'.$md5Sign, 'kpPaymentFail.php');
           return $this->responsePayment(1002,'验签失败');
        }

        if($data['status'] !== 1) {
            oo::logs()->debug3('充值失败  param:'.json_encode($param),'kpPaymentFail.php');
            return $this->responsePayment(1000, '充值失败');
        }

        $dbGoods = oo::commonOprModel('goods')->getGoodsInfoByOrderId($data['ywordernum']);
        if(empty($dbGoods) || !isset($dbGoods['uid'])) {
            oo::logs()->debug3('订单不存在  param:'.json_encode($param).' dbgoods:'.json_encode($dbGoods) ,'kpPaymentFail.php');
            return $this->responsePayment(1003, '订单不存在');
        }
        //获取靠谱用户的uid
        $uid = $this->getKPUid($username);
        if (empty($dbGoods) || empty($uid) || $uid != $dbGoods['uid']) {//验证用户对不对
            oo::logs()->debug3('用户不正确  param:'.json_encode($param).'  dbgoods:'.json_encode($dbGoods),'kpPaymentFail.php');
            return $this->responsePayment(1006, '用户不存在');
        }

        if($appid == $this->appidEUR) {
            $pusd = ($dbGoods['gpriceEur'] ?? 0) * 100;//欧版的按欧分算
        }else {
            $pusd = ($dbGoods['sl_pusd'] ?? 0) * 100;//美版的按美分算
        }

        if($pusd <= 0 || $pusd != $amount) { //验证支付金额对不对
            oo::logs()->debug3('商品价格错误  param:'.json_encode($param).'  dbgoods:'.json_encode($dbGoods),'kpPaymentFail.php');
            return $this->responsePayment(1009, '金额不正确');
        }

        $uid = $dbGoods['uid'];
        $pubDeliveryRet = oo::commonOprModel('payment')->pubDelivery($uid, $ywordernum, $kpordernum);
        if( $pubDeliveryRet === true || $pubDeliveryRet === -3 ){//发货成功或者是已发货的订单，则返回正确
            return $this->responsePayment(1000, '处理成功');
        }
        return $this->responsePayment(1005, '系统异常');
    }


    //对参数md5加密
    public function md5Str($data)
    {
        $str = '';
         foreach ($data as $v) {
              $str = $str.'|'.$v;
         }
         $str = ltrim($str, '|');
         return md5($str);
    }


    /**
     * 返回支付发货结果
     * @param $code
     * @param $signKey
     * @param $msg
     * @return string
     */
    public function responsePayment($code,  $msg)
    {
        $str = $code.'|'.$this->_signKey;
        $data['msg'] = $msg;
        $data['code'] = $code;
        $data['sign'] = md5($str);
        return $data;
    }


    /**
     * 通过kpid获取uid
     * @param $kpid
     * @return mixed
     */
    public function getKPUid($kpid)
    {
        //2.27-wsc-注释以下四行
//        $table = otable::kaopu();
//        $sql = "SELECT uid FROM {$table} WHERE kpid = '{$kpid}' LIMIT 1";
//        $ret = oo::commonOprDb('common')->getOne($sql, 1);
//        return intval($ret['uid'] ?? 0);
    }
}