<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\domain\channel\driver\wechat;

use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
use app\service\domain\channel\CoreChannelCatalogService;
use app\service\domain\pay\CorePayConfigService;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\MiniApp\Application as MiniAppApplication;
use EasyWeChat\OfficialAccount\Application as OfficialAccountApplication;
use think\App;


/**
 * Easywechat工厂类
 * @package app\service\domain\channel\driver\wechat
 */
class EasywechatFactory
{
    private App $app;
    private CorePayConfigService $payConfigService;

    /**
     * 构造函数
     * @param App $app
     * @param CorePayConfigService $payConfigService
     */
    public function __construct(App $app, CorePayConfigService $payConfigService)
    {
        $this->app = $app;
        $this->payConfigService = $payConfigService;
    }

    /**
     * 创建账号
     * @param array $baseConfig
     * @return OfficialAccountApplication
     */

    public function officialAccount(array $baseConfig): OfficialAccountApplication
    {
        return new OfficialAccountApplication([
            'app_id' => (string)($baseConfig['app_id'] ?? ''),
            'secret' => (string)($baseConfig['app_secret'] ?? ''),
            'token' => (string)($baseConfig['token'] ?? ''),
            'aes_key' => (string)($baseConfig['aes_key'] ?? ''),
            'http' => [
                'timeout' => 5.0,
                'retry' => true,
            ],
        ]);
    }

    /**
     * 创建小程序
     * @param array $baseConfig
     * @return MiniAppApplication
     */

    public function miniApp(array $baseConfig): MiniAppApplication
    {
        return new MiniAppApplication([
            'app_id' => (string)($baseConfig['app_id'] ?? ''),
            'secret' => (string)($baseConfig['app_secret'] ?? ''),
            'token' => (string)($baseConfig['token'] ?? ''),
            'aes_key' => (string)($baseConfig['aes_key'] ?? ''),
            'http' => [
                'timeout' => 5.0,
                'retry' => true,
            ],
        ]);
    }

    /**
     * 创建支付宝
     * @param string $channel
     */

    public function bootAlipay(string $channel): void
    {
        $config = $this->payConfigService->getProviderConfig($channel, 'alipay');
        if (!$config) {
            return;
        }

        $options = new Config();
        $options->protocol = 'https';
        $options->gatewayHost = trim((string)($config['gateway'] ?? 'openapi.alipay.com')) ?: 'openapi.alipay.com';
        $options->signType = 'RSA2';
        $options->appId = (string)($config['app_id'] ?? '');
        $options->merchantPrivateKey = (string)($config['private_key'] ?? '');
        $options->alipayPublicKey = (string)($config['alipay_public_key'] ?? '');
        $options->notifyUrl = (string)($config['notify_url'] ?? '');
        AlipayFactory::setOptions($options);
    }

    /**
     * 获取支付宝密钥掩码
     * @return string
     */

    public function secretMask(): string
    {
        return CoreChannelCatalogService::SECRET_MASK;
    }
}