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