This commit is contained in:
yansongda 2021-07-17 18:21:42 +08:00
parent da6ae3956a
commit 9ada0f049b
5 changed files with 86 additions and 17 deletions

View File

@ -30,8 +30,8 @@ jobs:
matrix:
os: [ ubuntu-latest ]
php-version:
- 7.3
- 7.4
- 8.0
steps:
- name: Checkout Code
uses: actions/checkout@v2

View File

@ -5,9 +5,33 @@ declare(strict_types=1);
namespace Yansongda\Pay\Plugin\Wechat\Pay\App;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Config;
use Yansongda\Supports\Str;
class InvokePrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\InvokePrepayPlugin
{
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
* @throws \Exception
*/
protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
{
$config = new Config([
'appid' => $this->getAppid($rocket),
'partnerid' => get_wechat_config($rocket->getParams())->get('mch_id'),
'prepayid' => $prepayId,
'package' => 'Sign=WXPay',
'noncestr' => Str::random(32),
'timestamp' => time().'',
]);
$config->set('sign', $this->getSign($config, $rocket->getParams()));
return $config;
}
protected function getAppid(Rocket $rocket): string
{
$config = get_wechat_config($rocket->getParams());

View File

@ -12,6 +12,7 @@ use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Logger;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Config;
use Yansongda\Supports\Str;
class InvokePrepayPlugin implements PluginInterface
@ -31,20 +32,13 @@ class InvokePrepayPlugin implements PluginInterface
Logger::info('[wechat][InvokePrepayPlugin] 插件开始装载', ['rocket' => $rocket]);
$prepayId = $rocket->getDestination()->get('prepay_id');
$data = new Collection([
'appid' => $this->getAppid($rocket),
'timeStamp' => time().'',
'nonceStr' => Str::random(32),
'package' => 'prepay_id='.$prepayId,
'signType' => 'RSA',
]);
$data->set('sign', $this->getSign($data, $rocket->getParams()));
$config = $this->getInvokeConfig($rocket, $prepayId);
if (is_null($prepayId)) {
throw new InvalidResponseException(InvalidResponseException::RESPONSE_MISSING_NECESSARY_PARAMS);
}
$rocket->setDestination($this->transToResponse($data));
$rocket->setDestination($this->transToResponse($config));
Logger::info('[wechat][InvokePrepayPlugin] 插件装载完毕', ['rocket' => $rocket]);
@ -57,17 +51,17 @@ class InvokePrepayPlugin implements PluginInterface
* @throws \Yansongda\Pay\Exception\InvalidConfigException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
protected function getSign(Collection $destination, array $params): string
protected function getSign(Collection $invokeConfig, array $params): string
{
$contents = $destination->get('appid', '')."\n".
$destination->get('timeStamp', '')."\n".
$destination->get('nonceStr', '')."\n".
$destination->get('package', '')."\n";
$contents = $invokeConfig->get('appid', $invokeConfig->get('appId'))."\n".
$invokeConfig->get('timeStamp', '')."\n".
$invokeConfig->get('nonceStr', '')."\n".
$invokeConfig->get('package', '')."\n";
return get_wechat_sign($params, $contents);
}
protected function transToResponse(Collection $data): ResponseInterface
protected function transToResponse(Config $data): ResponseInterface
{
return new Response(
200,
@ -76,6 +70,27 @@ class InvokePrepayPlugin implements PluginInterface
);
}
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
* @throws \Exception
*/
protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
{
$config = new Config([
'appId' => $this->getAppid($rocket),
'timeStamp' => time().'',
'nonceStr' => Str::random(32),
'package' => 'prepay_id='.$prepayId,
'signType' => 'RSA',
]);
$config->set('paySign', $this->getSign($config, $rocket->getParams()));
return $config;
}
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException

View File

@ -0,0 +1,26 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\App;
use Yansongda\Pay\Plugin\Wechat\Pay\App\InvokePrepayPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class InvokePrepayPluginTest extends TestCase
{
public function testNormal()
{
$rocket = (new Rocket())->setDestination(new Collection(['prepay_id' => 'yansongda']));
$result = (new InvokePrepayPlugin())->assembly($rocket, function ($rocket) { return $rocket; });
$contents = $result->getDestination()->getBody()->getContents();
self::assertStringContainsString('appid', $contents);
self::assertStringContainsString('partnerid', $contents);
self::assertStringContainsString('package', $contents);
self::assertStringContainsString('Sign=WXPay', $contents);
self::assertStringContainsString('sign', $contents);
}
}

View File

@ -15,6 +15,10 @@ class InvokePrepayPluginTest extends TestCase
$result = (new InvokePrepayPlugin())->assembly($rocket, function ($rocket) { return $rocket; });
self::assertStringContainsString('package', $result->getDestination()->getBody()->getContents());
$contents = $result->getDestination()->getBody()->getContents();
self::assertStringContainsString('appId', $contents);
self::assertStringContainsString('package', $contents);
self::assertStringContainsString('paySign', $contents);
}
}