From ac4c6c1e279aabbafc7d422e31d5059861df1912 Mon Sep 17 00:00:00 2001 From: xl Date: Mon, 13 Nov 2023 22:57:36 +0800 Subject: [PATCH] eee --- wchat/wx/V3/WxV3PaymentNotify.php | 79 ++++--------------------------- 1 file changed, 9 insertions(+), 70 deletions(-) diff --git a/wchat/wx/V3/WxV3PaymentNotify.php b/wchat/wx/V3/WxV3PaymentNotify.php index 657fe5f..1f7a434 100644 --- a/wchat/wx/V3/WxV3PaymentNotify.php +++ b/wchat/wx/V3/WxV3PaymentNotify.php @@ -3,6 +3,7 @@ namespace wchat\wx\V3; use Exception; +use OpenSSLAsymmetricKey; use Psr\Http\Message\RequestInterface; use wchat\wx\SmallProgram; use wchat\wx\V3\Notify\GoodsDetail; @@ -45,7 +46,7 @@ class WxV3PaymentNotify extends SmallProgram * @param string $event_type * @param string $summary * @param array $resource - * @throws Exception + * @param string $publicKey */ public function __construct( public string $id = "EV-2018022511223320873", @@ -53,7 +54,8 @@ class WxV3PaymentNotify extends SmallProgram public string $resource_type = "encrypt-resource", public string $event_type = "TRANSACTION.SUCCESS", public string $summary = "支付成功", - public array $resource = [] + public array $resource = [], + public string $publicKey = '' ) { } @@ -72,7 +74,7 @@ class WxV3PaymentNotify extends SmallProgram */ public function verify(RequestInterface $request): bool { - $platformPublicKeyInstance = $this->rsaFrom('file:///path/to/wechatpay/inWechatpaySerial.pem', KEY_TYPE_PUBLIC); + $platformPublicKeyInstance = $this->rsaFrom($this->publicKey, KEY_TYPE_PUBLIC); $inWechatpaySignature = $request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取 $inWechatpayTimestamp = $request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取 $inWechatpayNonce = $request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取 @@ -126,80 +128,17 @@ class WxV3PaymentNotify extends SmallProgram /** * @param $thing * @param string $type - * @return \OpenSSLAsymmetricKey + * @return OpenSSLAsymmetricKey */ - protected function rsaFrom($thing, string $type = KEY_TYPE_PRIVATE): \OpenSSLAsymmetricKey + protected function rsaFrom($thing, string $type = KEY_TYPE_PRIVATE): OpenSSLAsymmetricKey { - $pkey = ($isPublic = $type === KEY_TYPE_PUBLIC) ? openssl_pkey_get_public($this->parse($thing, $type)) : openssl_pkey_get_private($this->parse($thing)); + $pkey = (($isPublic = $type === KEY_TYPE_PUBLIC) ? openssl_pkey_get_public(file_get_contents($thing)) : openssl_pkey_get_private(file_get_contents($thing))); if (false === $pkey) { - throw new \UnexpectedValueException(sprintf('Cannot load %s from(%s), please take care about the \$thing input.', $isPublic ? 'publicKey' : 'privateKey', gettype($thing))); + throw new \UnexpectedValueException(sprintf('Cannot load %s from(%s), please take care about the $thing input.', $isPublic ? 'publicKey' : 'privateKey', gettype($thing))); } return $pkey; } - - /** - * @param $thing - * @param string $type - * @return mixed|string - */ - protected function parse($thing, string $type = KEY_TYPE_PRIVATE): mixed - { - $src = $thing; - if (is_string($src) && is_int(strpos($src, PKEY_PEM_NEEDLE)) && $type === KEY_TYPE_PUBLIC && preg_match(PKEY_PEM_FORMAT_PATTERN, $src, $matches)) { - [, $kind, $base64] = $matches; - $mapRules = array_combine(array_column(RULES, 1/*column*/), array_keys(RULES)); - $protocol = $mapRules[$kind] ?? ''; - if ('public.pkcs1' === $protocol) { - $src = sprintf('%s://%s', $protocol, str_replace([CHR_CR, CHR_LF], '', $base64)); - } - } - if (is_string($src) && is_bool(strpos($src, LOCAL_FILE_PROTOCOL)) && is_int(strpos($src, '://'))) { - $protocol = parse_url($src, PHP_URL_SCHEME); - [$format, $kind, $offset] = RULES[$protocol] ?? [null, null, null]; - if ($format && $kind && $offset) { - $src = substr($src, $offset); - if ('public.pkcs1' === $protocol) { - $src = $this->pkcs1ToSpki($src); - [$format, $kind] = RULES['public.spki']; - } - return sprintf($format, $kind, wordwrap($src, 64, CHR_LF, true)); - } - } - - return $src; - } - - - /** - * @param string $thing - * @return string - */ - protected function pkcs1ToSpki(string $thing): string - { - $raw = CHR_NUL . base64_decode($thing); - $new = pack('H*', ASN1_OID_RSAENCRYPTION) . CHR_ETX . self::encodeLength($raw) . $raw; - - return base64_encode(pack('Ca*a*', ASN1_SEQUENCE, self::encodeLength($new), $new)); - } - - - /** - * @param string $thing - * @return string - */ - protected function encodeLength(string $thing): string - { - $num = strlen($thing); - if ($num <= 0x7F) { - return sprintf('%c', $num); - } - - $tmp = ltrim(pack('N', $num), CHR_NUL); - return pack('Ca*', strlen($tmp) | 0x80, $tmp); - } - - /** * @param $ciphertext * @param $nonce