Skip to content

Commit

Permalink
fixed #2737
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 27, 2023
1 parent 54b1ca3 commit 134e056
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/Pay/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\HttpClient\RequestUtil;
use EasyWeChat\Kernel\ServerResponse;
use EasyWeChat\Kernel\Support\AesEcb;
use EasyWeChat\Kernel\Support\AesGcm;
use EasyWeChat\Kernel\Support\Xml;
use EasyWeChat\Kernel\Traits\InteractWithHandlers;
Expand Down Expand Up @@ -116,12 +117,49 @@ public function getRequestMessage(ServerRequestInterface $request = null): \Easy

// 微信支付的回调数据回调,偶尔是 XML https://github.com/w7corp/easywechat/issues/2737
// PS: 这帮傻逼,真的是该死啊
if (str_starts_with($originContent, '<xml')) {
$attributes = Xml::parse($originContent);
} else {
$attributes = json_decode($originContent, true);
$isXml = str_starts_with($originContent, '<xml');
$attributes = $isXml ? $this->decodeXmlMessage($originContent) : $this->decodeJsonMessage($originContent);

return new Message($attributes, $originContent);
}

/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
*/
protected function decodeXmlMessage(string $contents): array
{
$attributes = Xml::parse($contents);

if (! is_array($attributes)) {
throw new RuntimeException('Invalid request body.');
}

if (! empty($attributes['req_info'])) {
$key = $this->merchant->getV2SecretKey();

if (empty($key)) {
throw new InvalidArgumentException('V2 secret key is required.');
}

$attributes = Xml::parse(AesEcb::decrypt($attributes['req_info'], $key, iv: ''));
}

if (! is_array($attributes)) {
throw new RuntimeException('Failed to decrypt request message.');
}

return $attributes;
}

/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
*/
protected function decodeJsonMessage(string $contents): array
{
$attributes = json_decode($contents, true);

if (! is_array($attributes)) {
throw new RuntimeException('Invalid request body.');
}
Expand All @@ -144,7 +182,7 @@ public function getRequestMessage(ServerRequestInterface $request = null): \Easy
throw new RuntimeException('Failed to decrypt request message.');
}

return new Message($attributes, $originContent);
return $attributes;
}

/**
Expand Down

0 comments on commit 134e056

Please sign in to comment.