<?php
defined('IN_WEB') or die('Include Error!');
/**
 * 三方支 vovo验签
 */
class ModelPaymentVivo {

    const PLAT='vivo';

    public function getApayInfo() {
        $configs = oo::cfg('pay');
        return $configs[self::PLAT] ?? [];
    }

    public function getApiUrl($region = '') {
        if($region && $region == 'IN'){ //印度玩家订单查询地址
            return 'https://in-pay.vivoglobal.com/api/partner/query';
        }
        return 'https://pay.vivoglobal.com/api/partner/query';
    }

    /**
     * vivo订单查询
     * @param string $orderId
     * @param int $uid
     * @return bool
     */
    public function qryOrder($orderId, $uid, $region = '') {
        $conf = $this->getApayInfo();
        if(!isset($conf['appid'], $conf['appkey'])){
            oo::logs()->debug3([
                'uid' => $uid, 'data' => ['orderid' => $orderId, 'msg' => 'appinfo empty']
            ], 'checkorder.log');
            return false;
        }
        $param = [
            'signType'       => 'MD5',
            'appId'          => $conf['appid'],
            'partnerOrderId' => $orderId,
        ];
        $key = md5($conf['appkey']);
        $signStr = "appId={$param['appId']}&partnerOrderId={$param['partnerOrderId']}&{$key}";
        $param['sign'] = strtolower(md5($signStr));
        $headers = ['content-type: application/x-www-form-urlencoded'];
        $ret = oo::functions()->curl($this->getApiUrl($region), http_build_query($param), 1, 20, $headers);

        if(!isset($ret['responseCode'], $ret['tradeStatus']) || $ret['tradeStatus'] != 'Completed'){
            oo::logs()->debug3([
                'uid' => $uid, 'data' => ['orderid' => $orderId, 'data'=>(array)$ret, 'msg' => 'query return error']
            ], 'checkorder.log');
            return false;
        }
        return true;
    }
}
