|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Yansongda\Pay\Contract\PluginInterface; |
|
9
|
|
|
use Yansongda\Pay\Exception\InvalidConfigException; |
|
10
|
|
|
use Yansongda\Pay\Rocket; |
|
11
|
|
|
use Yansongda\Supports\Collection; |
|
12
|
|
|
use Yansongda\Supports\Str; |
|
13
|
|
|
|
|
14
|
|
|
class SignPlugin implements PluginInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
18
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
19
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
20
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
21
|
|
|
*/ |
|
22
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
23
|
|
|
{ |
|
24
|
|
|
$this->filterPayload($rocket); |
|
25
|
|
|
|
|
26
|
|
|
$privateKey = $this->getPrivateKey($rocket->getParams()); |
|
27
|
|
|
|
|
28
|
|
|
openssl_sign($this->getSignContent($rocket->getPayload()), $sign, $privateKey, OPENSSL_ALGO_SHA256); |
|
29
|
|
|
|
|
30
|
|
|
$sign = base64_encode($sign); |
|
31
|
|
|
|
|
32
|
|
|
!is_resource($privateKey) ?: openssl_free_key($privateKey); |
|
33
|
|
|
|
|
34
|
|
|
$rocket->mergePayload(['sign' => $sign]); |
|
35
|
|
|
|
|
36
|
|
|
return $next($rocket); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function filterPayload(Rocket $rocket): void |
|
40
|
|
|
{ |
|
41
|
|
|
$payload = $rocket->getPayload()->filter(function ($v, $k) { |
|
42
|
|
|
return '' !== $v && !is_null($v) && 'sign' != $k; |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$contents = array_filter($payload->get('biz_content', []), function ($v, $k) { |
|
46
|
|
|
return !Str::startsWith($k, '_'); |
|
47
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
|
48
|
|
|
|
|
49
|
|
|
$rocket->setPayload( |
|
50
|
|
|
$payload->merge(['biz_content' => json_encode($contents)]) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerDependencyException |
|
56
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
57
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidConfigException |
|
58
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
59
|
|
|
* |
|
60
|
|
|
* @return false|resource|string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getPrivateKey(array $params) |
|
63
|
|
|
{ |
|
64
|
|
|
$privateKey = get_alipay_config($params)->get('app_secret_cert'); |
|
65
|
|
|
|
|
66
|
|
|
if (is_null($privateKey)) { |
|
67
|
|
|
throw new InvalidConfigException(InvalidConfigException::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [app_secret_cert]'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return get_public_crt_or_private_cert($privateKey); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function getSignContent(Collection $payload): string |
|
74
|
|
|
{ |
|
75
|
|
|
return $payload->sortKeys()->toString(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|