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

use app\exception\GameException;
use support\Log;
use WebmanTech\LaravelHttpClient\Facades\Http;

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

    public function __construct()
    {
        $this->merchantId = config('one_pay.merchantId');
        $this->key = config('one_pay.key');
        $this->api_key = config('one_pay.api_key');
        $this->domain = config('one_pay.domain');
        $this->notifyUrl = config('one_pay.notifyUrl');
        $this->payoutNotifyUrl = config('one_pay.payoutNotifyUrl');
        $this->returnUrl = config('one_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('onepay:'.$api,json_decode($result, true));
        return json_decode($result, true);
    }

    /**
     * 创建签名
     * @throws GameException
     */
    public  function getAuth(): string
    {
        $data = [
            'username' => $this->merchantId,
            'api_key' => $this->api_key,
        ];
        $result = $this->doCurl('merchant/auth',$data);
        if ($result['status'] == 'true') {
            return $result['auth'];
        } else {
            throw new GameException($result['message'], 0);
        }
    }

    /**
     * 创建签名
     * @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
    {
        return md5($this->key.$params['order_id']);
    }

    /**
     * 存款
     * @param array $params 参数
     * @throws GameException
     */
    public  function deposit(array $params): array
    {
        $data = [
            'username' => $params['uuid'],
            'auth' => $this->getAuth(),
            'amount' => $params['amount'],
            'currency' => 'MYR',
            'orderid' => $params['orderNo'],
            'email' => $params['email'],
            'phone_number' => $params['phone'],
            'redirect_url' => $this->returnUrl.'?orderNo='.$params['orderNo'],
            'pay_method' => $params['paymentCode'],
            'callback_url' => $this->notifyUrl,
        ];
        if ($data['pay_method'] == 'online_banking') {
            $data['bank_id'] = $params['bank_id'];
        }
        return $this->doCurl('merchant/generate_orders',$data);
    }

    /**
     * 代付
     * @param array $params 参数
     * @throws GameException
     */
    public  function payout(array $params): array
    {
        $data = [
            'auth' => $this->getAuth(),
            'amount' => $params['amount'],
            'currency' => 'MYR',
            'orderid' => $params['orderNo'],
            'bank_id' => $params['bankCode'],
            'holder_name' => $params['bankAccountName'],
            'account_no' => $params['bankAccountNo'],
            'callback_url' => $this->payoutNotifyUrl,
        ];
        return $this->doCurl('merchant/withdraw_orders',$data);
    }

    /**
     * 订单查询
     * @param array $params 参数
     */
    public  function query(array $params): array
    {
        $data = [
            'username' => $this->merchantId,
            'id' => $params['orderNo'],
        ];
        return $this->doCurl('merchant/check_status',$data);
    }

}
