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\Pay\get_wechat_config; |
||
18 | use function Yansongda\Pay\get_wechat_type_key; |
||
19 | |||
20 | /** |
||
21 | * @see https://pay.weixin.qq.com/docs/merchant/apis/cash-coupons/coupon/send-coupon.html |
||
22 | * @see https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/coupon/send-coupon.html |
||
23 | */ |
||
24 | class SendPlugin implements PluginInterface |
||
25 | { |
||
26 | /** |
||
27 | * @throws InvalidParamsException |
||
28 | * @throws ContainerException |
||
29 | * @throws ServiceNotFoundException |
||
30 | */ |
||
31 | public function assembly(Rocket $rocket, Closure $next): Rocket |
||
32 | { |
||
33 | Logger::debug('[Wechat][V3][Marketing][Coupon][Coupons][SendPlugin] 插件开始装载', ['rocket' => $rocket]); |
||
34 | |||
35 | $params = $rocket->getParams(); |
||
36 | $config = get_wechat_config($params); |
||
37 | $payload = $rocket->getPayload(); |
||
38 | $openId = $payload?->get('openid') ?? null; |
||
39 | |||
40 | if (empty($openId)) { |
||
41 | throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 发放指定批次的代金券,参数缺少 `openid`'); |
||
42 | } |
||
43 | |||
44 | $rocket->setPayload(array_merge( |
||
45 | [ |
||
46 | '_method' => 'POST', |
||
47 | '_url' => 'v3/marketing/favor/users/'.$openId.'/coupons', |
||
48 | '_service_url' => 'v3/marketing/favor/users/'.$openId.'/coupons', |
||
49 | ], |
||
50 | $this->normal($payload, $params, $config), |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
51 | )); |
||
52 | |||
53 | Logger::info('[Wechat][V3][Marketing][Coupon][Coupons][SendPlugin] 插件装载完毕', ['rocket' => $rocket]); |
||
54 | |||
55 | return $next($rocket); |
||
56 | } |
||
57 | |||
58 | protected function normal(Collection $payload, array $params, array $config): array |
||
59 | { |
||
60 | if (empty($payload->get('appid'))) { |
||
61 | $payload->set('appid', $config[get_wechat_type_key($params)] ?? ''); |
||
62 | } |
||
63 | |||
64 | if (empty($payload->get('stock_creator_mchid'))) { |
||
65 | $payload->set('stock_creator_mchid', $config['mch_id'] ?? ''); |
||
66 | } |
||
67 | |||
68 | return $payload->except('openid')->all(); |
||
69 | } |
||
70 | } |
||
71 |