1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
use Yansongda\Pay\Contract\ConfigInterface; |
6
|
|
|
use Yansongda\Pay\Pay; |
7
|
|
|
use Yansongda\Supports\Config; |
8
|
|
|
use Yansongda\Supports\Str; |
9
|
|
|
|
10
|
|
View Code Duplication |
if (!function_exists('get_alipay_config')) { |
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
13
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
14
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
15
|
|
|
*/ |
16
|
|
|
function get_alipay_config(array $params): Config |
17
|
|
|
{ |
18
|
|
|
$alipay = Pay::get(ConfigInterface::class)->get('alipay'); |
19
|
|
|
|
20
|
|
|
$config = $params['_config'] ?? 'default'; |
21
|
|
|
|
22
|
|
|
return new Config($alipay[$config] ?? []); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (!function_exists('get_alipay_cert')) { |
27
|
|
|
/** |
28
|
|
|
* @return false|resource|string |
29
|
|
|
*/ |
30
|
|
|
function get_alipay_cert(string $key) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
if (Str::endsWith($key, '.crt')) { |
33
|
|
|
$key = file_get_contents($key); |
34
|
|
|
} elseif (Str::endsWith($key, '.pem')) { |
35
|
|
|
$key = openssl_pkey_get_private( |
36
|
|
|
Str::startsWith($key, 'file://') ? $key : 'file://'.$key |
37
|
|
|
); |
38
|
|
|
} else { |
39
|
|
|
$key = "-----BEGIN RSA PRIVATE KEY-----\n". |
40
|
|
|
wordwrap($key, 64, "\n", true). |
41
|
|
|
"\n-----END RSA PRIVATE KEY-----"; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $key; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
if (!function_exists('get_wechat_config')) { |
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
51
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
52
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
53
|
|
|
*/ |
54
|
|
|
function get_wechat_config(array $params): Config |
55
|
|
|
{ |
56
|
|
|
$wechat = Pay::get(ConfigInterface::class)->get('wechat'); |
57
|
|
|
|
58
|
|
|
$config = $params['_config'] ?? 'default'; |
59
|
|
|
|
60
|
|
|
return new Config($wechat[$config] ?? []); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.