<?php
defined('IN_WEB') or die('Include Error!');

/**
 * 俄语支付
 * User: Administrator
 * Date: 2018/6/19
 * Time: 12:27
 */
class ModelPaymentru
{

    private $_gameKey = 'a17479231dc298309a3fda7d7d00111a';
    /**
     *	俄语发货接口
     */
    public function deliveryRussianPay($param){
//        oo::logs()->debug3('param'.json_encode($param),  'ruPaymentFail.php');
        $orderId = $param['cp_order_id'] ?? 0;
        $serialNum = $param['order_id'] ?? 0;
        $goodsId = $param['goods_id'] ?? 0;
        $amount = $param['pay_amount'] ?? 0;
        $gameId = $param['game_id'] ?? 0;
        $userId = $param['user_id'] ?? 0;
        $sign = $param['sign'] ?? '';

        if(empty($orderId) || empty($serialNum) || $amount <= 0 || empty($goodsId)) {
            oo::logs()->debug3('参数异常  param:'.json_encode($param),'ruPaymentFail.php');
            return $this->responsePayment(1, '参数异常');
        }

        $md5Sign = md5($gameId.$orderId.$userId.$this->_gameKey);
        if(empty($sign) || $sign != $md5Sign){
            oo::logs()->debug3('验签失败  param:'.json_encode($param). '  验签的sign:'.$sign.'  md5Sign:'.$md5Sign, 'ruPaymentFail.php');
           return $this->responsePayment(1,'验签失败');
        }

        $dbGoods = oo::commonOprModel('goods')->getGoodsInfoByOrderId($orderId);
        if(empty($dbGoods) || !isset($dbGoods['uid'])) {
            oo::logs()->debug3('订单不存在  param:'.json_encode($param).' dbgoods:'.json_encode($dbGoods) ,'ruPaymentFail.php');
            return $this->responsePayment(1, '订单不存在');
        }

        if(empty($goodsId) || $goodsId != $dbGoods['gpid']) {
            oo::logs()->debug3('商品ID不正确  param:'.json_encode($param).' dbgoods:'.json_encode($dbGoods) ,'ruPaymentFail.php');
            return $this->responsePayment(1, '订单不存在');
        }

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

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


    /**
     * 返回支付发货结果
     * @param $code
     * @param $signKey
     * @param $msg
     * @return string
     */
    public function responsePayment($code,  $msg)
    {
        $data['msg'] = $msg;
        $data['code'] = $code;
        return $data;
    }
}