<?php

namespace app\common\payment;

use Payment\Client\Refund;
use Payment\Config;
use Payment\Common\PayException;
use support\Cache;

class Tlpay
{
    protected $config;

    public function __construct()
    {
        $this->config = config('payment.tl');
    }


    //当天交易请用撤销,非当天交易才用此退货接口
    public function refund($data)
    {
        $params = array();
        $params["cusid"] = AppConfig::CUSID;
        $params["appid"] = AppConfig::APPID;
        $params["version"] = AppConfig::APIVERSION;
        $params["trxamt"] = $data['trxamt'];
        $params["reqsn"] = $data['reqsn'];
        $params["oldtrxid"] = $data['oldtrxid'];//原来订单号
        $params["randomstr"] = $data['randomstr'];//
        $params["signtype"] = 'RSA';
        try {
            $params["sign"] = urlencode(AppUtil::Sign($params));//签名
            $paramsStr = AppUtil::ToUrlParams($params);
            $url = AppConfig::APIURL . "/refund";
            $rsp = http_post($url, $paramsStr);

            file_put_contents(runtime_path('logs/tlpay.log'), $rsp);

            return $rsp;
        } catch (\Exception $e) {
            throw new PayException("退款失败，原因：" . $e->getMessage());
        }

    }

//    public function refund($data)
//    {
//        if (!$data){
//            return json_fail('参数异常');
//        }
//
//        try {
//            $ret = Refund::run(Config::TL_REFUND, $this->config, $data);
//
//            file_put_contents(runtime_path('logs/tlpay.log'),json_encode($ret, JSON_UNESCAPED_UNICODE));
//            return $ret;
//        } catch (PayException $e) {
//            dump($e->getMessage());
//            throw new PayException("发起退款失败");
//        }
//
//    }
}