fix: 支持分账传递姓名 (#559)

This commit is contained in:
yansongda 2022-01-21 12:21:06 +08:00 committed by GitHub
parent 5fd54d3808
commit 4388309db8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 28 deletions

View File

@ -1,3 +1,9 @@
## v3.0.25
### added
- fix: 支持分账传递姓名 (#559)
## v3.0.24
### added

View File

@ -7,32 +7,71 @@ namespace Yansongda\Pay\Plugin\Wechat\Fund\Profitsharing;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Traits\HasWechatEncryption;
use Yansongda\Supports\Collection;
class AddReceiverPlugin extends GeneralPlugin
{
use HasWechatEncryption;
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\InvalidConfigException
* @throws \Yansongda\Pay\Exception\InvalidParamsException
* @throws \Yansongda\Pay\Exception\InvalidResponseException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
protected function doSomething(Rocket $rocket): void
{
$params = $rocket->getParams();
$config = get_wechat_config($rocket->getParams());
$extra = $this->getWechatId($config, $rocket->getPayload());
$wechatId = [
'appid' => $config->get('mp_app_id'),
];
if (!empty($params['receivers'][0]['name'] ?? '')) {
$params = $this->loadSerialNo($params);
if (Pay::MODE_SERVICE == $config->get('mode')) {
$wechatId['sub_mchid'] = $rocket->getPayload()
->get('sub_mchid', $config->get('sub_mch_id', ''));
$rocket->setParams($params);
$extra['receivers'] = $this->getEncryptUserName($params);
}
$rocket->mergePayload($wechatId);
$rocket->mergePayload($extra);
}
protected function getUri(Rocket $rocket): string
{
return 'v3/profitsharing/receivers/add';
}
protected function getWechatId(Collection $config, Collection $payload): array
{
$wechatId = [
'appid' => $config->get('mp_app_id'),
];
if (Pay::MODE_SERVICE == $config->get('mode')) {
$wechatId['sub_mchid'] = $payload->get('sub_mchid', $config->get('sub_mch_id', ''));
}
return $wechatId;
}
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\InvalidParamsException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
protected function getEncryptUserName(array $params): array
{
$lists = $params['receivers'] ?? [];
$publicKey = $this->getPublicKey($params, $params['_serial_no'] ?? '');
foreach ($lists as $key => $list) {
$lists[$key]['name'] = encrypt_wechat_contents($list['name'], $publicKey);
}
return $lists;
}
}

View File

@ -12,14 +12,24 @@ use Yansongda\Supports\Collection;
class AddReceiverPluginTest extends TestCase
{
/**
* @var AddReceiverPlugin
*/
protected $plugin;
protected function setUp(): void
{
parent::setUp();
$this->plugin = new AddReceiverPlugin();
}
public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$plugin = new AddReceiverPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
@ -34,9 +44,7 @@ class AddReceiverPluginTest extends TestCase
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection());
$plugin = new AddReceiverPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
@ -51,13 +59,31 @@ class AddReceiverPluginTest extends TestCase
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_mchid' => '123']));
$plugin = new AddReceiverPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('wx55955316af4ef13', $payload->get('appid'));
self::assertEquals('123', $payload->get('sub_mchid'));
}
public function testEncryptName()
{
$params = [
'receivers' => [
[
'name' => 'yansongda'
]
]
];
$rocket = new Rocket();
$rocket->setParams($params)->setPayload(new Collection());
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertNotEquals('yansongda', $payload->get('receivers.0.name'));
self::assertStringContainsString('==', $payload->get('receivers.0.name'));
}
}

View File

@ -12,14 +12,24 @@ use Yansongda\Supports\Collection;
class CreatePluginTest extends TestCase
{
/**
* @var CreatePlugin
*/
protected $plugin;
protected function setUp(): void
{
parent::setUp();
$this->plugin = new CreatePlugin();
}
public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$plugin = new CreatePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
@ -34,9 +44,7 @@ class CreatePluginTest extends TestCase
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection());
$plugin = new CreatePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
@ -51,9 +59,7 @@ class CreatePluginTest extends TestCase
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_mchid' => '123']));
$plugin = new CreatePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
@ -74,9 +80,7 @@ class CreatePluginTest extends TestCase
$rocket = new Rocket();
$rocket->setParams($params)->setPayload(new Collection());
$plugin = new CreatePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertNotEquals('yansongda', $payload->get('receivers.0.name'));