diff --git a/wchat/common/AppConfig.php b/wchat/common/AppConfig.php index e5fda26..e2885ef 100644 --- a/wchat/common/AppConfig.php +++ b/wchat/common/AppConfig.php @@ -11,32 +11,40 @@ class AppConfig /** * @var string */ - protected string $appId = ''; + public string $appId = ''; /** * @var string */ - protected string $appSecret = ''; + public string $appSecret = ''; /** * @var PayConfig */ - protected PayConfig $payConfig; + public PayConfig $pay; + private string $proxyHost = ''; + private int $proxyPort = 0; + + private string $notifyUrl = ''; + private string $body = ''; + private string $currency = 'CNY'; + private string $remoteIp = ''; + /** * @var int */ - protected int $type; + public int $type; /** * @var array|string[] */ #[ArrayShape(['token' => 'string', 'secret' => 'string', 'unionId' => 'string'])] - protected array $notice = ['token' => '', 'secret' => '', 'unionId' => '']; + public array $notice = ['token' => '', 'secret' => '', 'unionId' => '']; /** @@ -49,7 +57,7 @@ class AppConfig $model->appId = $app->appId; $model->appSecret = $app->appSecret; $model->type = $app->type; - $model->payConfig = PayConfig::parse($app->pay); + $model->pay = PayConfig::parse($app->pay); return $model; } @@ -62,4 +70,99 @@ class AppConfig return $this->type === 3; } + /** + * @return string + */ + public function getProxyHost(): string + { + return $this->proxyHost; + } + + /** + * @param string $proxyHost + */ + public function setProxyHost(string $proxyHost): void + { + $this->proxyHost = $proxyHost; + } + + /** + * @return int + */ + public function getProxyPort(): int + { + return $this->proxyPort; + } + + /** + * @param int $proxyPort + */ + public function setProxyPort(int $proxyPort): void + { + $this->proxyPort = $proxyPort; + } + + /** + * @return string + */ + public function getNotifyUrl(): string + { + return $this->notifyUrl; + } + + /** + * @param string $notifyUrl + */ + public function setNotifyUrl(string $notifyUrl): void + { + $this->notifyUrl = $notifyUrl; + } + + /** + * @return string + */ + public function getBody(): string + { + return $this->body; + } + + /** + * @param string $body + */ + public function setBody(string $body): void + { + $this->body = $body; + } + + /** + * @return string + */ + public function getCurrency(): string + { + return $this->currency; + } + + /** + * @param string $currency + */ + public function setCurrency(string $currency): void + { + $this->currency = $currency; + } + + /** + * @return string + */ + public function getRemoteIp(): string + { + return $this->remoteIp; + } + + /** + * @param string $remoteIp + */ + public function setRemoteIp(string $remoteIp): void + { + $this->remoteIp = $remoteIp; + } } \ No newline at end of file diff --git a/wchat/common/Multiprogramming.php b/wchat/common/Multiprogramming.php index f337a1d..b5d2f03 100644 --- a/wchat/common/Multiprogramming.php +++ b/wchat/common/Multiprogramming.php @@ -8,8 +8,6 @@ namespace wchat\common; -use wchat\common\Config; - abstract class Multiprogramming implements Progaram { @@ -23,9 +21,9 @@ abstract class Multiprogramming implements Progaram /** - * @var PayConfig + * @var AppConfig */ - protected PayConfig $payConfig; + protected AppConfig $payConfig; /** @@ -72,7 +70,7 @@ abstract class Multiprogramming implements Progaram /** - * @param \wchat\common\Config $config + * @param Config $config * @return void */ public function setConfig(Config $config): void @@ -81,26 +79,26 @@ abstract class Multiprogramming implements Progaram } /** - * @return PayConfig + * @return AppConfig */ - public function getPayConfig(): PayConfig + public function getPayConfig(): AppConfig { return $this->payConfig; } /** - * @param PayConfig $payConfig + * @param AppConfig $payConfig */ - public function setPayConfig(PayConfig $payConfig): void + public function setPayConfig(AppConfig $payConfig): void { $this->payConfig = $payConfig; } /** - * @return \wchat\common\Config + * @return Config */ - public function getConfig(): \wchat\common\Config + public function getConfig(): Config { return $this->config; } diff --git a/wchat/common/libs/Wx.php b/wchat/common/libs/Wx.php index c5a8f3a..ca492a3 100644 --- a/wchat/common/libs/Wx.php +++ b/wchat/common/libs/Wx.php @@ -51,6 +51,6 @@ class Wx /** * @var string */ - public string $serialNumber; + public string $SerialNumber; } \ No newline at end of file diff --git a/wchat/wx/V3/TransferBatches.php b/wchat/wx/V3/TransferBatches.php index 20fda63..279c3b3 100644 --- a/wchat/wx/V3/TransferBatches.php +++ b/wchat/wx/V3/TransferBatches.php @@ -21,12 +21,17 @@ class TransferBatches extends SmallProgram #[ArrayShape([])] public function request(TransferDetail $detail): array { - $body = []; - $body['appid'] = $this->getConfig()->getAppid(); + $payConfig = $this->getPayConfig(); + $body = []; + if ($payConfig->typeIsApp()) { + $body['appid'] = $payConfig->pay->wx->appId; + } else { + $body['appid'] = $payConfig->appId; + } $body['out_batch_no'] = $detail->out_detail_no; - $body["batch_name"] = $this->getConfig()->getBody(); - $body["body"] = $this->getConfig()->getBody(); - $body["batch_remark"] = $this->getConfig()->getBody(); + $body["batch_name"] = $payConfig->getBody(); + $body["body"] = $payConfig->getBody(); + $body["batch_remark"] = $payConfig->getBody(); $body["total_amount"] = $detail->transfer_amount; $body["total_num"] = 1; $body["transfer_detail_list"] = [$detail->toArray()]; diff --git a/wchat/wx/V3/WxV3PaymentNotify.php b/wchat/wx/V3/WxV3PaymentNotify.php index ab34153..2e9e631 100644 --- a/wchat/wx/V3/WxV3PaymentNotify.php +++ b/wchat/wx/V3/WxV3PaymentNotify.php @@ -54,7 +54,7 @@ class WxV3PaymentNotify extends SmallProgram */ public function verify(RequestInterface $request): bool { - $platformPublicKeyInstance = $this->rsaFrom($this->payConfig->wx->mchKey, KEY_TYPE_PUBLIC); + $platformPublicKeyInstance = $this->rsaFrom($this->payConfig->pay->wx->mchKey, KEY_TYPE_PUBLIC); $inWechatpaySignature = $request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取 $inWechatpayTimestamp = $request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取 $inWechatpayNonce = $request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取 @@ -127,7 +127,7 @@ class WxV3PaymentNotify extends SmallProgram */ public function decode($ciphertext, $nonce, $associated_data): bool { - $data = $this->decrypt($ciphertext, $this->payConfig->wx->secret, $nonce, $associated_data); + $data = $this->decrypt($ciphertext, $this->payConfig->pay->wx->secret, $nonce, $associated_data); $this->notifyModel = new NotifyModel(); $this->notifyModel->amount = $data['amount']; $this->notifyModel->payer = $data['payer']; diff --git a/wchat/wx/V3/WxV3PaymentTait.php b/wchat/wx/V3/WxV3PaymentTait.php index 63f2e8a..a3b128d 100644 --- a/wchat/wx/V3/WxV3PaymentTait.php +++ b/wchat/wx/V3/WxV3PaymentTait.php @@ -44,12 +44,17 @@ trait WxV3PaymentTait */ public function getInitCore($orderNo, $total): array { - $body['appid'] = $this->getConfig()->getAppid(); - $body['mchid'] = $this->getConfig()->getMchId(); - $body['description'] = $this->getConfig()->getBody(); + $payConfig = $this->getPayConfig(); + if ($payConfig->typeIsApp()) { + $body['appid'] = $payConfig->pay->wx->appId; + } else { + $body['appid'] = $payConfig->appId; + } + $body['mchid'] = $payConfig->pay->wx->mchId; + $body['description'] = $payConfig->getBody(); $body['out_trade_no'] = $orderNo; - $body['notify_url'] = $this->getConfig()->getNotifyUrl(); - $body['amount'] = ['total' => $total, 'currency' => 'CNY']; + $body['notify_url'] = $payConfig->getNotifyUrl(); + $body['amount'] = ['total' => $total, 'currency' => $payConfig->getCurrency()]; return $body; } @@ -84,6 +89,8 @@ trait WxV3PaymentTait */ public function signature(string $http_method, string $canonical_url, string $body = ''): string { + $payConfig = $this->getPayConfig(); + $rand = md5(random_bytes(32)); $time = time(); @@ -91,8 +98,8 @@ trait WxV3PaymentTait $sign = $this->openssl_signature($message); - return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $this->getConfig()->getSchema(), - $this->getConfig()->getMchId(), $rand, $time, $this->getConfig()->getSerialNo(), $sign); + return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $payConfig->pay->wx->schema, + $payConfig->pay->wx->mchId, $rand, $time, $payConfig->pay->wx->SerialNumber, $sign); } @@ -102,9 +109,9 @@ trait WxV3PaymentTait */ public function openssl_signature($body): string { - $pem = file_get_contents($this->getConfig()->getMchKey()); + $payConfig = $this->getPayConfig(); - $mch_private_key = openssl_get_privatekey($pem); + $mch_private_key = openssl_get_privatekey($payConfig->pay->wx->mchKey); openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption'); return base64_encode($raw_sign);