feat: 微信服务商退款及查询退款支持自动 sub_mchid 参数 (#619)

* feat: 微信服务商退款及查询退款支持自动 sub_mchid 参数

* tests: fix unit tests
This commit is contained in:
yansongda 2022-06-02 22:03:47 +08:00 committed by GitHub
parent b493c936c6
commit d56f0527d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 182 additions and 0 deletions

View File

@ -1,3 +1,21 @@
## v3.1.5
### added
- feat: 微信服务商退款及查询退款支持自动 sub_mchid 参数(#619)
## v3.1.4
### added
- feat: 支持微信投诉API (#614)
## v3.1.3
### added
- feat: 配置文件增加第三方应用授权token的支持 (#602)
## v3.1.2
### fixed

View File

@ -25,6 +25,18 @@ class FindRefundPlugin extends GeneralPlugin
return 'v3/refund/domestic/refunds/'.$payload->get('out_refund_no');
}
/**
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
protected function getPartnerUri(Rocket $rocket): string
{
$config = get_wechat_config($rocket->getParams());
$url = parent::getPartnerUri($rocket);
return $url.'?sub_mchid='.($rocket->getPayload()->get('sub_mchid', $config->get('sub_mch_id')));
}
protected function getMethod(): string
{
return 'GET';

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Yansongda\Pay\Plugin\Wechat\Pay\Common;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;
@ -14,7 +15,18 @@ class RefundPlugin extends GeneralPlugin
return 'v3/refund/domestic/refunds';
}
/**
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
protected function doSomething(Rocket $rocket): void
{
$config = get_wechat_config($rocket->getParams());
if (Pay::MODE_SERVICE == $config->get('mode')) {
$rocket->mergePayload([
'sub_mchid' => $rocket->getPayload()->get('sub_mchid', $config->get('sub_mch_id')),
]);
}
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Common;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\Pay\Common\FindRefundPlugin;
use Yansongda\Pay\Provider\Wechat;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class FindRefundPluginTest extends TestCase
{
/**
* @var \Yansongda\Pay\Plugin\Wechat\Pay\Common\FindRefundPlugin
*/
protected $plugin;
protected function setUp(): void
{
parent::setUp();
$this->plugin = new FindRefundPlugin();
}
public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection(['out_refund_no' => '123']));
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('GET', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_NORMAL].'v3/refund/domestic/refunds/123'), $radar->getUri());
self::assertNull($payload);
}
public function testPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['out_refund_no' => '123']));
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('GET', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_SERVICE].'v3/refund/domestic/refunds/123?sub_mchid=1600314070'), $radar->getUri());
self::assertNull($payload);
}
public function testPartnerDirectPayload()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_mchid' => '123','out_refund_no' => '456']));
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_SERVICE].'v3/refund/domestic/refunds/456?sub_mchid=123'), $radar->getUri());
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Common;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\Pay\Common\RefundPlugin;
use Yansongda\Pay\Provider\Wechat;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class RefundPluginTest extends TestCase
{
/**
* @var \Yansongda\Pay\Plugin\Wechat\Pay\Common\RefundPlugin
*/
protected $plugin;
protected function setUp(): void
{
parent::setUp();
$this->plugin = new RefundPlugin();
}
public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('POST', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_NORMAL].'v3/refund/domestic/refunds'), $radar->getUri());
}
public function testPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection());
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('POST', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_SERVICE].'v3/refund/domestic/refunds'), $radar->getUri());
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
public function testPartnerDirectPayload()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_mchid' => '123']));
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('123', $payload->get('sub_mchid'));
}
}