Skip to content

Commit

Permalink
Merge branch 'release/v0.4.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
bolechen committed Feb 26, 2019
2 parents d2dfa9e + 4513f88 commit f692290
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,27 @@ 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;
}

public function checkSign(array $data)
{
$sign = $this->signature($data);

if ($sign != $data['sign']) {
if (!isset($data['sign']) || $sign != $data['sign']) {
throw new \Exception('签名不正确');
}
}
Expand Down
21 changes: 11 additions & 10 deletions tests/GongmallApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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&timestamp=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&timestamp=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&timestamp=1550055394039&workNumber=8096';

$this->expectException(\Exception::class);
$result = $this->gongmall->push->parse($callback_str);
}
}

0 comments on commit f692290

Please sign in to comment.