diff --git a/wchat/wx/V3/WxV3PaymentNotify.php b/wchat/wx/V3/WxV3PaymentNotify.php index 5585d99..37f5b86 100644 --- a/wchat/wx/V3/WxV3PaymentNotify.php +++ b/wchat/wx/V3/WxV3PaymentNotify.php @@ -9,10 +9,8 @@ use wchat\wx\V3\Notify\GoodsDetail; use wchat\wx\V3\Notify\NotifyModel; use wchat\wx\V3\Notify\PromotionDetail; -const KEY_LENGTH_BYTE = 32; -const AUTH_TAG_LENGTH_BYTE = 16; -const KEY_TYPE_PUBLIC = 'public'; -const KEY_TYPE_PRIVATE = 'private'; +const KEY_TYPE_PUBLIC = 'public'; +const KEY_TYPE_PRIVATE = 'private'; const LOCAL_FILE_PROTOCOL = 'file://'; const PKEY_PEM_NEEDLE = ' KEY-'; @@ -22,10 +20,10 @@ const CHR_CR = "\r"; const CHR_LF = "\n"; const RULES = [ - 'private.pkcs1' => [self::PKEY_PEM_FORMAT, 'RSA PRIVATE', 16], - 'private.pkcs8' => [self::PKEY_PEM_FORMAT, 'PRIVATE', 16], - 'public.pkcs1' => [self::PKEY_PEM_FORMAT, 'RSA PUBLIC', 15], - 'public.spki' => [self::PKEY_PEM_FORMAT, 'PUBLIC', 14], + 'private.pkcs1' => [PKEY_PEM_FORMAT, 'RSA PRIVATE', 16], + 'private.pkcs8' => [PKEY_PEM_FORMAT, 'PRIVATE', 16], + 'public.pkcs1' => [PKEY_PEM_FORMAT, 'RSA PUBLIC', 15], + 'public.spki' => [PKEY_PEM_FORMAT, 'PUBLIC', 14], ]; const ASN1_OID_RSAENCRYPTION = '300d06092a864886f70d0101010500'; const ASN1_SEQUENCE = 48; @@ -87,8 +85,7 @@ class WxV3PaymentNotify extends SmallProgram if (!$timeOffsetStatus || !$verifiedStatus) { return false; } - $this->decode(); - return true; + return $this->decode($this->resource['ciphertext'], $this->resource['nonce'], $this->resource['associated_data']); } @@ -133,18 +130,10 @@ class WxV3PaymentNotify extends SmallProgram */ 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($this->parse($thing, $type)) : openssl_pkey_get_private($this->parse($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; } @@ -157,10 +146,9 @@ class WxV3PaymentNotify extends SmallProgram 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)) { + 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)array_combine(array_column(RULES, 1/*column*/), array_keys(RULES)); + $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)); @@ -213,12 +201,14 @@ class WxV3PaymentNotify extends SmallProgram /** - * @return void - * @throws Exception + * @param $ciphertext + * @param $nonce + * @param $associated_data + * @return bool */ - public function decode(): void + public function decode($ciphertext, $nonce, $associated_data): bool { - $data = $this->decrypt($this->resource['ciphertext'], $this->resource['nonce'], $this->resource['associated_data']); + $data = $this->decrypt($ciphertext, $nonce, $associated_data); $this->notifyModel = new NotifyModel(); $this->notifyModel->amount = $data['amount']; $this->notifyModel->payer = $data['payer']; @@ -251,6 +241,7 @@ class WxV3PaymentNotify extends SmallProgram } $this->notifyModel->promotion_detail[] = $detail; } + return true; }