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

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

class SklPayServices
{
    // 应用key
    private $merchantId = '';
    // 应用代码
    private $api_key = '';
    // 应用秘钥
    private $domain = '';
    // domain
    private $notifyUrl = '';
    private $payoutNotifyUrl = '';
    private $returnUrl = '';
    // 过期时间
    public $expireTime = 180;
    /**
     * @var array|mixed|null
     */

    public function __construct()
    {
        $this->merchantId = config('skl_pay.merchantId');
        $this->api_key = config('skl_pay.api_key');
        $this->domain = config('skl_pay.domain');
        $this->notifyUrl = config('skl_pay.notifyUrl');
        $this->payoutNotifyUrl = config('skl_pay.payoutNotifyUrl');
        $this->returnUrl = config('skl_pay.returnUrl');
    }

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

    /**
     * 存款
     * @param array $params 参数
     * @throws GameException
     */
    public  function deposit(array $params): array
    {
        $data = [
            'api_token' => $this->api_key,
            'amount' => $params['amount'],
            'gateway' => $params['paymentCode'],
            'pusername' => $params['name'],
            'invoice_no' => $params['orderNo'],
        ];
        return $this->doCurl('/api/transaction/init',$data);
    }

    /**
     * 代付
     * @param array $params 参数
     * @throws GameException
     */
    public  function payout(array $params): array
    {
        $data = [
            'api_token' => $this->api_key,
            'amount' => $params['amount'],
            'to_bank' => $params['bankCode'],
            'to_bank_account_no' => $params['bankAccountNo'],
            'account_holder' => $params['bankAccountName'],
            'invoice_no' => $params['orderNo'],
        ];
        return $this->doCurl('/api/transfer_out/init',$data);
    }

    /**
     * 订单查询
     * @param array $params 参数
     */
    public  function query(array $params): array
    {
        $data = [
            'transaction_id' => $params['orderNo'],
        ];
        return $this->doCurl('/api/transaction/get_status',$data);
    }

}
