<?php
// 日本手机号发送短信
namespace app\service;

use support\Log;
use WebmanTech\LaravelHttpClient\Facades\Http;

class SePayServices
{
    // 应用key
    private $merchantId = '';
    // 应用代码
    private $key = '';
    // 应用秘钥
    private $domain = '';
    // domain
    private $notifyUrl = '';
    private $payoutNotifyUrl = '';
    private $returnUrl = '';
    // 过期时间
    public $expireTime = 180;

    public function __construct()
    {
        $this->merchantId = config('se-pay.merchantId');
        $this->key = config('se-pay.MD5');
        $this->domain = config('se-pay.domain');
        $this->notifyUrl = config('se-pay.notifyUrl');
        $this->payoutNotifyUrl = config('se-pay.payoutNotifyUrl');
        $this->returnUrl = config('se-pay.returnUrl');
    }

    /**
     * 执行请求
     * @param string $api 接口
     * @param array $params 参数
     */
    public function doCurl(string $api, array $params): array
    {
        $result = Http::timeout(10)->asForm()->post($this->domain.$api, $params);
        Log::channel('pay_log')->info('sepay:'.$api,json_decode($result, true));
        return json_decode($result, true);
    }

    /**
     * 创建签名
     * @param array $params 参数
     */
    public  function calculateSignature(array $params): string
    {
        ksort($params);
        $signature_string = '';
        foreach ($params as $key => $value) {
            $signature_string .= $key . ':' . $value . '&';
        }
        $signature_string .= 'key:' . $this->key;
        return  strtoupper(md5($signature_string));
    }

    /**
     * 验证返回签名
     * @param array $params 参数
     */
    public  function verifySign(array $params): string
    {
        ksort($params);
        unset($params['signMsg']);
        $signature_string = '';
        foreach ($params as $key => $value) {
            $signature_string .= $key . ':' . $value . '&';
        }
        $signature_string .= 'key:' . $this->key;
        return strtoupper(md5($signature_string));
    }

    /**
     * 存款
     * @param array $params 参数
     */
    public  function deposit(array $params): array
    {
        $params['notifyUrl'] = $this->notifyUrl;
        $params['returnUrl'] = $this->returnUrl.'?orderNo='.$params['orderNo'];
        $request_str = base64_encode(json_encode($params));
        $data = [
            'merchantNo' => $this->merchantId,
            'request' => $request_str,
            'version' => '1.0',
        ];
        $signature = $this->calculateSignature($data);
        $data['signMsg'] = $signature;
        return $this->doCurl('qrh5',$data);
    }

    /**
     * 代付
     * @param array $params 参数
     */
    public  function payout(array $params): array
    {
        $params['notifyUrl'] = $this->payoutNotifyUrl;
        $request_str = base64_encode(json_encode($params));
        $data = [
            'merchantNo' => $this->merchantId,
            'request' => $request_str,
            'version' => '1.0',
        ];
        $signature = $this->calculateSignature($data);
        $data['signMsg'] = $signature;
        return $this->doCurl('payout',$data);
    }

    /**
     * 订单查询
     * @param array $params 参数
     */
    public  function query(array $params): array
    {
        $request_str = base64_encode(json_encode($params));
        $data = [
            'merchantNo' => $this->merchantId,
            'request' => $request_str,
            'version' => '1.0',
        ];
        $signature = $this->calculateSignature($data);
        $data['signMsg'] = $signature;
        return $this->doCurl('query',$data);
    }

}
