yansongda /
pay
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\App; |
||
| 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_provider_config; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @see https://pay.weixin.qq.com/docs/merchant/apis/in-app-payment/query-by-wx-trade-no.html |
||
| 21 | * @see https://pay.weixin.qq.com/docs/partner/apis/partner-in-app-payment/query-by-wx-trade-no.html |
||
| 22 | */ |
||
| 23 | class QueryByWxPlugin implements PluginInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @throws ContainerException |
||
| 27 | * @throws InvalidParamsException |
||
| 28 | * @throws ServiceNotFoundException |
||
| 29 | */ |
||
| 30 | public function assembly(Rocket $rocket, Closure $next): Rocket |
||
| 31 | { |
||
| 32 | Logger::debug('[Wechat][V3][Pay][App][QueryByWxPlugin] 插件开始装载', ['rocket' => $rocket]); |
||
| 33 | |||
| 34 | $params = $rocket->getParams(); |
||
| 35 | $config = get_provider_config('wechat', $params); |
||
| 36 | $payload = $rocket->getPayload(); |
||
| 37 | $transactionId = $payload?->get('transaction_id') ?? null; |
||
| 38 | |||
| 39 | if (empty($transactionId)) { |
||
| 40 | throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: App 通过微信订单号查询订单,参数缺少 `transaction_id`'); |
||
| 41 | } |
||
| 42 | |||
| 43 | $rocket->setPayload([ |
||
| 44 | '_method' => 'GET', |
||
| 45 | '_url' => 'v3/pay/transactions/id/'.$transactionId.'?'.$this->normal($config), |
||
| 46 | '_service_url' => 'v3/pay/partner/transactions/id/'.$transactionId.'?'.$this->service($payload, $config), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 47 | ]); |
||
| 48 | |||
| 49 | Logger::info('[Wechat][V3][Pay][App][QueryByWxPlugin] 插件装载完毕', ['rocket' => $rocket]); |
||
| 50 | |||
| 51 | return $next($rocket); |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function normal(array $config): string |
||
| 55 | { |
||
| 56 | return http_build_query([ |
||
| 57 | 'mchid' => $config['mch_id'] ?? 'null', |
||
| 58 | ]); |
||
| 59 | } |
||
| 60 | |||
| 61 | protected function service(Collection $payload, array $config): string |
||
| 62 | { |
||
| 63 | return http_build_query([ |
||
| 64 | 'sp_mchid' => $config['mch_id'] ?? 'null', |
||
| 65 | 'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? 'null'), |
||
| 66 | ]); |
||
| 67 | } |
||
| 68 | } |
||
| 69 |