Skip to content

Commit 4232b64

Browse files
committed
update
1 parent 9e1d494 commit 4232b64

File tree

6 files changed

+81
-75
lines changed

6 files changed

+81
-75
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v3.7.9
2+
3+
### added
4+
5+
- feat: 新增抖音支付(#1014)
6+
17
## v3.7.8
28

39
### added

src/Functions.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -649,29 +649,3 @@ function get_douyin_url(array $config, ?Collection $payload): string
649649

650650
return Douyin::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url;
651651
}
652-
653-
/**
654-
* @throws InvalidConfigException
655-
* @throws InvalidSignException
656-
*/
657-
function verify_douyin_sign(array $config, array $contents, string $sign): void
658-
{
659-
if (empty($sign)) {
660-
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证抖音签名失败-抖音签名为空', func_get_args());
661-
}
662-
663-
$contents['token'] = $config['mch_secret_token'] ?? null;
664-
665-
if (empty($contents['token'])) {
666-
throw new InvalidConfigException(Exception::CONFIG_DOUYIN_INVALID, '配置异常: 缺少抖音配置 -- [mch_secret_token]');
667-
}
668-
669-
sort($contents, SORT_STRING);
670-
$data = trim(implode('', $contents));
671-
672-
$result = $sign === sha1($data);
673-
674-
if (!$result) {
675-
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证抖音签名失败', func_get_args());
676-
}
677-
}

src/Plugin/Douyin/V1/Pay/CallbackPlugin.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Yansongda\Artful\Exception\ServiceNotFoundException;
1313
use Yansongda\Artful\Logger;
1414
use Yansongda\Artful\Rocket;
15+
use Yansongda\Pay\Exception\Exception;
1516
use Yansongda\Pay\Exception\InvalidSignException;
1617

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

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

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

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

4647
return $next($rocket);
4748
}
49+
50+
/**
51+
* @throws InvalidConfigException
52+
* @throws InvalidSignException
53+
*/
54+
protected function verifySign(array $config, array $contents, string $sign): void
55+
{
56+
if (empty($sign)) {
57+
throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证抖音签名失败-抖音签名为空', func_get_args());
58+
}
59+
60+
$contents['token'] = $config['mch_secret_token'] ?? null;
61+
62+
if (empty($contents['token'])) {
63+
throw new InvalidConfigException(Exception::CONFIG_DOUYIN_INVALID, '配置异常: 缺少抖音配置 -- [mch_secret_token]');
64+
}
65+
66+
sort($contents, SORT_STRING);
67+
$data = trim(implode('', $contents));
68+
69+
$result = $sign === sha1($data);
70+
71+
if (!$result) {
72+
throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证抖音签名失败', func_get_args());
73+
}
74+
}
4875
}

tests/FunctionTest.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -707,50 +707,4 @@ public function testGetDouyinUrl()
707707
self::expectExceptionCode(Exception::PARAMS_DOUYIN_URL_MISSING);
708708
get_douyin_url([], new Collection([]));
709709
}
710-
711-
public function testVerifyDouyinSign()
712-
{
713-
$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"}';
714-
715-
$body = json_decode($post, true);
716-
717-
$contents = $body;
718-
unset($contents['msg_signature'], $contents['type']);
719-
720-
verify_douyin_sign(get_provider_config('douyin'), $contents, $body['msg_signature']);
721-
722-
self::assertTrue(true);
723-
724-
self::expectException(InvalidSignException::class);
725-
self::expectExceptionCode(Exception::SIGN_EMPTY);
726-
verify_douyin_sign(get_provider_config('douyin'), [], '');
727-
}
728-
729-
public function testVerifyDouyinSignError()
730-
{
731-
$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"}';
732-
733-
$body = json_decode($post, true);
734-
735-
$contents = $body;
736-
unset($contents['msg_signature'], $contents['type']);
737-
738-
self::expectException(InvalidSignException::class);
739-
self::expectExceptionCode(Exception::SIGN_ERROR);
740-
verify_douyin_sign(get_provider_config('douyin'), $contents, 'foo');
741-
}
742-
743-
public function testVerifyDouyinSignConfigError()
744-
{
745-
$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"}';
746-
747-
$body = json_decode($post, true);
748-
749-
$contents = $body;
750-
unset($contents['msg_signature'], $contents['type']);
751-
752-
self::expectException(InvalidConfigException::class);
753-
self::expectExceptionCode(Exception::CONFIG_DOUYIN_INVALID);
754-
verify_douyin_sign([], $contents, 'foo');
755-
}
756710
}

tests/Plugin/Douyin/V1/Pay/CallbackPluginTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Plugin\Douyin\V1\Pay;
66

7+
use Yansongda\Artful\Exception\InvalidConfigException;
78
use Yansongda\Artful\Rocket;
9+
use Yansongda\Pay\Exception\Exception;
10+
use Yansongda\Pay\Exception\InvalidSignException;
811
use Yansongda\Pay\Plugin\Douyin\V1\Pay\CallbackPlugin;
912
use Yansongda\Pay\Tests\TestCase;
1013

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

22-
public function testNotifyCallbackIncludePlus()
25+
public function testCallback()
2326
{
2427
$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"}';
2528

@@ -31,4 +34,46 @@ public function testNotifyCallbackIncludePlus()
3134
self::assertNotEmpty($result->getPayload()->all());
3235
self::assertNotEmpty($result->getDestination()->all());
3336
}
37+
38+
public function testVerifyDouyinSignEmpty()
39+
{
40+
$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"}';
41+
42+
$rocket = new Rocket();
43+
$rocket->setParams(json_decode($post, true));
44+
45+
self::expectException(InvalidSignException::class);
46+
self::expectExceptionCode(Exception::SIGN_EMPTY);
47+
48+
$this->plugin->assembly($rocket, function ($rocket) {return $rocket;});
49+
}
50+
51+
public function testVerifyDouyinSignError()
52+
{
53+
$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"}';
54+
55+
$rocket = new Rocket();
56+
$rocket->setParams(json_decode($post, true));
57+
58+
self::expectException(InvalidSignException::class);
59+
self::expectExceptionCode(Exception::SIGN_ERROR);
60+
61+
$this->plugin->assembly($rocket, function ($rocket) {return $rocket;});
62+
}
63+
64+
public function testVerifyDouyinSignConfigError()
65+
{
66+
$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"}';
67+
68+
$params = json_decode($post, true);
69+
$params['_config'] = 'empty_salt';
70+
71+
$rocket = new Rocket();
72+
$rocket->setParams($params);
73+
74+
self::expectException(InvalidConfigException::class);
75+
self::expectExceptionCode(Exception::CONFIG_DOUYIN_INVALID);
76+
77+
$this->plugin->assembly($rocket, function ($rocket) {return $rocket;});
78+
}
3479
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ protected function setUp(): void
214214
'mch_id' => '73744242495132490630',
215215
// 必填-支付 Token,用于支付回调签名
216216
// 抖音开放平台 --> 应用详情 --> 支付信息 --> 支付设置 --> Token(令牌)
217-
'mch_secret_token' => 'douyin_mini_token',
217+
'mch_secret_token' => '',
218218
// 必填-支付 SALT,用于支付签名
219219
// 抖音开放平台 --> 应用详情 --> 支付信息 --> 支付设置 --> SALT
220220
'mch_secret_salt' => '',

0 commit comments

Comments
 (0)