<?php

namespace app\service\wx;

use EasyWeChat\Factory;
use EasyWeChat\Payment\Application;
use think\facade\Config;

/**
 * 微信支付
 * Class WechatPay
 * @package app\service\wx
 */
class WechatPay
{
    private static $app = null;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    //微信支付实例  单例模式
    public static function getInstance(): ?Application
    {
        if (self::$app == null) {
            Config::load('extra/wechat', 'wechat');
            $conf      = config('wechat');
            $config    = [
                // 必要配置
                'app_id'    => $conf['applets_appId'],//此处使用的小程序APPID
                'mch_id'    => $conf['mchid'],
                'key'       => $conf['key'],          // API 密钥

                // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
                'cert_path' => root_path().'config/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
                'key_path'  => root_path().'config/cert/apiclient_key.pem',  // XXX: 绝对路径!!!!

                'notify_url' => $conf['applets_notify_url'],     // 你也可以在下单时单独设置来想覆盖它
            ];
            self::$app = Factory::payment($config);
        }
        return self::$app;
    }
}