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 875440f commit 9e1d494
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 11 deletions.
8 changes: 1 addition & 7 deletions src/Provider/Douyin.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,11 @@ public function cancel(array $order): Collection|Rocket
}

/**
* @throws ContainerException
* @throws InvalidParamsException
* @throws ServiceNotFoundException
*/
public function close(array $order): Collection|Rocket
{
Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null));

$this->__call('close', [$order]);

return new Collection();
throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 抖音不支持 close API');
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,5 +720,37 @@ public function testVerifyDouyinSign()
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');
}
}
87 changes: 87 additions & 0 deletions tests/Plugin/Douyin/V1/Pay/Mini/QueryRefundPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Plugin\Douyin\V1\Pay\Mini;

use Yansongda\Artful\Exception\InvalidParamsException;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Plugin\Douyin\V1\Pay\Mini\QueryRefundPlugin;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;

class QueryRefundPluginTest extends TestCase
{
protected QueryRefundPlugin $plugin;

protected function setUp(): void
{
parent::setUp();

$this->plugin = new QueryRefundPlugin();
}

public function testEmptyPayload()
{
$rocket = new Rocket();

self::expectException(InvalidParamsException::class);
self::expectExceptionCode(Exception::PARAMS_NECESSARY_PARAMS_MISSING);
self::expectExceptionMessage('参数异常: 抖音小程序查询退款订单,参数为空');

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

public function testNormal()
{
$rocket = new Rocket();
$rocket->setPayload(new Collection( [
"out_order_no" => "yansongda",
]));

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

self::assertEquals([
"out_order_no" => "yansongda",
'_method' => 'POST',
'_url' => 'api/apps/ecpay/v1/query_refund',
'app_id' => 'tt226e54d3bd581bf801',
], $result->getPayload()->all());
}

public function testServiceParams()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection([
'out_order_no' => 'yansongda',
'thirdparty_id' => 'service_provider111',
]));

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

self::assertEquals([
'out_order_no' => 'yansongda',
'_method' => 'POST',
'_url' => 'api/apps/ecpay/v1/query_refund',
'app_id' => 'tt226e54d3bd581bf801',
'thirdparty_id' => 'service_provider111'
], $result->getPayload()->all());
}

public function testService()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection([
'out_order_no' => 'yansongda',
]));

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

self::assertEquals([
'out_order_no' => 'yansongda',
'_method' => 'POST',
'_url' => 'api/apps/ecpay/v1/query_refund',
'app_id' => 'tt226e54d3bd581bf801',
'thirdparty_id' => 'service_provider'
], $result->getPayload()->all());
}
}
38 changes: 34 additions & 4 deletions tests/Provider/DouyinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Mockery;
use Psr\Http\Message\ResponseInterface;
use Yansongda\Artful\Contract\HttpClientInterface;
use Yansongda\Artful\Exception\Exception;
use Yansongda\Artful\Exception\InvalidParamsException;
Expand Down Expand Up @@ -80,6 +82,22 @@ public function testCallMini()
self::assertEquals('771c1952ffb5e0744fc0ad1337aafa6a', $payload->get('sign'));
}

public function testClose()
{
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(\Yansongda\Pay\Exception\Exception::PARAMS_METHOD_NOT_SUPPORTED);

Pay::douyin()->close([]);
}

public function testCancel()
{
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(\Yansongda\Pay\Exception\Exception::PARAMS_METHOD_NOT_SUPPORTED);

Pay::douyin()->cancel([]);
}

public function testQuery()
{
$response = new Response(
Expand Down Expand Up @@ -163,12 +181,24 @@ public function testRefund()

public function testCallback()
{
$callback = Pay::douyin()->callback(json_decode(
'{"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"}',
true
));
$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"}';

$callback = Pay::douyin()->callback(json_decode($post, true));
self::assertInstanceOf(Collection::class, $callback);
self::assertNotEmpty($callback->all());

$request = new ServerRequest('POST', 'https://yansongda.cn/unipay/notify', [], $post);
$callback = Pay::douyin()->callback($request);

self::assertInstanceOf(Collection::class, $callback);
self::assertNotEmpty($callback->all());
}

public function testSuccess()
{
$result = Pay::douyin()->success();

self::assertInstanceOf(ResponseInterface::class, $result);
self::assertStringContainsString('success', (string) $result->getBody());
}
}

0 comments on commit 9e1d494

Please sign in to comment.