Skip to content

Commit

Permalink
docs: 优化注释 && 增加单测 (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Jan 1, 2024
1 parent 1121253 commit 63de184
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- change: 查询API方法由 `find` 改为 `query`,同时参数只支持 array(#897)
- change: cancel/close 的 API 参数只支持 array,不再支持 string(#900, #901)
- change: 微信合单支付去掉独立的 `combine_app_id`,`combine_mch_id` 配置,复用其它配置(#909)
- change: 手机网站支付快捷方式由 wap 改为 h5(#911, #915)
- change: 手机网站支付快捷方式由 wap 改为 h5(#911, #915, #916)
- change(internal): 按场景对 支付宝/微信/银联 插件进行分类 && 插件代码优化(#894, #909, #913)
- change(internal): 将 支付/微信/银联 shortcut 从 plugin 文件夹独立出来(#895, #904, #905)
- change(internal): shortcut 完整标明各个插件,不使用 commonPlugin(#886)
Expand Down
6 changes: 3 additions & 3 deletions src/Provider/Alipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

/**
* @method ResponseInterface|Rocket app(array $order) APP 支付
* @method Collection|Rocket pos(array $order) 刷卡支付
* @method Collection|Rocket scan(array $order) 扫码支付
* @method Collection|Rocket pos(array $order) 刷卡支付(付款码,被扫码)
* @method Collection|Rocket scan(array $order) 扫码支付(摄像头,主动扫)
* @method Collection|Rocket transfer(array $order) 帐户转账
* @method ResponseInterface|Rocket wap(array $order) 手机网站支付
* @method ResponseInterface|Rocket h5(array $order) 手机网站支付
* @method ResponseInterface|Rocket web(array $order) 电脑支付
* @method Collection|Rocket mini(array $order) 小程序支付
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Provider/Unipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
use Yansongda\Supports\Str;

/**
* @method ResponseInterface web(array $order) 电脑支付
* @method ResponseInterface|Rocket web(array $order) 电脑支付
* @method ResponseInterface|Rocket h5(array $order) H5支付
* @method Collection|Rocket pos(array $order) 刷卡支付(付款码,被扫码)
* @method Collection|Rocket scan(array $order) 扫码支付(摄像头,主动扫)
*/
class Unipay extends AbstractProvider
{
Expand Down
15 changes: 6 additions & 9 deletions src/Provider/Wechat.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@
use Yansongda\Supports\Str;

/**
* @method Collection app(array $order) APP 支付
* @method Collection mini(array $order) 小程序支付
* @method Collection mp(array $order) 公众号支付
* @method Collection scan(array $order) 扫码支付
* @method Collection wap(array $order) H5 支付
* @method Collection transfer(array $order) 帐户转账
* @method Collection papay(array $order) 支付时签约(委托代扣)
* @method Collection papayApply(array $order) 申请代扣(委托代扣)
* @method Collection papayContract(array $order) 申请代扣(委托代扣)
* @method Collection|Rocket app(array $order) APP 支付
* @method Collection|Rocket mini(array $order) 小程序支付
* @method Collection|Rocket mp(array $order) 公众号支付
* @method Collection|Rocket scan(array $order) 扫码支付(摄像头,主动扫)
* @method Collection|Rocket h5(array $order) H5 支付
* @method Collection|Rocket transfer(array $order) 帐户转账
*/
class Wechat extends AbstractProvider
{
Expand Down
178 changes: 161 additions & 17 deletions tests/Provider/AlipayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,148 @@ public function testShortcutIncompatible()
Pay::alipay()->foo();
}

public function testQueryDefault()
public function testWeb()
{
$result = Pay::alipay()->web([
'out_trade_no' => 'web'.time(),
'total_amount' => '0.01',
'subject' => 'yansongda 测试 - 01',
'_return_rocket' => true,
]);

$radar = $result->getRadar();

// 支付宝参数里有实时时间,导致签名不一样,这里只验证签名之前的部分
$body1 = 'app_id=9021000122682882&method=alipay.trade.page.pay&format=JSON&return_url=https%3A%2F%2Fpay.yansongda.cn&charset=utf-8&sign_type=RSA2&timestamp=';
$body2 = '&version=1.0&notify_url=https%3A%2F%2Fpay.yansongda.cn&app_cert_sn=e90dd23a37c5c7b616e003970817ff82&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&biz_content=%7B%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%2C%22out_trade_no';
$body3= 'total_amount%22%3A%220.01%22%2C%22subject%22%3A%22yansongda+%5Cu6d4b%5Cu8bd5+-+01%22%7D&sign=';

self::assertStringContainsString($body1, (string) $radar->getBody());
self::assertStringContainsString($body2, (string) $radar->getBody());
self::assertStringContainsString($body3, (string) $radar->getBody());
self::assertEquals('POST', $radar->getMethod());
}

public function testWebGet()
{
$result = Pay::alipay()->web([
'_method' => 'get',
'out_trade_no' => 'web'.time(),
'total_amount' => '0.01',
'subject' => 'yansongda 测试 - 01',
'_return_rocket' => true,
]);

$radar = $result->getRadar();

// 支付宝参数里有实时时间,导致签名不一样,这里只验证签名之前的部分
$body1 = 'app_id=9021000122682882&method=alipay.trade.page.pay&format=JSON&return_url=https%3A%2F%2Fpay.yansongda.cn&charset=utf-8&sign_type=RSA2&timestamp=';
$body2 = '&version=1.0&notify_url=https%3A%2F%2Fpay.yansongda.cn&app_cert_sn=e90dd23a37c5c7b616e003970817ff82&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&biz_content=%7B%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%2C%22out_trade_no';
$body3= 'total_amount%22%3A%220.01%22%2C%22subject%22%3A%22yansongda+%5Cu6d4b%5Cu8bd5+-+01%22%7D&sign=';

self::assertStringContainsString($body1, (string) $radar->getBody());
self::assertStringContainsString($body2, (string) $radar->getBody());
self::assertStringContainsString($body3, (string) $radar->getBody());
self::assertEquals('GET', $radar->getMethod());
}

public function testH5()
{
$result = Pay::alipay()->h5([
'out_trade_no' => 'web'.time(),
'total_amount' => '0.01',
'subject' => 'yansongda 测试 - 01',
'quit_url' => 'https://yansongda.cn',
'_return_rocket' => true,
]);

$radar = $result->getRadar();

// 支付宝参数里有实时时间,导致签名不一样,这里只验证签名之前的部分
$body1 = 'app_id=9021000122682882&method=alipay.trade.wap.pay&format=JSON&return_url=https%3A%2F%2Fpay.yansongda.cn&charset=utf-8&sign_type=RSA2&timestamp=';
$body2 = '&version=1.0&notify_url=https%3A%2F%2Fpay.yansongda.cn&app_cert_sn=e90dd23a37c5c7b616e003970817ff82&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&biz_content=%7B%22out_trade_no%22%3A%22web';
$body3= 'total_amount%22%3A%220.01%22%2C%22subject%22%3A%22yansongda+%5Cu6d4b%5Cu8bd5+-+01%22%2C%22quit_url%22%3A%22https%3A%5C%2F%5C%2Fyansongda.cn%22%7D&sign=';

self::assertStringContainsString($body1, (string) $radar->getBody());
self::assertStringContainsString($body2, (string) $radar->getBody());
self::assertStringContainsString($body3, (string) $radar->getBody());
self::assertEquals('POST', $radar->getMethod());
}

public function testH5Get()
{
$result = Pay::alipay()->h5([
'_method' => 'get',
'out_trade_no' => 'web'.time(),
'total_amount' => '0.01',
'subject' => 'yansongda 测试 - 01',
'quit_url' => 'https://yansongda.cn',
'_return_rocket' => true,
]);

$radar = $result->getRadar();

// 支付宝参数里有实时时间,导致签名不一样,这里只验证签名之前的部分
$body1 = 'app_id=9021000122682882&method=alipay.trade.wap.pay&format=JSON&return_url=https%3A%2F%2Fpay.yansongda.cn&charset=utf-8&sign_type=RSA2&timestamp=';
$body2 = '&version=1.0&notify_url=https%3A%2F%2Fpay.yansongda.cn&app_cert_sn=e90dd23a37c5c7b616e003970817ff82&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&biz_content=%7B%22out_trade_no%22%3A%22web';
$body3= 'total_amount%22%3A%220.01%22%2C%22subject%22%3A%22yansongda+%5Cu6d4b%5Cu8bd5+-+01%22%2C%22quit_url%22%3A%22https%3A%5C%2F%5C%2Fyansongda.cn%22%7D&sign=';

self::assertStringContainsString($body1, (string) $radar->getBody());
self::assertStringContainsString($body2, (string) $radar->getBody());
self::assertStringContainsString($body3, (string) $radar->getBody());
self::assertEquals('GET', $radar->getMethod());
}

public function testApp()
{
$result = Pay::alipay()->app([
'out_trade_no' => 'web'.time(),
'total_amount' => '0.01',
'subject' => 'yansongda 测试 - 01',
]);

// 支付宝参数里有实时时间,导致签名不一样,这里只验证签名之前的部分
$body1 = 'app_id=9021000122682882&method=alipay.trade.app.pay&format=JSON&return_url=https%3A%2F%2Fpay.yansongda.cn&charset=utf-8&sign_type=RSA2';
$body2 = '&version=1.0&notify_url=https%3A%2F%2Fpay.yansongda.cn&app_cert_sn=e90dd23a37c5c7b616e003970817ff82&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&biz_content=%7B%22out_trade_no%22%3A%22web';
$body3= 'total_amount%22%3A%220.01%22%2C%22subject%22%3A%22yansongda+%5Cu6d4b%5Cu8bd5+-+01%22%7D&sign=';

self::assertStringContainsString($body1, (string) $result->getBody());
self::assertStringContainsString($body2, (string) $result->getBody());
self::assertStringContainsString($body3, (string) $result->getBody());
}

public function testScan()
{
$response = [
"alipay_trade_query_response" => [
"alipay_trade_precreate_response" => [
"code" => "10000",
"msg" => "Success",
"buyer_logon_id" => "ifv***@sandbox.com",
"buyer_pay_amount" => "0.00",
"buyer_user_id" => "2088722003899169",
"buyer_user_type" => "PRIVATE",
"invoice_amount" => "0.00",
"out_trade_no" => "1703141270",
"point_amount" => "0.00",
"receipt_amount" => "0.00",
"send_pay_date" => "2023-12-21 14:48:36",
"total_amount" => "0.01",
"trade_no" => "2023122122001499160501586202",
"trade_status" => "TRADE_SUCCESS",
"out_trade_no" => "1704093802",
"qr_code" => "https://qr.alipay.com/bax07651xvtprxfkmxyf00a9",
],
"sign" => "WBz5iEFVhP99SRqHAaUi6KD+6u4xUxOgLAJ989gxByd79pa9bhHfQ0EFO/78YU3TuoqNvUBbHZ7LPxP+OPQFTUtHa5JF2pz+EfgBkYOnBPW+YGz6arGqmAPBy9I+ltJxKNKq4G7ehPG0gbtQQcVqqIR9vDylitmlGIIe+YKfNbEi+vPNkQ3HXLsu3lXKGqB21XSYb/NdxneALsVOowVqgU2SSR/+5TcUzCuW5VA/LWKnpXZEDdE1HTgUFqvqrYtLoVfmXO41oKZdrR3t4/rbV64YlWR4vPSuELuC4gLdXdd63PaOmdIo/5TxI26379ZC8IfhcBiS/KO3PYm1dbgpIg==",
"sign" => "Mz0jF7DeQ6ogyle/5P6cxqbezmR9RNkuJFNIiH4bPrcHjwnaNsb3Ad5PAFc7xERDHOvfdWkNzx8++Ov+cAhfqIcKWNLyL7UFiRIeL1LoLku2K0rcAXeuxm9KKjYaEl3lr0JFQE/zUElfDcQn3s9ujNFkkMNOcPgQwbSPlEXja0a4syG76FnTknsITHONIbCCsIWnHMuf95cq8zkgjMUOlfVOL8l4Iv0n9n2r9H1OJKv+2GmDp6ntc9xAhrIlO+f5urp1N+LzO9lDx5r1HIjqlSiemNZAdQveQjRuOtx/FM4KhFvn3312Xv7XuKW9iqNnBSwXOEAuUXghXcqD8xl05g==",
];

$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn(new Response(200, [], json_encode($response)));
Pay::set(HttpClientInterface::class, $http);

$result = Pay::alipay()->query(['out_trade_no' => '1703141270']);
$result = Pay::alipay()->scan([
'out_trade_no' => time(),
'total_amount' => '0.01',
'subject' => 'yansongda 测试 - 01',
'_return_rocket' => true,
]);

self::assertEqualsCanonicalizing($response['alipay_trade_query_response'], $result->except('_sign')->all());
// 支付宝参数里有实时时间,导致签名不一样,这里只验证签名之前的部分
$body1 = 'app_id=9021000122682882&method=alipay.trade.precreate&format=JSON&return_url=https%3A%2F%2Fpay.yansongda.cn&charset=utf-8&sign_type=RSA2&timestamp=';
$body2 = '&version=1.0&notify_url=https%3A%2F%2Fpay.yansongda.cn&app_cert_sn=e90dd23a37c5c7b616e003970817ff82&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&biz_content=%7B%22out_trade_no';
$body3 = 'total_amount%22%3A%220.01%22%2C%22subject%22%3A%22yansongda+%5Cu6d4b%5Cu8bd5+-+01%22%7D&sign=';

self::assertStringContainsString($body1, (string) $result->getRadar()->getBody());
self::assertStringContainsString($body2, (string) $result->getRadar()->getBody());
self::assertStringContainsString($body3, (string) $result->getRadar()->getBody());
self::assertEqualsCanonicalizing($response['alipay_trade_precreate_response'], $result->getDestination()->except('_sign')->all());
}

public function testTransfer()
Expand Down Expand Up @@ -111,6 +224,37 @@ public function testTransfer()
self::assertEqualsCanonicalizing($response['alipay_fund_trans_uni_transfer_response'], $result->getDestination()->except('_sign')->all());
}

public function testQueryDefault()
{
$response = [
"alipay_trade_query_response" => [
"code" => "10000",
"msg" => "Success",
"buyer_logon_id" => "ifv***@sandbox.com",
"buyer_pay_amount" => "0.00",
"buyer_user_id" => "2088722003899169",
"buyer_user_type" => "PRIVATE",
"invoice_amount" => "0.00",
"out_trade_no" => "1703141270",
"point_amount" => "0.00",
"receipt_amount" => "0.00",
"send_pay_date" => "2023-12-21 14:48:36",
"total_amount" => "0.01",
"trade_no" => "2023122122001499160501586202",
"trade_status" => "TRADE_SUCCESS",
],
"sign" => "WBz5iEFVhP99SRqHAaUi6KD+6u4xUxOgLAJ989gxByd79pa9bhHfQ0EFO/78YU3TuoqNvUBbHZ7LPxP+OPQFTUtHa5JF2pz+EfgBkYOnBPW+YGz6arGqmAPBy9I+ltJxKNKq4G7ehPG0gbtQQcVqqIR9vDylitmlGIIe+YKfNbEi+vPNkQ3HXLsu3lXKGqB21XSYb/NdxneALsVOowVqgU2SSR/+5TcUzCuW5VA/LWKnpXZEDdE1HTgUFqvqrYtLoVfmXO41oKZdrR3t4/rbV64YlWR4vPSuELuC4gLdXdd63PaOmdIo/5TxI26379ZC8IfhcBiS/KO3PYm1dbgpIg==",
];

$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn(new Response(200, [], json_encode($response)));
Pay::set(HttpClientInterface::class, $http);

$result = Pay::alipay()->query(['out_trade_no' => '1703141270']);

self::assertEqualsCanonicalizing($response['alipay_trade_query_response'], $result->except('_sign')->all());
}

public function testQueryTransfer()
{
$response = [
Expand Down
4 changes: 3 additions & 1 deletion tests/Provider/UnipayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ public function testQuery()
$result = Pay::unipay()->query([
'txnTime' => '20220911041647',
'orderId' => 'pay20220911041647',
'_return_rocket' => true,
]);

self::assertArrayHasKey('origRespMsg', $result->all());
self::assertEquals('txnTime=20220911041647&orderId=pay20220911041647&version=5.1.0&encoding=utf-8&backUrl=https%3A%2F%2Fpay.yansongda.cn&currencyCode=156&accessType=0&signature=OQkKHuMITwTijM%2Fk4QLWYpFwU5LEuXWHLtsWZsuv9dCyvTx5Z4dIY5%2FdpaQgpOw6iLDDtjz6Ocdo3IYPn%2FUySoyh0W8DO31poqL1uWxEimN0mzKa4z4milWFqzFfe6hOzP1gNXVSzz8xS21QNaUsJsvk4JdbqC5eLwV4xBAgLTAvbZ5GQM4Ta5gIYC%2B1vK6Jg0XSxQQPFQ35jeKSJyqj2yyKJFnET6lWe7ZQSDCjpMjDNGtf2FvVSd9PAX5xp5DA4WXQP73U81NE7kBtxW6WbsBiaS05YO2ylg7ukh4AM%2FgzbvvU24FO%2F5keLy7MdVFB7ZEDVuuEwXNnauiosH4LhQ%3D%3D&signMethod=01&merId=777290058167151&frontUrl=https%3A%2F%2Fpay.yansongda.cn&certId=69903319369&bizType=000000&txnType=00&txnSubType=00', (string) $result->getRadar()->getBody());
self::assertArrayHasKey('origRespMsg', $result->getDestination()->all());
}

public function testCancel()
Expand Down

0 comments on commit 63de184

Please sign in to comment.