1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\Coupon\Coupons; |
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\Supports\Collection; |
16
|
|
|
|
17
|
|
|
use function Yansongda\Artful\filter_params; |
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/cash-coupons/coupon/list-coupons-by-filter.html |
23
|
|
|
* @see https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/coupon/list-coupons-by-filter.html |
24
|
|
|
*/ |
25
|
|
|
class QueryUserPlugin implements PluginInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @throws InvalidParamsException |
29
|
|
|
* @throws ContainerException |
30
|
|
|
* @throws ServiceNotFoundException |
31
|
|
|
*/ |
32
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
33
|
|
|
{ |
34
|
|
|
Logger::debug('[Wechat][V3][Marketing][Coupon][Coupons][QueryUserPlugin] 插件开始装载', ['rocket' => $rocket]); |
35
|
|
|
|
36
|
|
|
$params = $rocket->getParams(); |
37
|
|
|
$config = get_wechat_config($params); |
38
|
|
|
$payload = $rocket->getPayload(); |
39
|
|
|
$openId = $payload?->get('openid') ?? null; |
40
|
|
|
|
41
|
|
|
if (empty($openId)) { |
42
|
|
|
throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 根据商户号查用户的券,参数缺少 `openid`'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$rocket->setPayload([ |
46
|
|
|
'_method' => 'GET', |
47
|
|
|
'_url' => 'v3/marketing/favor/users/'.$openId.'/coupons?'.$this->normal($payload, $params, $config), |
|
|
|
|
48
|
|
|
'_service_url' => 'v3/marketing/favor/users/'.$openId.'/coupons?'.$this->normal($payload, $params, $config), |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
Logger::info('[Wechat][V3][Marketing][Coupon][Coupons][QueryUserPlugin] 插件装载完毕', ['rocket' => $rocket]); |
52
|
|
|
|
53
|
|
|
return $next($rocket); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function normal(Collection $payload, array $params, array $config): string |
57
|
|
|
{ |
58
|
|
|
$appId = $payload->get('appid'); |
59
|
|
|
|
60
|
|
|
if (is_null($appId)) { |
61
|
|
|
$payload->set('appid', $config[get_wechat_type_key($params)] ?? ''); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return filter_params($payload)->except('openid')->query(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|