diff --git a/CHANGELOG.md b/CHANGELOG.md index bf261789d..aeb407fa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v3.2.3 + +### added + +- feat: 微信 Native 支付支持关联其它类型 appid(#680) + ## v3.2.2 ### refactor diff --git a/src/Plugin/Wechat/Pay/Common/PrepayPlugin.php b/src/Plugin/Wechat/Pay/Common/PrepayPlugin.php index 524ce82d7..732f860b0 100644 --- a/src/Plugin/Wechat/Pay/Common/PrepayPlugin.php +++ b/src/Plugin/Wechat/Pay/Common/PrepayPlugin.php @@ -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; } } diff --git a/src/Plugin/Wechat/Pay/H5/PrepayPlugin.php b/src/Plugin/Wechat/Pay/H5/PrepayPlugin.php index c53a7e9a8..b45f93322 100644 --- a/src/Plugin/Wechat/Pay/H5/PrepayPlugin.php +++ b/src/Plugin/Wechat/Pay/H5/PrepayPlugin.php @@ -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; - } } diff --git a/tests/Plugin/Wechat/Pay/App/PrepayPluginTest.php b/tests/Plugin/Wechat/Pay/App/PrepayPluginTest.php index b1632bbeb..796f415fd 100644 --- a/tests/Plugin/Wechat/Pay/App/PrepayPluginTest.php +++ b/tests/Plugin/Wechat/Pay/App/PrepayPluginTest.php @@ -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(); diff --git a/tests/Plugin/Wechat/Pay/Common/PrepayPluginTest.php b/tests/Plugin/Wechat/Pay/Common/PrepayPluginTest.php index cd2797266..b2f5cc742 100644 --- a/tests/Plugin/Wechat/Pay/Common/PrepayPluginTest.php +++ b/tests/Plugin/Wechat/Pay/Common/PrepayPluginTest.php @@ -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(); diff --git a/tests/Plugin/Wechat/Pay/Mini/PrepayPluginTest.php b/tests/Plugin/Wechat/Pay/Mini/PrepayPluginTest.php index 774411a7f..a42de5894 100644 --- a/tests/Plugin/Wechat/Pay/Mini/PrepayPluginTest.php +++ b/tests/Plugin/Wechat/Pay/Mini/PrepayPluginTest.php @@ -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(); diff --git a/tests/Plugin/Wechat/Pay/Native/PrepayPluginTest.php b/tests/Plugin/Wechat/Pay/Native/PrepayPluginTest.php new file mode 100644 index 000000000..d0a359ce2 --- /dev/null +++ b/tests/Plugin/Wechat/Pay/Native/PrepayPluginTest.php @@ -0,0 +1,145 @@ +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')); + } +}