fix: 微信内网页支付供应商模式 sub_appid 非必填 (#628)

* fixed: 公众号支付供应商模式 sub_appid 非必填
* refactor: 优化代码

Co-authored-by: BranchZero Sun <branchzero@elemenx.com>
Co-authored-by: yansongda <me@yansongda.cn>
This commit is contained in:
BranchZero Sun 2022-07-17 21:54:48 +08:00 committed by GitHub
parent cc8ad8cafa
commit c51793e88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 66 deletions

View File

@ -1,3 +1,9 @@
## v3.1.7
### fixed
- fix: 微信内网页支付供应商模式 sub_appid 非必填(#628)
## v3.1.6
### fixed

View File

@ -4,9 +4,7 @@ declare(strict_types=1);
namespace Yansongda\Pay\Plugin\Wechat\Pay\App;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Config;
class PrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\PrepayPlugin
{
@ -20,22 +18,8 @@ class PrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\PrepayPlugin
return 'v3/pay/partner/transactions/app';
}
protected function getWechatId(Config $config, Rocket $rocket): array
protected function getConfigKey(array $params): string
{
$payload = $rocket->getPayload();
if (Pay::MODE_SERVICE == $config->get('mode')) {
return [
'sp_appid' => $config->get('app_id', ''),
'sp_mchid' => $config->get('mch_id', ''),
'sub_appid' => $payload->get('sub_appid', $config->get('sub_app_id')),
'sub_mchid' => $payload->get('sub_mchid', $config->get('sub_mch_id')),
];
}
return [
'appid' => $config->get('app_id', ''),
'mchid' => $config->get('mch_id', ''),
];
return 'app_id';
}
}

View File

@ -41,19 +41,31 @@ class PrepayPlugin extends GeneralPlugin
protected function getWechatId(Config $config, Rocket $rocket): array
{
$payload = $rocket->getPayload();
$configKey = $this->getConfigKey($rocket->getParams());
if (Pay::MODE_SERVICE == $config->get('mode')) {
return [
'sp_appid' => $config->get('mp_app_id', ''),
'sp_mchid' => $config->get('mch_id', ''),
'sub_appid' => $payload->get('sub_appid', $config->get('sub_mp_app_id')),
'sub_mchid' => $payload->get('sub_mchid', $config->get('sub_mch_id')),
];
}
return [
'appid' => $config->get('mp_app_id', ''),
$result = [
'appid' => $config->get($configKey, ''),
'mchid' => $config->get('mch_id', ''),
];
if (Pay::MODE_SERVICE == $config->get('mode')) {
$result = [
'sp_appid' => $config->get($configKey, ''),
'sp_mchid' => $config->get('mch_id', ''),
'sub_mchid' => $payload->get('sub_mchid', $config->get('sub_mch_id')),
];
$subAppId = $payload->get('sub_appid', $config->get('sub_'.$configKey));
if (!empty($subAppId)) {
$result['sub_appid'] = $subAppId;
}
}
return $result;
}
protected function getConfigKey(array $params): string
{
return 'mp_app_id';
}
}

View File

@ -4,9 +4,7 @@ declare(strict_types=1);
namespace Yansongda\Pay\Plugin\Wechat\Pay\H5;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Config;
class PrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\PrepayPlugin
{
@ -20,27 +18,13 @@ class PrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\PrepayPlugin
return 'v3/pay/partner/transactions/h5';
}
protected function getWechatId(Config $config, Rocket $rocket): array
protected function getConfigKey(array $params): string
{
$payload = $rocket->getPayload();
$key = ($rocket->getParams()['_type'] ?? 'mp').'_app_id';
$key = ($params['_type'] ?? 'mp').'_app_id';
if ('app_app_id' === $key) {
$key = 'app_id';
}
if (Pay::MODE_SERVICE == $config->get('mode')) {
return [
'sp_appid' => $config->get($key, ''),
'sp_mchid' => $config->get('mch_id', ''),
'sub_appid' => $payload->get('sub_appid', $config->get('sub_'.$key)),
'sub_mchid' => $payload->get('sub_mchid', $config->get('sub_mch_id')),
];
}
return [
'appid' => $config->get($key, ''),
'mchid' => $config->get('mch_id', ''),
];
return $key;
}
}

View File

@ -12,26 +12,17 @@ class PrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\PrepayPlugin
{
protected function getWechatId(Config $config, Rocket $rocket): array
{
$payload = $rocket->getPayload();
$wechatId = parent::getWechatId($config, $rocket);
$result = [
'appid' => $config->get('mini_app_id', ''),
'mchid' => $config->get('mch_id', ''),
];
if (Pay::MODE_SERVICE == $config->get('mode')) {
$result = [
'sp_appid' => $config->get('mini_app_id', $config->get('mp_app_id', '')),
'sp_mchid' => $config->get('mch_id', ''),
'sub_mchid' => $payload->get('sub_mchid', $config->get('sub_mch_id')),
];
$subAppId = $payload->get('sub_appid', $config->get('sub_mini_app_id'));
if (!empty($subAppId)) {
$result['sub_appid'] = $subAppId;
}
if (Pay::MODE_SERVICE == $config->get('mode') && empty($wechatId['sp_appid'])) {
$wechatId['sp_appid'] = $config->get('mp_app_id', '');
}
return $result;
return $wechatId;
}
protected function getConfigKey(array $params): string
{
return 'mini_app_id';
}
}

View File

@ -81,4 +81,17 @@ class PrepayPluginTest extends TestCase
self::assertEquals('123', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
public function testPartnerDirectPayloadWithoutSubAppId()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider4'])->setPayload(new Collection());
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertArrayNotHasKey('sub_appid', $payload->all());
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
}

View File

@ -82,6 +82,22 @@ class TestCase extends \PHPUnit\Framework\TestCase
'sub_mch_id' => '1600314072',
'mode' => Pay::MODE_SERVICE,
],
'service_provider4' => [
'mp_app_id' => 'wx55955316af4ef13',
'mch_id' => '1600314069',
'mini_app_id' => 'wx55955316af4ef14',
'mch_secret_key' => '53D67FCB97E68F9998CBD17ED7A8D1E2',
'mch_secret_cert' => __DIR__.'/Cert/wechatAppPrivateKey.pem',
'mch_public_cert_path' => __DIR__.'/Cert/wechatAppPublicKey.pem',
'wechat_public_cert_path' => [
'45F59D4DABF31918AFCEC556D5D2C6E376675D57' => __DIR__.'/Cert/wechatPublicKey.crt',
],
'sub_mp_app_id' => '',
'sub_app_id' => 'wx55955316af4ef16',
'sub_mini_app_id' => 'wx55955316af4ef17',
'sub_mch_id' => '1600314070',
'mode' => Pay::MODE_SERVICE,
],
'empty_wechat_public_cert' => [
'app_id' => 'yansongda',
'mp_app_id' => 'wx55955316af4ef13',