diff --git a/WePayV3/Contracts/BasicWePay.php b/WePayV3/Contracts/BasicWePay.php index 7ad6d7b..8f628f3 100644 --- a/WePayV3/Contracts/BasicWePay.php +++ b/WePayV3/Contracts/BasicWePay.php @@ -333,7 +333,15 @@ abstract class BasicWePay protected function signBuild($data) { $pkeyid = openssl_pkey_get_private($this->config['cert_private']); - openssl_sign($data, $signature, $pkeyid, 'sha256WithRSAEncryption'); + if ($pkeyid === false) { + throw new InvalidArgumentException("Invalid private key -- [cert_private]"); + } + $signature = ''; + if (!openssl_sign($data, $signature, $pkeyid, 'sha256WithRSAEncryption')) { + openssl_free_key($pkeyid); + throw new InvalidArgumentException("Failed to sign data with private key"); + } + openssl_free_key($pkeyid); return base64_encode($signature); } diff --git a/WePayV3/Order.php b/WePayV3/Order.php index c4866dc..2b18d25 100644 --- a/WePayV3/Order.php +++ b/WePayV3/Order.php @@ -52,6 +52,13 @@ class Order extends BasicWePay if (empty($types[$type])) { throw new InvalidArgumentException("Payment {$type} not defined."); } else { + // 自动填充 mchid 和 appid(如果未提供) + if (empty($data['mchid']) && !empty($this->config['mch_id'])) { + $data['mchid'] = $this->config['mch_id']; + } + if (empty($data['appid']) && !empty($this->config['appid'])) { + $data['appid'] = $this->config['appid']; + } // 创建预支付码 $result = $this->doRequest('POST', $types[$type], json_encode($data, JSON_UNESCAPED_UNICODE), true); if (empty($result['h5_url']) && empty($result['code_url']) && empty($result['prepay_id'])) {