Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加只签约插件 #765

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Plugin/Wechat/Papay/OnlyContractPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Yansongda\Pay\Plugin\Wechat\Papay;

use Closure;

use function Yansongda\Pay\get_wechat_config;

use Yansongda\Pay\Parser\NoHttpRequestParser;
use Yansongda\Pay\Plugin\Wechat\RadarSignPlugin;
use Yansongda\Pay\Rocket;

/**
* 返回只签约(委托代扣)参数.
*/
class OnlyContractPlugin extends RadarSignPlugin
{
public function assembly(Rocket $rocket, Closure $next): Rocket
{
$config = get_wechat_config($rocket->getParams());

$wechatId = $this->getWechatId($config, $rocket);

if (!$rocket->getPayload()->has('notify_url')) {
$wechatId['notify_url'] = $config['notify_url'] ?? null;
}

$rocket->mergePayload($wechatId);

$rocket->mergePayload([
'sign' => $this->v2GetSign($config['mch_secret_key_v2'] ?? '', $rocket->getPayload()->all()),
]);

$rocket->setDestination($rocket->getPayload());

$rocket->setDirection(NoHttpRequestParser::class);

return $next($rocket);
}

protected function getWechatId(array $config, Rocket $rocket): array
{
$configKey = $this->getConfigKey($rocket->getParams());

$result = [
'appid' => $config[$configKey] ?? '',
'mch_id' => $config['mch_id'] ?? '',
];

return $result;
}

protected function getConfigKey(array $params): string
{
$key = ($params['_type'] ?? 'mp').'_app_id';

if ('app_app_id' === $key) {
$key = 'app_id';
}

return $key;
}
}