From d64ffe4b07e83f5f4752e631b6344218e6bf6785 Mon Sep 17 00:00:00 2001 From: xl Date: Mon, 13 Nov 2023 23:52:41 +0800 Subject: [PATCH] eee --- wchat/common/AppConfig.php | 65 ++++++++++++++++++++++++++ wchat/common/Multiprogramming.php | 22 +++++++++ wchat/common/PayConfig.php | 77 ++++++------------------------- wchat/common/libs/Aliyun.php | 50 ++++++++++++++++++++ wchat/common/libs/Qq.php | 50 ++++++++++++++++++++ wchat/common/libs/Wx.php | 56 ++++++++++++++++++++++ wchat/wx/V3/WxV3PaymentNotify.php | 38 ++++----------- 7 files changed, 265 insertions(+), 93 deletions(-) create mode 100644 wchat/common/AppConfig.php create mode 100644 wchat/common/libs/Aliyun.php create mode 100644 wchat/common/libs/Qq.php create mode 100644 wchat/common/libs/Wx.php diff --git a/wchat/common/AppConfig.php b/wchat/common/AppConfig.php new file mode 100644 index 0000000..e5fda26 --- /dev/null +++ b/wchat/common/AppConfig.php @@ -0,0 +1,65 @@ + 'string', 'secret' => 'string', 'unionId' => 'string'])] + protected array $notice = ['token' => '', 'secret' => '', 'unionId' => '']; + + + /** + * @param object $app + * @return static + */ + public static function instance(object $app): static + { + $model = new static(); + $model->appId = $app->appId; + $model->appSecret = $app->appSecret; + $model->type = $app->type; + $model->payConfig = PayConfig::parse($app->pay); + return $model; + } + + + /** + * @return bool + */ + public function typeIsApp(): bool + { + return $this->type === 3; + } + +} \ No newline at end of file diff --git a/wchat/common/Multiprogramming.php b/wchat/common/Multiprogramming.php index eacf03c..f337a1d 100644 --- a/wchat/common/Multiprogramming.php +++ b/wchat/common/Multiprogramming.php @@ -22,6 +22,12 @@ abstract class Multiprogramming implements Progaram protected string $errorMsg = ''; + /** + * @var PayConfig + */ + protected PayConfig $payConfig; + + /** * @param $message * @param int $code @@ -74,6 +80,22 @@ abstract class Multiprogramming implements Progaram $this->config = $config; } + /** + * @return PayConfig + */ + public function getPayConfig(): PayConfig + { + return $this->payConfig; + } + + /** + * @param PayConfig $payConfig + */ + public function setPayConfig(PayConfig $payConfig): void + { + $this->payConfig = $payConfig; + } + /** * @return \wchat\common\Config diff --git a/wchat/common/PayConfig.php b/wchat/common/PayConfig.php index 6230fab..546dc0a 100644 --- a/wchat/common/PayConfig.php +++ b/wchat/common/PayConfig.php @@ -2,81 +2,30 @@ namespace wchat\common; -use JetBrains\PhpStorm\ArrayShape; +use wchat\common\libs\Aliyun; +use wchat\common\libs\Qq; +use wchat\common\libs\Wx; class PayConfig { /** - * @var array|string[] + * @var Qq */ - #[ArrayShape(["appId" => "string", "mchCa" => "string", "mchId" => "string", "mchKey" => "string", "mchCert" => "string", "appSecret" => "string", "mchSecret" => "string"])] - protected array $qq = ["appId" => "", "mchCa" => "", "mchId" => "", "mchKey" => "", "mchCert" => "", "appSecret" => "", "mchSecret" => ""]; + public Qq $qq; /** - * @var array|string[] + * @var Wx */ - #[ArrayShape(["appId" => "string", "mchId" => "string", "schema" => "string", "mchKey" => "string", "secret" => "string", "mchCert" => "string", "appSecret" => "string", "SerialNumber" => "string"])] - protected array $wx = ["appId" => "", "mchId" => "", "schema" => "", "mchKey" => "", "secret" => "", "mchCert" => "", "appSecret" => "", "SerialNumber" => ""]; + public Wx $wx; /** - * @var array|string[] + * @var Aliyun */ - #[ArrayShape(["appId" => "string", "appKey" => "string", "appSecret" => "string", "aliPubSecret" => "string", "appPubSecret" => "string", "aliRootSecret" => "string", "openFileState" => "string"])] - protected array $ali = ["appId" => "", "appKey" => "", "appSecret" => "", "aliPubSecret" => "", "appPubSecret" => "", "aliRootSecret" => "", "openFileState" => "0"]; - - - /** - * @return array|string[] - */ - #[ArrayShape(["appId" => "string", "mchCa" => "string", "mchId" => "string", "mchKey" => "string", "mchCert" => "string", "appSecret" => "string", "mchSecret" => "string"])] public function getQq(): array - { - return $this->qq; - } - - /** - * @param array|string[] $qq - */ - public function setQq(array $qq): void - { - $this->qq = $qq; - } - - /** - * @return array|string[] - */ - #[ArrayShape(["appId" => "string", "mchId" => "string", "schema" => "string", "mchKey" => "string", "secret" => "string", "mchCert" => "string", "appSecret" => "string", "SerialNumber" => "string"])] public function getWx(): array - { - return $this->wx; - } - - /** - * @param array|string[] $wx - */ - public function setWx(array $wx): void - { - $this->wx = $wx; - } - - /** - * @return array|string[] - */ - #[ArrayShape(["appId" => "string", "appKey" => "string", "appSecret" => "string", "aliPubSecret" => "string", "appPubSecret" => "string", "aliRootSecret" => "string", "openFileState" => "string"])] - public function getAli(): array - { - return $this->ali; - } - - /** - * @param array|string[] $ali - */ - public function setAli(array $ali): void - { - $this->ali = $ali; - } + public Aliyun $ali; /** @@ -88,10 +37,10 @@ class PayConfig if (is_string($pay)) { $pay = json_decode($pay, true); } - $model = new static(); - foreach ($pay as $key => $value) { - $model->$key = $value; - } + $model = new static(); + $model->ali = \Kiri::configure(new Aliyun(), $pay['ali']); + $model->qq = \Kiri::configure(new Qq(), $pay['qq']); + $model->wx = \Kiri::configure(new Wx(), $pay['wx']); return $model; } diff --git a/wchat/common/libs/Aliyun.php b/wchat/common/libs/Aliyun.php new file mode 100644 index 0000000..17f7664 --- /dev/null +++ b/wchat/common/libs/Aliyun.php @@ -0,0 +1,50 @@ + [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; -const CHR_NUL = "\0"; -const CHR_ETX = "\3"; - - class WxV3PaymentNotify extends SmallProgram { @@ -46,16 +28,14 @@ class WxV3PaymentNotify extends SmallProgram * @param string $event_type * @param string $summary * @param array $resource - * @param string $publicKey */ public function __construct( - public string $id = "EV-2018022511223320873", - public string $create_time = "2015-05-20T13:29:35+08:00", - public string $resource_type = "encrypt-resource", - public string $event_type = "TRANSACTION.SUCCESS", - public string $summary = "支付成功", - public array $resource = [], - public string $publicKey = '' + public string $id = "EV-2018022511223320873", + public string $create_time = "2015-05-20T13:29:35+08:00", + public string $resource_type = "encrypt-resource", + public string $event_type = "TRANSACTION.SUCCESS", + public string $summary = "支付成功", + public array $resource = [] ) { } @@ -74,7 +54,7 @@ class WxV3PaymentNotify extends SmallProgram */ public function verify(RequestInterface $request): bool { - $platformPublicKeyInstance = $this->rsaFrom($this->publicKey, KEY_TYPE_PUBLIC); + $platformPublicKeyInstance = $this->rsaFrom($this->payConfig->wx->mchKey, KEY_TYPE_PUBLIC); $inWechatpaySignature = $request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取 $inWechatpayTimestamp = $request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取 $inWechatpayNonce = $request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取 @@ -147,7 +127,7 @@ class WxV3PaymentNotify extends SmallProgram */ public function decode($ciphertext, $nonce, $associated_data): bool { - $data = $this->decrypt($ciphertext, $this->getConfig()->getMchKey(), $nonce, $associated_data); + $data = $this->decrypt($ciphertext, $this->payConfig->wx->secret, $nonce, $associated_data); $this->notifyModel = new NotifyModel(); $this->notifyModel->amount = $data['amount']; $this->notifyModel->payer = $data['payer'];