-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
200 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EasyWeChat\Pay\Contracts; | ||
|
||
use Psr\Http\Message\MessageInterface; | ||
|
||
interface Validator | ||
{ | ||
/** | ||
* @throws \EasyWeChat\Pay\Exceptions\InvalidSignatureException if signature validate failed. | ||
*/ | ||
public function validate(MessageInterface $message): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace EasyWeChat\Pay\Exceptions; | ||
|
||
use EasyWeChat\Kernel\Exceptions\RuntimeException; | ||
|
||
class InvalidSignatureException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EasyWeChat\Pay; | ||
|
||
use EasyWeChat\Kernel\Exceptions\InvalidConfigException; | ||
use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; | ||
use EasyWeChat\Pay\Exceptions\InvalidSignatureException; | ||
use Psr\Http\Message\MessageInterface; | ||
|
||
class Validator implements \EasyWeChat\Pay\Contracts\Validator | ||
{ | ||
public const MAX_ALLOWED_CLOCK_OFFSET = 300; | ||
|
||
public const HEADER_TIMESTAMP = 'Wechatpay-Timestamp'; | ||
|
||
public const HEADER_NONCE = 'Wechatpay-Nonce'; | ||
|
||
public const HEADER_SERIAL = 'Wechatpay-Serial'; | ||
|
||
public const HEADER_SIGNATURE = 'Wechatpay-Signature'; | ||
|
||
public function __construct(protected MerchantInterface $merchant) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException | ||
* @throws \EasyWeChat\Pay\Exceptions\InvalidSignatureException | ||
*/ | ||
public function validate(MessageInterface $message): void | ||
{ | ||
foreach ([self::HEADER_SIGNATURE, self::HEADER_TIMESTAMP, self::HEADER_SERIAL, self::HEADER_NONCE] as $header) { | ||
if (! $message->hasHeader($header)) { | ||
throw new InvalidSignatureException("Missing Header: {$header}"); | ||
} | ||
} | ||
|
||
[$timestamp] = $message->getHeader(self::HEADER_TIMESTAMP); | ||
[$nonce] = $message->getHeader(self::HEADER_NONCE); | ||
[$serial] = $message->getHeader(self::HEADER_SERIAL); | ||
[$signature] = $message->getHeader(self::HEADER_SIGNATURE); | ||
|
||
$body = (string) $message->getBody(); | ||
|
||
$message = "{$timestamp}\n{$nonce}\n{$body}\n"; | ||
|
||
if (\time() - \intval($timestamp) > self::MAX_ALLOWED_CLOCK_OFFSET) { | ||
throw new InvalidSignatureException('Clock Offset Exceeded'); | ||
} | ||
|
||
$publicKey = $this->merchant->getPlatformCert($serial); | ||
|
||
if (! $publicKey) { | ||
throw new InvalidConfigException( | ||
"No platform certs found for serial: {$serial}, | ||
please download from wechat pay and set it in merchant config with key `certs`." | ||
); | ||
} | ||
|
||
if (false === \openssl_verify( | ||
$message, | ||
base64_decode($signature), | ||
strval($publicKey), | ||
OPENSSL_ALGO_SHA256 | ||
)) { | ||
throw new InvalidSignatureException('Invalid Signature'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a0eead0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
easywechat – ./
easywechat-overtrue.vercel.app
easywechat.vercel.app
easywechat-git-6x-overtrue.vercel.app