diff --git a/src/Push.php b/src/Push.php index 64693ca..3edaea2 100644 --- a/src/Push.php +++ b/src/Push.php @@ -32,13 +32,19 @@ public function __construct($apiKey, $apiSecret, Request $request) * * @throws Exception */ - public function parse($data = null) + public function parse($postRaw = null) { - if (!$data) { - $data = json_decode($this->request->getContent(), true); + if (!$postRaw) { + $postRaw = $this->request->getContent(); + } + + parse_str($postRaw, $data); + if (!is_array($data)) { + throw new \Exception('数据不正确'); } $this->checkSign($data); + return $data; } @@ -46,7 +52,7 @@ public function checkSign(array $data) { $sign = $this->signature($data); - if ($sign != $data['sign']) { + if (!isset($data['sign']) || $sign != $data['sign']) { throw new \Exception('签名不正确'); } } diff --git a/tests/GongmallApiTest.php b/tests/GongmallApiTest.php index 8b057cd..830df38 100644 --- a/tests/GongmallApiTest.php +++ b/tests/GongmallApiTest.php @@ -89,20 +89,21 @@ public function testCompany() */ public function testPush() { - //回调示例 - $callback_str = 'appKey=58ead180d70a49048c8df010124fb9d7&bankName=%E4%B8%AD%E5%9B%BD%E5%B7%A5%E5%95%86%E9%93%B6%E8%A1%8C&extraParam=&identity=411423198309221234&mobile=18627000000&name=%E9%99%88%E4%BC%AF%E4%B9%90&nonce=f8d4a31e391f4fffabfb785d5cdc44e1&salaryAccount=6212253202006079587&sign=xxx&status=2×tamp=1550055394039&workNumber=8096'; - parse_str($callback_str, $post); - - $this->expectException(\Exception::class); - $result = $this->gongmall->push->parse($post); - //回调示例 $callback_str = 'appKey=58ead180d70a49048c8df010124fb9d7&bankName=%E4%B8%AD%E5%9B%BD%E5%B7%A5%E5%95%86%E9%93%B6%E8%A1%8C&extraParam=&identity=411423198309221234&mobile=18627000000&name=%E9%99%88%E4%BC%AF%E4%B9%90&nonce=f8d4a31e391f4fffabfb785d5cdc44e1&salaryAccount=6212253202006079587&sign=F2142A0AD77796FAC3328625AC8CCE38&status=2×tamp=1550055394039&workNumber=8096'; - parse_str($callback_str, $post); - - $result = $this->gongmall->push->parse($post); + $result = $this->gongmall->push->parse($callback_str); + // dump($result); $this->assertArrayHasKey('appKey', $result); $this->assertArrayHasKey('sign', $result); } + + public function testPushHasException() + { + //回调示例 + $callback_str = 'appKey=58ead180d70a49048c8df010124fb9d7&bankName=%E4%B8%AD%E5%9B%BD%E5%B7%A5%E5%95%86%E9%93%B6%E8%A1%8C&extraParam=&identity=411423198309221234&mobile=18627000000&name=%E9%99%88%E4%BC%AF%E4%B9%90&nonce=f8d4a31e391f4fffabfb785d5cdc44e1&salaryAccount=6212253202006079587&sign=xxx&status=2×tamp=1550055394039&workNumber=8096'; + + $this->expectException(\Exception::class); + $result = $this->gongmall->push->parse($callback_str); + } }