Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Aug 4, 2024
1 parent 9e1d494 commit 4232b64
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 75 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.7.9

### added

- feat: 新增抖音支付(#1014)

## v3.7.8

### added
Expand Down
26 changes: 0 additions & 26 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,29 +649,3 @@ function get_douyin_url(array $config, ?Collection $payload): string

return Douyin::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url;
}

/**
* @throws InvalidConfigException
* @throws InvalidSignException
*/
function verify_douyin_sign(array $config, array $contents, string $sign): void
{
if (empty($sign)) {
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证抖音签名失败-抖音签名为空', func_get_args());
}

$contents['token'] = $config['mch_secret_token'] ?? null;

if (empty($contents['token'])) {
throw new InvalidConfigException(Exception::CONFIG_DOUYIN_INVALID, '配置异常: 缺少抖音配置 -- [mch_secret_token]');
}

sort($contents, SORT_STRING);
$data = trim(implode('', $contents));

$result = $sign === sha1($data);

if (!$result) {
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证抖音签名失败', func_get_args());
}
}
29 changes: 28 additions & 1 deletion src/Plugin/Douyin/V1/Pay/CallbackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yansongda\Artful\Exception\ServiceNotFoundException;
use Yansongda\Artful\Logger;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidSignException;

use function Yansongda\Artful\filter_params;
Expand All @@ -35,7 +36,7 @@ public function assembly(Rocket $rocket, Closure $next): Rocket

$value = filter_params($params, fn ($k, $v) => '' !== $v && 'msg_signature' != $k && 'type' != $k);

verify_douyin_sign($config, $value->all(), $params['msg_signature'] ?? '');
$this->verifySign($config, $value->all(), $params['msg_signature'] ?? '');

$rocket->setPayload($params)
->setDirection(NoHttpRequestDirection::class)
Expand All @@ -45,4 +46,30 @@ public function assembly(Rocket $rocket, Closure $next): Rocket

return $next($rocket);
}

/**
* @throws InvalidConfigException
* @throws InvalidSignException
*/
protected function verifySign(array $config, array $contents, string $sign): void
{
if (empty($sign)) {
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证抖音签名失败-抖音签名为空', func_get_args());
}

$contents['token'] = $config['mch_secret_token'] ?? null;

if (empty($contents['token'])) {
throw new InvalidConfigException(Exception::CONFIG_DOUYIN_INVALID, '配置异常: 缺少抖音配置 -- [mch_secret_token]');
}

sort($contents, SORT_STRING);
$data = trim(implode('', $contents));

$result = $sign === sha1($data);

if (!$result) {
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证抖音签名失败', func_get_args());
}
}
}
46 changes: 0 additions & 46 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,50 +707,4 @@ public function testGetDouyinUrl()
self::expectExceptionCode(Exception::PARAMS_DOUYIN_URL_MISSING);
get_douyin_url([], new Collection([]));
}

public function testVerifyDouyinSign()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"840bdf067c1d6056becfe88735c8ebb7e1ab809c","nonce":"5280","timestamp":"1722769986","type":"payment"}';

$body = json_decode($post, true);

$contents = $body;
unset($contents['msg_signature'], $contents['type']);

verify_douyin_sign(get_provider_config('douyin'), $contents, $body['msg_signature']);

self::assertTrue(true);

self::expectException(InvalidSignException::class);
self::expectExceptionCode(Exception::SIGN_EMPTY);
verify_douyin_sign(get_provider_config('douyin'), [], '');
}

public function testVerifyDouyinSignError()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"840bdf067c1d6056becfe88735c8ebb7e1ab809c","nonce":"5280","timestamp":"1722769986","type":"payment"}';

$body = json_decode($post, true);

$contents = $body;
unset($contents['msg_signature'], $contents['type']);

self::expectException(InvalidSignException::class);
self::expectExceptionCode(Exception::SIGN_ERROR);
verify_douyin_sign(get_provider_config('douyin'), $contents, 'foo');
}

public function testVerifyDouyinSignConfigError()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"840bdf067c1d6056becfe88735c8ebb7e1ab809c","nonce":"5280","timestamp":"1722769986","type":"payment"}';

$body = json_decode($post, true);

$contents = $body;
unset($contents['msg_signature'], $contents['type']);

self::expectException(InvalidConfigException::class);
self::expectExceptionCode(Exception::CONFIG_DOUYIN_INVALID);
verify_douyin_sign([], $contents, 'foo');
}
}
47 changes: 46 additions & 1 deletion tests/Plugin/Douyin/V1/Pay/CallbackPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Plugin\Douyin\V1\Pay;

use Yansongda\Artful\Exception\InvalidConfigException;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidSignException;
use Yansongda\Pay\Plugin\Douyin\V1\Pay\CallbackPlugin;
use Yansongda\Pay\Tests\TestCase;

Expand All @@ -19,7 +22,7 @@ protected function setUp(): void
$this->plugin = new CallbackPlugin();
}

public function testNotifyCallbackIncludePlus()
public function testCallback()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"840bdf067c1d6056becfe88735c8ebb7e1ab809c","nonce":"5280","timestamp":"1722769986","type":"payment"}';

Expand All @@ -31,4 +34,46 @@ public function testNotifyCallbackIncludePlus()
self::assertNotEmpty($result->getPayload()->all());
self::assertNotEmpty($result->getDestination()->all());
}

public function testVerifyDouyinSignEmpty()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"","nonce":"5280","timestamp":"1722769986","type":"payment"}';

$rocket = new Rocket();
$rocket->setParams(json_decode($post, true));

self::expectException(InvalidSignException::class);
self::expectExceptionCode(Exception::SIGN_EMPTY);

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

public function testVerifyDouyinSignError()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"foo","nonce":"5280","timestamp":"1722769986","type":"payment"}';

$rocket = new Rocket();
$rocket->setParams(json_decode($post, true));

self::expectException(InvalidSignException::class);
self::expectExceptionCode(Exception::SIGN_ERROR);

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

public function testVerifyDouyinSignConfigError()
{
$post = '{"msg":"{\"appid\":\"tt226e54d3bd581bf801\",\"cp_orderno\":\"202408041111312119\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"\",\"out_channel_order_no\":\"\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"73744242495132490630\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1722769986,\"message\":\"\",\"order_id\":\"7398108028895054107\"}","msg_signature":"840bdf067c1d6056becfe88735c8ebb7e1ab809c","nonce":"5280","timestamp":"1722769986","type":"payment"}';

$params = json_decode($post, true);
$params['_config'] = 'empty_salt';

$rocket = new Rocket();
$rocket->setParams($params);

self::expectException(InvalidConfigException::class);
self::expectExceptionCode(Exception::CONFIG_DOUYIN_INVALID);

$this->plugin->assembly($rocket, function ($rocket) {return $rocket;});
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ protected function setUp(): void
'mch_id' => '73744242495132490630',
// 必填-支付 Token,用于支付回调签名
// 抖音开放平台 --> 应用详情 --> 支付信息 --> 支付设置 --> Token(令牌)
'mch_secret_token' => 'douyin_mini_token',
'mch_secret_token' => '',
// 必填-支付 SALT,用于支付签名
// 抖音开放平台 --> 应用详情 --> 支付信息 --> 支付设置 --> SALT
'mch_secret_salt' => '',
Expand Down

0 comments on commit 4232b64

Please sign in to comment.