1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Yansongda\Artful\Artful; |
10
|
|
|
use Yansongda\Artful\Event\ArtfulEnd; |
11
|
|
|
use Yansongda\Artful\Event\ArtfulStart; |
12
|
|
|
use Yansongda\Artful\Event\HttpEnd; |
13
|
|
|
use Yansongda\Artful\Event\HttpStart; |
14
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
15
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
16
|
|
|
use Yansongda\Pay\Provider\Alipay; |
17
|
|
|
use Yansongda\Pay\Provider\Douyin; |
18
|
|
|
use Yansongda\Pay\Provider\Jsb; |
19
|
|
|
use Yansongda\Pay\Provider\Unipay; |
20
|
|
|
use Yansongda\Pay\Provider\Wechat; |
21
|
|
|
use Yansongda\Pay\Service\AlipayServiceProvider; |
22
|
|
|
use Yansongda\Pay\Service\DouyinServiceProvider; |
23
|
|
|
use Yansongda\Pay\Service\JsbServiceProvider; |
24
|
|
|
use Yansongda\Pay\Service\UnipayServiceProvider; |
25
|
|
|
use Yansongda\Pay\Service\WechatServiceProvider; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @method static Alipay alipay(array $config = [], $container = null) |
29
|
|
|
* @method static Wechat wechat(array $config = [], $container = null) |
30
|
|
|
* @method static Unipay unipay(array $config = [], $container = null) |
31
|
|
|
* @method static Jsb jsb(array $config = [], $container = null) |
32
|
|
|
* @method static Douyin douyin(array $config = [], $container = null) |
33
|
|
|
*/ |
34
|
|
|
class Pay |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* 正常模式. |
38
|
|
|
*/ |
39
|
|
|
public const MODE_NORMAL = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* 沙箱模式. |
43
|
|
|
*/ |
44
|
|
|
public const MODE_SANDBOX = 1; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 服务商模式. |
48
|
|
|
*/ |
49
|
|
|
public const MODE_SERVICE = 2; |
50
|
|
|
|
51
|
|
|
protected static array $providers = [ |
52
|
|
|
AlipayServiceProvider::class, |
53
|
|
|
WechatServiceProvider::class, |
54
|
|
|
UnipayServiceProvider::class, |
55
|
|
|
JsbServiceProvider::class, |
56
|
|
|
DouyinServiceProvider::class, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws ContainerException |
61
|
|
|
* @throws ServiceNotFoundException |
62
|
|
|
*/ |
63
|
|
|
public static function __callStatic(string $service, array $config = []) |
64
|
|
|
{ |
65
|
|
|
if (!empty($config)) { |
66
|
|
|
self::config(...$config); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return Artful::get($service); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws ContainerException |
74
|
|
|
*/ |
75
|
|
|
public static function config(array $config = [], null|Closure|ContainerInterface $container = null): bool |
76
|
|
|
{ |
77
|
|
|
$result = Artful::config($config, $container); |
78
|
|
|
|
79
|
|
|
if ($result) { |
80
|
|
|
Event::addListener(ArtfulStart::class, [PayListener::class, 'artfulStart']); |
81
|
|
|
Event::addListener(ArtfulEnd::class, [PayListener::class, 'artfulEnd']); |
82
|
|
|
Event::addListener(HttpStart::class, [PayListener::class, 'httpStart']); |
83
|
|
|
Event::addListener(HttpEnd::class, [PayListener::class, 'httpEnd']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach (self::$providers as $provider) { |
87
|
|
|
Artful::load($provider); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws ContainerException |
95
|
|
|
*/ |
96
|
|
|
public static function set(string $name, mixed $value): void |
97
|
|
|
{ |
98
|
|
|
Artful::set($name, $value); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @throws ContainerException |
103
|
|
|
* @throws ServiceNotFoundException |
104
|
|
|
*/ |
105
|
|
|
public static function get(string $service): mixed |
106
|
|
|
{ |
107
|
|
|
return Artful::get($service); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public static function setContainer(null|Closure|ContainerInterface $container): void |
111
|
|
|
{ |
112
|
|
|
Artful::setContainer($container); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function clear(): void |
116
|
|
|
{ |
117
|
|
|
Artful::clear(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|