<?php

namespace App\libs\wechat\wxpay;

use App\libs\helpers\Helper;
use lc\wechat\wxpay\base\WxPayException;
use lc\wechat\wxpay\database\RedPack;
use lc\wechat\wxpay\database\Results;
use lc\wechat\wxpay\database\Transfer;
use lc\wechat\wxpay\database\UnifyOrder;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class WxPayHelper
{
    /**
     * 红包日志.
     *
     * @param Transfer|RedPack $database 红包类|企业支付零钱类
     */
    public static function writeRedPackLogs($database, array $output, int $type)
    {
        if (1 == $type) {
            $paymentNo = !empty($output['payment_no']) ? $output['payment_no'] : null;
            $openid    = $database->getAttributes('openid');
            $orderId   = $database->getAttributes('partner_trade_no');
            $amount    = $database->getAttributes('amount');
        } else {
            $paymentNo = !empty($output['send_listid']) ? $output['send_listid'] : null;
            $openid    = $database->getAttributes('re_openid');
            $orderId   = $database->getAttributes('mch_billno');
            $amount    = $database->getAttributes('total_amount');
        }
        Db::connect('main')
            ->table('lcapp_v4_hblogs')
            ->insert([
                'appid'        => Config::get('lc.wxpay.id'),
                'proj_id'      => env('id', Config::get('lc.pid')),
                'openid'       => $openid,
                'order_id'     => $orderId,
                'amount'       => $amount,
                'payment_no'   => $paymentNo,
                'input'        => json_encode($database->getValues(), JSON_UNESCAPED_UNICODE),
                'output'       => json_encode($output, JSON_UNESCAPED_UNICODE),
                'result_code'  => isset($output['result_code']) ? $output['result_code'] : $output['return_code'],
                'err_code'     => isset($output['err_code']) ? $output['err_code'] : null,
                'created_date' => date('YmdHis'),
                'created_time' => time(),
            ]);
    }

    /**
     * 记录调用日志.
     *
     * @param UnifyOrder $unify   统一下单类
     * @param array      $output  接口返回数据
     * @param string     $reqName 请求名
     */
    public static function writeWxOrderLogs($unify, array $output, string $reqName)
    {
        Db::table('lcapp_v4_wxorder_logs')
            ->insert([
                'req_name'     => $reqName,
                'appid'        => Config::get('lc.wxpay.id'),
                'proj_id'      => env('id', Config::get('lc.pid')),
                'openid'       => $unify->getAttributes('openid'),
                'order_id'     => $unify->getAttributes('out_trade_no'),
                'amount'       => $unify->getAttributes('total_fee') ? $unify->getAttributes('total_fee') : 0,
                'input'        => json_encode($unify->getValues(), JSON_UNESCAPED_UNICODE),
                'output'       => json_encode($output, JSON_UNESCAPED_UNICODE),
                'result_code'  => isset($output['result_code']) ? $output['result_code'] : $output['return_code'],
                'err_code'     => isset($output['err_code']) ? $output['err_code'] : null,
                'created_date' => date('YmdHis'),
                'created_time' => time(),
            ]);
    }

    /*
     * 发送红包请求
     *
     * @param   int     $type   1企业支付到零钱，2现金红包
     * @param   string  $url    请求地址
     * @param   array   $params 请求参数
     * @return  array
     */
    public static function sendRedPackRequest(int $type, string $url, array $params)
    {
        $class = (1 == $type) ? new Transfer() : new RedPack();
        foreach ($params as $key => $value) {
            $class->setAttributes($key, $value);
        }
        $class->setSign();

        $xml      = Helper::arrayToXml($class->getValues());
        $response = self::postXmlCurl($xml, $url, true, 6);
        $result   = Results::init($response, false);

        self::writeRedPackLogs($class, $result, $type);

        return $result;
    }

    /*
     * 查询红包请求
     *
     * @param   int     $type   1企业支付到零钱，2现金红包
     * @param   string  $url    请求地址
     * @param   array   $params 请求参数
     * @return  array
     */
    public static function queryRequest(int $type, string $url, array $params)
    {
        $class   = (1 == $type) ? new Transfer() : new RedPack();
        $apiName = (1 == $type) ? 'gettransferinfo' : 'gethbinfo';
        foreach ($params as $key => $value) {
            $class->setAttributes($key, $value);
        }
        $class->setSign();

        $xml      = Helper::arrayToXml($class->getValues());
        $response = self::postXmlCurl($xml, $url, true, 6);
        $result   = Results::init($response, false);

        self::writeWxOrderLogs($class, $result, $apiName);

        return $result;
    }

    /*
     * 发送统一下单请求
     *
     * @param   string  $url        请求地址
     * @param   array   $params     参数
     * @param   string  $apiName    接口名
     * @return  array
     */
    public static function sendUnifyRequest(string $url, array $params, string $apiName)
    {
        $unify = new UnifyOrder();
        foreach ($params as $key => $value) {
            $unify->setAttributes($key, $value);
        }
        $unify->setSign();

        $xml      = Helper::arrayToXml($unify->getValues());
        $response = self::postXmlCurl($xml, $url, in_array($apiName, ['refund', 'send_coupon']) ? true : false, 6);
        $result   = Results::init($response, false);

        self::writeWxOrderLogs($unify, $result, $apiName);

        return $result;
    }

    /**
     * 以post方式提交xml到对应的接口url.
     *
     * @param string $xml     需要post的xml数据
     * @param string $url     url
     * @param bool   $useCert 是否需要证书，默认不需要
     * @param int    $second  url执行超时时间，默认30s
     *
     * @throws WxPayException
     */
    public static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
    {
        $ch = curl_init();
        // 设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);

        // 如果有配置代理这里就设置代理
        if ('0.0.0.0' != WxPayConfig::$curlProxyPort && 0 != WxPayConfig::$curlProxyPort) {
            curl_setopt($ch, CURLOPT_PROXY, WxPayConfig::$curlProxyHost);
            curl_setopt($ch, CURLOPT_PROXYPORT, WxPayConfig::$curlProxyPort);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 严格校验
        // 设置header
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if (true == $useCert) {
            // 设置证书
            // 使用证书：cert 与 key 分别属于两个.pem文件
            curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLCERT, WxPayConfig::$sslCertPath);
            curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLKEY, WxPayConfig::$sslKeyPath);
        }
        // post提交方式
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        // 运行curl
        $data = curl_exec($ch);
        // 返回结果
        if ($data) {
            curl_close($ch);

            return $data;
        }
        $errorNo = curl_errno($ch);
        $error   = curl_error($ch);
        curl_close($ch);

        throw new WxPayException('curl出错，错误码: ' . $errorNo . '，错误信息：' . $error);
    }

    /**
     * 获取 JSAPI 支付，前端所需参数数据.
     *
     * @param string $prepayId 统一下单成功返回的id
     *
     * @return  array
     */
    public static function getJsApiParameters($prepayId)
    {
        new WxPayConfig();
        $data = [
            'appId'     => WxPayConfig::$appId,
            'timeStamp' => (string) time(),
            'nonceStr'  => Helper::generateNonceStr(),
            'package'   => 'prepay_id=' . $prepayId,
            'signType'  => 'MD5',
        ];

        $data['paySign'] = Helper::makeSign($data, WxPayConfig::$key);

        return $data;
    }
}
