Skip to content

Commit

Permalink
优化代码...
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Jan 5, 2018
1 parent f911e31 commit 38a3d94
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"php": ">=7.0",
"ext-openssl": "*",
"ext-simplexml":"*",
"yansongda/supports": "^1.1",
"yansongda/supports": "^1.4",
"monolog/monolog": "^1.23",
"symfony/http-foundation": "^3.0|^4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Gateways/Alipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function verify(): Collection

$data = $request->request->count() > 0 ? $request->request->all() : $request->query->all();

$data = Support::encoding($data);
$data = Support::encoding($data, 'utf-8', $data['charset'] ?? 'gb2312');

Log::debug('Receive Alipay Request:', $data);

Expand Down
58 changes: 27 additions & 31 deletions src/Gateways/Alipay/Support.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,29 @@ public static function getInstance()
*
* @return Collection
*/
public static function requestApi($data, $publicKey): Collection
public static function requestApi(array $data, $publicKey): Collection
{
Log::debug('Request To Alipay Api', [self::baseUri(), $data]);
Log::debug('Request To Alipay Api', [self::getInstance()->baseUri(), $data]);

$method = str_replace('.', '_', $data['method']).'_response';

$data = self::encoding(self::getInstance()->post('', $data), 'utf-8', 'gb2312');
$data = json_decode($data, true);
$result = self::encoding(self::getInstance()->post('', $data), 'utf-8', 'gb2312');
$result = json_decode($result, true);

if (!self::verifySign($data[$method], $publicKey, true, $data['sign'])) {
Log::warning('Alipay Sign Verify FAILED', $data);
if (!self::verifySign($result[$method], $publicKey, true, $result['sign'])) {
Log::warning('Alipay Sign Verify FAILED', $result);

throw new InvalidSignException('Alipay Sign Verify FAILED', 3, $data);
throw new InvalidSignException('Alipay Sign Verify FAILED', 3, $result);
}

if (isset($data[$method]['code']) && $data[$method]['code'] == '10000') {
return new Collection($data[$method]);
if (isset($result[$method]['code']) && $result[$method]['code'] == '10000') {
return new Collection($result[$method]);
}

throw new GatewayException(
'Get Alipay API Error:'.$data[$method]['msg'].' - '.$data[$method]['sub_code'],
$data[$method]['code'],
$data
'Get Alipay API Error:'.$result[$method]['msg'].' - '.$result[$method]['sub_code'],
$result[$method]['code'],
$result
);
}

Expand All @@ -86,9 +86,12 @@ public static function requestApi($data, $publicKey): Collection
*
* @author yansongda <[email protected]>
*
* @param array $parmas
* @param string $privateKey
*
* @return string
*/
public static function generateSign($parmas, $privateKey = null): string
public static function generateSign(array $parmas, $privateKey = null): string
{
if (is_null($privateKey)) {
throw new InvalidConfigException('Missing Alipay Config -- [private_key]', 1);
Expand Down Expand Up @@ -119,7 +122,7 @@ public static function generateSign($parmas, $privateKey = null): string
*
* @return bool
*/
public static function verifySign($data, $publicKey = null, $sync = false, $sign = null): bool
public static function verifySign(array $data, $publicKey = null, $sync = false, $sign = null): bool
{
if (is_null($publicKey)) {
throw new InvalidConfigException('Missing Alipay Config -- [ali_public_key]', 2);
Expand All @@ -136,10 +139,7 @@ public static function verifySign($data, $publicKey = null, $sync = false, $sign
$sign = $sign ?? $data['sign'];

$toVerify = $sync ? self::encoding(json_encode($data, JSON_UNESCAPED_UNICODE), 'gb2312', 'utf-8') :
self::getSignContent(
self::encoding($data, 'gb2312', 'utf-8'),
true
);
self::getSignContent($data, true);

return openssl_verify($toVerify, base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256) === 1;
}
Expand All @@ -149,17 +149,19 @@ public static function verifySign($data, $publicKey = null, $sync = false, $sign
*
* @author yansongda <[email protected]>
*
* @param array $toBeSigned
* @param array $data
* @param bool $verify
*
* @return string
*/
public static function getSignContent(array $toBeSigned, $verify = false): string
public static function getSignContent(array $data, $verify = false): string
{
ksort($toBeSigned);
$data = self::encoding($data, $data['charset'] ?? 'gb2312', 'utf-8');

ksort($data);

$stringToBeSigned = '';
foreach ($toBeSigned as $k => $v) {
foreach ($data as $k => $v) {
if ($verify && $k != 'sign' && $k != 'sign_type') {
$stringToBeSigned .= $k.'='.$v.'&';
}
Expand All @@ -181,17 +183,11 @@ public static function getSignContent(array $toBeSigned, $verify = false): strin
* @param string $to
* @param string $from
*
* @return string|array
* @return array
*/
public static function encoding($data, $to = 'utf-8', $from = 'gb2312')
public static function encoding($data, $to = 'utf-8', $from = null): array
{
if (is_string($data)) {
$data = Str::encoding($data, $to, $from);
} elseif (is_array($data) && (!isset($data['charset']) || $data['charset'] != 'utf-8')) {
$data = Arr::encoding($data, $to, $from);
}

return $data;
return Arr::encoding((array) $data, $to, $from);
}

/**
Expand Down

0 comments on commit 38a3d94

Please sign in to comment.