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

feat: 微信 扫码支付 支持关联 mini_app_id #680

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.2.3

### added

- feat: 微信 Native 支付支持关联其它类型 appid(#680)

## v3.2.2

### refactor
Expand Down
7 changes: 6 additions & 1 deletion src/Plugin/Wechat/Pay/Common/PrepayPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ protected function getWechatId(array $config, Rocket $rocket): array

protected function getConfigKey(array $params): string
{
return 'mp_app_id';
$key = ($params['_type'] ?? 'mp').'_app_id';
if ('app_app_id' === $key) {
$key = 'app_id';
}

return $key;
}
}
10 changes: 0 additions & 10 deletions src/Plugin/Wechat/Pay/H5/PrepayPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,4 @@ protected function getPartnerUri(Rocket $rocket): string
{
return 'v3/pay/partner/transactions/h5';
}

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

return $key;
}
}
13 changes: 13 additions & 0 deletions tests/Plugin/Wechat/Pay/App/PrepayPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public function testWechatIdNormal()
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testWechatIdNormalWithType()
{
$rocket = new Rocket();
$rocket->setParams(['_type' => 'mini'])->setPayload(new Collection());

$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });

$payload = $result->getPayload();

self::assertEquals('yansongda', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testWechatIdPartner()
{
$rocket = new Rocket();
Expand Down
21 changes: 21 additions & 0 deletions tests/Plugin/Wechat/Pay/Common/PrepayPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ public function testNormal()
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testNormalType()
{
$rocket = new Rocket();
$rocket->setParams(['_type' => 'mini'])->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_NORMAL].'v3/pay/transactions/jsapi'), $radar->getUri());
self::assertArrayNotHasKey('sp_appid', $payload->all());
self::assertArrayNotHasKey('sp_mchid', $payload->all());
self::assertArrayNotHasKey('sub_appid', $payload->all());
self::assertArrayNotHasKey('sub_mchid', $payload->all());
self::assertEquals('wx55955316af4ef14', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testPartner()
{
$rocket = new Rocket();
Expand Down
13 changes: 13 additions & 0 deletions tests/Plugin/Wechat/Pay/Mini/PrepayPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public function testWechatIdNormal()
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testWechatIdNormalWithType()
{
$rocket = new Rocket();
$rocket->setParams(['_type' => 'app'])->setPayload(new Collection());

$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });

$payload = $result->getPayload();

self::assertEquals('wx55955316af4ef14', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testWechatIdPartner()
{
$rocket = new Rocket();
Expand Down
145 changes: 145 additions & 0 deletions tests/Plugin/Wechat/Pay/Native/PrepayPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Native;

use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\Pay\Native\PrepayPlugin;
use Yansongda\Pay\Provider\Wechat;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;

class PrepayPluginTest extends TestCase
{
/**
* @var \Yansongda\Pay\Plugin\Wechat\Pay\Native\PrepayPlugin
*/
protected $plugin;

protected function setUp(): void
{
parent::setUp();

$this->plugin = new PrepayPlugin();
}

public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->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_NORMAL].'v3/pay/transactions/native'), $radar->getUri());
self::assertArrayNotHasKey('sp_appid', $payload->all());
self::assertArrayNotHasKey('sp_mchid', $payload->all());
self::assertArrayNotHasKey('sub_appid', $payload->all());
self::assertArrayNotHasKey('sub_mchid', $payload->all());
self::assertEquals('wx55955316af4ef13', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testNormalType()
{
$rocket = new Rocket();
$rocket->setParams(['_type' => 'mini'])->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_NORMAL].'v3/pay/transactions/native'), $radar->getUri());
self::assertArrayNotHasKey('sp_appid', $payload->all());
self::assertArrayNotHasKey('sp_mchid', $payload->all());
self::assertArrayNotHasKey('sub_appid', $payload->all());
self::assertArrayNotHasKey('sub_mchid', $payload->all());
self::assertEquals('wx55955316af4ef14', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}

public function testNormalTypeApp()
{
$rocket = new Rocket();
$rocket->setParams(['_type' => 'app'])->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_NORMAL].'v3/pay/transactions/native'), $radar->getUri());
self::assertArrayNotHasKey('sp_appid', $payload->all());
self::assertArrayNotHasKey('sp_mchid', $payload->all());
self::assertArrayNotHasKey('sub_appid', $payload->all());
self::assertArrayNotHasKey('sub_mchid', $payload->all());
self::assertEquals('yansongda', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}

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/pay/partner/transactions/native'), $radar->getUri());
self::assertArrayNotHasKey('appid', $payload->all());
self::assertArrayNotHasKey('mchid', $payload->all());
self::assertEquals('wx55955316af4ef13', $payload->get('sp_appid'));
self::assertEquals('1600314069', $payload->get('sp_mchid'));
self::assertEquals('wx55955316af4ef15', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}

public function testPartnerType()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider', '_type' => 'mini'])->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/pay/partner/transactions/native'), $radar->getUri());
self::assertArrayNotHasKey('appid', $payload->all());
self::assertArrayNotHasKey('mchid', $payload->all());
self::assertEquals('wx55955316af4ef14', $payload->get('sp_appid'));
self::assertEquals('1600314069', $payload->get('sp_mchid'));
self::assertEquals('wx55955316af4ef17', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}

public function testPartnerDirectPayload()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_appid' => '123']));

$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });

$payload = $result->getPayload();

self::assertEquals('123', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
}