123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace Yansongda\Pay;
- use Exception;
- use Yansongda\Pay\Contracts\GatewayApplicationInterface;
- use Yansongda\Pay\Exceptions\InvalidGatewayException;
- use Yansongda\Pay\Gateways\Alipay;
- use Yansongda\Pay\Gateways\Wechat;
- use Yansongda\Pay\Listeners\KernelLogSubscriber;
- use Yansongda\Supports\Config;
- use Yansongda\Supports\Log;
- use Yansongda\Supports\Logger;
- use Yansongda\Supports\Str;
- /**
- * @method static Alipay alipay(array $config) 支付宝
- * @method static Wechat wechat(array $config) 微信
- */
- class Pay
- {
- /**
- * Config.
- *
- * @var Config
- */
- protected $config;
- /**
- * Bootstrap.
- *
- * @author yansongda <me@yansongda.cn>
- *
- * @throws Exception
- */
- public function __construct(array $config)
- {
- $this->config = new Config($config);
- $this->registerLogService();
- $this->registerEventService();
- }
- /**
- * Magic static call.
- *
- * @author yansongda <me@yansongda.cn>
- *
- * @param string $method
- * @param array $params
- *
- * @throws InvalidGatewayException
- * @throws Exception
- */
- public static function __callStatic($method, $params): GatewayApplicationInterface
- {
- $app = new self(...$params);
- return $app->create($method);
- }
- /**
- * Create a instance.
- *
- * @author yansongda <me@yansongda.cn>
- *
- * @param string $method
- *
- * @throws InvalidGatewayException
- */
- protected function create($method): GatewayApplicationInterface
- {
- $gateway = __NAMESPACE__.'\\Gateways\\'.Str::studly($method);
- if (class_exists($gateway)) {
- return self::make($gateway);
- }
- throw new InvalidGatewayException("Gateway [{$method}] Not Exists");
- }
- /**
- * Make a gateway.
- *
- * @author yansongda <me@yansonga.cn>
- *
- * @param string $gateway
- *
- * @throws InvalidGatewayException
- */
- protected function make($gateway): GatewayApplicationInterface
- {
- $app = new $gateway($this->config);
- if ($app instanceof GatewayApplicationInterface) {
- return $app;
- }
- throw new InvalidGatewayException("Gateway [{$gateway}] Must Be An Instance Of GatewayApplicationInterface");
- }
- /**
- * Register log service.
- *
- * @author yansongda <me@yansongda.cn>
- *
- * @throws Exception
- */
- protected function registerLogService()
- {
- $config = $this->config->get('log');
- $config['identify'] = 'yansongda.pay';
- $logger = new Logger();
- $logger->setConfig($config);
- Log::setInstance($logger);
- }
- /**
- * Register event service.
- *
- * @author yansongda <me@yansongda.cn>
- */
- protected function registerEventService()
- {
- Events::setDispatcher(Events::createDispatcher());
- Events::addSubscriber(new KernelLogSubscriber());
- }
- }
|