Pay.php
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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());
}
}