Merge pull request #402 from zhmuyu/master

扩展操作允许调用者处理接口响应
This commit is contained in:
yansongda 2020-11-23 22:40:17 +08:00 committed by GitHub
commit a51e81990f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -334,7 +334,7 @@ class Alipay implements GatewayApplicationInterface
* @throws InvalidSignException * @throws InvalidSignException
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function extend(string $method, callable $function, bool $now = true): ?Collection public function extend(string $method, callable $function, bool $now = true, bool $response = false): ?Collection
{ {
if (!$now && !method_exists($this, $method)) { if (!$now && !method_exists($this, $method)) {
$this->extends[$method] = $function; $this->extends[$method] = $function;
@ -354,7 +354,7 @@ class Alipay implements GatewayApplicationInterface
$this->payload = $customize; $this->payload = $customize;
$this->payload['sign'] = Support::generateSign($this->payload); $this->payload['sign'] = Support::generateSign($this->payload);
return Support::requestApi($this->payload); return Support::requestApi($this->payload, $response);
} }
return $customize; return $customize;

View File

@ -132,7 +132,7 @@ class Support
* @throws InvalidConfigException * @throws InvalidConfigException
* @throws InvalidSignException * @throws InvalidSignException
*/ */
public static function requestApi(array $data): Collection public static function requestApi(array $data, bool $response = false): Collection
{ {
Events::dispatch(new Events\ApiRequesting('Alipay', '', self::$instance->getBaseUri(), $data)); Events::dispatch(new Events\ApiRequesting('Alipay', '', self::$instance->getBaseUri(), $data));
@ -144,7 +144,7 @@ class Support
Events::dispatch(new Events\ApiRequested('Alipay', '', self::$instance->getBaseUri(), $result)); Events::dispatch(new Events\ApiRequested('Alipay', '', self::$instance->getBaseUri(), $result));
return self::processingApiResult($data, $result); return self::processingApiResult($data, $result, $response);
} }
/** /**
@ -396,8 +396,12 @@ class Support
* @throws InvalidConfigException * @throws InvalidConfigException
* @throws InvalidSignException * @throws InvalidSignException
*/ */
protected static function processingApiResult($data, $result): Collection protected static function processingApiResult($data, $result, $response = false): Collection
{ {
if ($response) {
return new Collection($result);
}
$method = str_replace('.', '_', $data['method']).'_response'; $method = str_replace('.', '_', $data['method']).'_response';
if (!isset($result['sign']) || '10000' != $result[$method]['code']) { if (!isset($result['sign']) || '10000' != $result[$method]['code']) {