1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
10
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
11
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
12
|
|
|
use Yansongda\Artful\Logger; |
13
|
|
|
use Yansongda\Artful\Rocket; |
14
|
|
|
|
15
|
|
|
use function Yansongda\Pay\get_provider_config; |
16
|
|
|
use function Yansongda\Pay\get_wechat_type_key; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see https://pay.weixin.qq.com/docs/merchant/apis/code-payment-v3/direct/code-pay.html |
20
|
|
|
*/ |
21
|
|
|
class PayPlugin implements PluginInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws ContainerException |
25
|
|
|
* @throws ServiceNotFoundException |
26
|
|
|
* @throws Throwable 随机字符串生成失败 |
27
|
|
|
*/ |
28
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
29
|
|
|
{ |
30
|
|
|
Logger::debug('[Wechat][V3][Pay][Pos][PayPlugin] 插件开始装载', ['rocket' => $rocket]); |
31
|
|
|
|
32
|
|
|
$payload = $rocket->getPayload(); |
33
|
|
|
$params = $rocket->getParams(); |
34
|
|
|
$config = get_provider_config('wechat', $params); |
35
|
|
|
|
36
|
|
|
if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) { |
37
|
|
|
$rocket->mergePayload(array_merge( |
38
|
|
|
[ |
39
|
|
|
'_method' => 'POST', |
40
|
|
|
'_url' => 'v3/pay/partner/transactions/codepay', |
41
|
|
|
], |
42
|
|
|
$this->service($payload, $params, $config) |
|
|
|
|
43
|
|
|
)); |
44
|
|
|
}else{ |
45
|
|
|
$rocket->mergePayload(array_merge( |
46
|
|
|
[ |
47
|
|
|
'_method' => 'POST', |
48
|
|
|
'_url' => 'v3/pay/transactions/codepay', |
49
|
|
|
], |
50
|
|
|
$this->normal($params, $config) |
51
|
|
|
)); |
52
|
|
|
} |
53
|
|
|
Logger::info('[Wechat][V3][Pay][Pos][PayPlugin] 插件装载完毕', ['rocket' => $rocket]); |
54
|
|
|
|
55
|
|
|
return $next($rocket); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function normal(array $params, array $config): array |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
'appid' => $config[get_wechat_type_key($params)] ?? '', |
62
|
|
|
'mchid' => $config['mch_id'] ?? '', |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function service(Collection $payload, array $params, array $config): array |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$configKey = get_wechat_type_key($params); |
69
|
|
|
|
70
|
|
|
$data = [ |
71
|
|
|
'sp_appid' => $config[$configKey] ?? '', |
72
|
|
|
'sp_mchid' => $config['mch_id'] ?? '', |
73
|
|
|
'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''), |
74
|
|
|
]; |
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|