OnlyContractPlugin.php
1.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
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Plugin\Wechat\Papay;
use Closure;
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Direction\NoHttpRequestDirection;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\InvalidConfigException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Rocket;
use function Yansongda\Pay\get_wechat_config;
use function Yansongda\Pay\get_wechat_sign_v2;
/**
* 返回只签约(委托代扣)参数.
*
* @see https://pay.weixin.qq.com/wiki/doc/api/wxpay_v2/papay/chapter3_3.shtml
*/
class OnlyContractPlugin implements PluginInterface
{
/**
* @throws ContainerException
* @throws InvalidConfigException
* @throws ServiceNotFoundException
*/
public function assembly(Rocket $rocket, Closure $next): Rocket
{
$config = get_wechat_config($rocket->getParams());
$wechatId = $this->getWechatId($config, $rocket->getParams());
if (!$rocket->getPayload()->has('notify_url')) {
$wechatId['notify_url'] = $config['notify_url'] ?? '';
}
$rocket->mergePayload($wechatId);
$rocket->mergePayload([
'sign' => get_wechat_sign_v2($rocket->getParams(), $rocket->getPayload()->all()),
]);
$rocket->setDestination($rocket->getPayload());
$rocket->setDirection(NoHttpRequestDirection::class);
return $next($rocket);
}
protected function getWechatId(array $config, array $params): array
{
$configKey = $this->getConfigKey($params);
return [
'appid' => $config[$configKey] ?? '',
'mch_id' => $config['mch_id'] ?? '',
];
}
protected function getConfigKey(array $params): string
{
$key = ($params['_type'] ?? 'mp').'_app_id';
if ('app_app_id' === $key) {
$key = 'app_id';
}
return $key;
}
}