mirror of
				https://gitee.com/zoujingli/WeChatDeveloper.git
				synced 2025-10-31 01:42:09 +08:00 
			
		
		
		
	增加V3接口配置
This commit is contained in:
		
							parent
							
								
									974f18ebf7
								
							
						
					
					
						commit
						4be6d206d9
					
				| @ -107,13 +107,19 @@ abstract class BasicWePay | |||||||
|     public function doRequest($method, $pathinfo, $jsondata = '', $verify = false) |     public function doRequest($method, $pathinfo, $jsondata = '', $verify = false) | ||||||
|     { |     { | ||||||
|         list($time, $nonce) = [time(), uniqid() . rand(1000, 9999)]; |         list($time, $nonce) = [time(), uniqid() . rand(1000, 9999)]; | ||||||
|         $jsondata = join("\n", [$method, $pathinfo, $time, $nonce, $jsondata, '']); |         $signstr = join("\n", [ | ||||||
|  |             $method, $pathinfo, $time, $nonce, $jsondata, '', | ||||||
|  |         ]); | ||||||
|         // 生成数据签名TOKEN
 |         // 生成数据签名TOKEN
 | ||||||
|         $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', |         $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', | ||||||
|             $this->config['mch_id'], $nonce, $time, $this->config['cert_serial'], $this->signBuild($jsondata) |             $this->config['mch_id'], $nonce, $time, $this->config['cert_serial'], $this->signBuild($signstr) | ||||||
|         ); |         ); | ||||||
|         $header = ["Accept: application/json", 'User-Agent: https://thinkadmin.top', "Authorization: WECHATPAY2-SHA256-RSA2048 {$token}"]; |         list($header, $content) = $this->_doRequestCurl($method, $this->base . $pathinfo, [ | ||||||
|         list($header, $result) = $this->_doRequestCurl($method, $this->base . $pathinfo, ['data' => $jsondata, 'header' => $header]); |             'data' => $jsondata, 'header' => [ | ||||||
|  |                 "Accept: application/json", "Content-Type: application/json", | ||||||
|  |                 'User-Agent: https://thinkadmin.top', "Authorization: WECHATPAY2-SHA256-RSA2048 {$token}", | ||||||
|  |             ], | ||||||
|  |         ]); | ||||||
|         if ($verify) { |         if ($verify) { | ||||||
|             $headers = []; |             $headers = []; | ||||||
|             foreach (explode("\n", $header) as $line) { |             foreach (explode("\n", $header) as $line) { | ||||||
| @ -123,12 +129,12 @@ abstract class BasicWePay | |||||||
|                     $headers[$keys] = trim($value); |                     $headers[$keys] = trim($value); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             $content = join("\n", [$headers['timestamp'], $headers['nonce'], $result, '']); |             $string = join("\n", [$headers['timestamp'], $headers['nonce'], $content, '']); | ||||||
|             if (!$this->signVerify($content, $headers['signature'], $headers['serial'])) { |             if (!$this->signVerify($string, $headers['signature'], $headers['serial'])) { | ||||||
|                 throw new InvalidResponseException("验证响应签名失败"); |                 throw new InvalidResponseException("验证响应签名失败"); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         return json_decode($result, true); |         return json_decode($content, true); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -141,15 +147,15 @@ abstract class BasicWePay | |||||||
|     private function _doRequestCurl($method, $location, $options = []) |     private function _doRequestCurl($method, $location, $options = []) | ||||||
|     { |     { | ||||||
|         $curl = curl_init(); |         $curl = curl_init(); | ||||||
|         // CURL头信息设置
 |  | ||||||
|         if (!empty($options['header'])) { |  | ||||||
|             curl_setopt($curl, CURLOPT_HTTPHEADER, $options['header']); |  | ||||||
|         } |  | ||||||
|         // POST数据设置
 |         // POST数据设置
 | ||||||
|         if (strtolower($method) === 'post') { |         if (strtolower($method) === 'post') { | ||||||
|             curl_setopt($curl, CURLOPT_POST, true); |             curl_setopt($curl, CURLOPT_POST, true); | ||||||
|             curl_setopt($curl, CURLOPT_POSTFIELDS, $options['data']); |             curl_setopt($curl, CURLOPT_POSTFIELDS, $options['data']); | ||||||
|         } |         } | ||||||
|  |         // CURL头信息设置
 | ||||||
|  |         if (!empty($options['header'])) { | ||||||
|  |             curl_setopt($curl, CURLOPT_HTTPHEADER, $options['header']); | ||||||
|  |         } | ||||||
|         curl_setopt($curl, CURLOPT_URL, $location); |         curl_setopt($curl, CURLOPT_URL, $location); | ||||||
|         curl_setopt($curl, CURLOPT_HEADER, true); |         curl_setopt($curl, CURLOPT_HEADER, true); | ||||||
|         curl_setopt($curl, CURLOPT_TIMEOUT, 60); |         curl_setopt($curl, CURLOPT_TIMEOUT, 60); | ||||||
| @ -186,7 +192,10 @@ abstract class BasicWePay | |||||||
|     protected function signVerify($data, $sign, $serial = '') |     protected function signVerify($data, $sign, $serial = '') | ||||||
|     { |     { | ||||||
|         $cert = $this->tmpFile($serial); |         $cert = $this->tmpFile($serial); | ||||||
|         if (empty($cert)) Cert::instance($this->config)->download(); |         if (empty($cert)) { | ||||||
|  |             Cert::instance($this->config)->download(); | ||||||
|  |             $cert = $this->tmpFile($serial); | ||||||
|  |         } | ||||||
|         return openssl_verify($data, base64_decode($sign), openssl_x509_read($cert), 'sha256WithRSAEncryption'); |         return openssl_verify($data, base64_decode($sign), openssl_x509_read($cert), 'sha256WithRSAEncryption'); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -200,13 +209,12 @@ abstract class BasicWePay | |||||||
|     protected function tmpFile($name, $content = null) |     protected function tmpFile($name, $content = null) | ||||||
|     { |     { | ||||||
|         if (is_null($content)) { |         if (is_null($content)) { | ||||||
|             return Tools::getCache($name) ?: ''; |             return base64_decode(Tools::getCache($name) ?: ''); | ||||||
|         } else { |         } else { | ||||||
|             return Tools::setCache($name, $content, 7200); |             return Tools::setCache($name, base64_encode($content), 7200); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * 支付通知 |      * 支付通知 | ||||||
|      * @return array |      * @return array | ||||||
|  | |||||||
							
								
								
									
										16
									
								
								_test/pay-v3-config-cert.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								_test/pay-v3-config-cert.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | |||||||
|  | <?php | ||||||
|  | try { | ||||||
|  |     // 1. 手动加载入口文件
 | ||||||
|  |     include "../include.php"; | ||||||
|  | 
 | ||||||
|  |     // 2. 准备公众号配置参数
 | ||||||
|  |     $config = include "./pay-v3-config.php"; | ||||||
|  | 
 | ||||||
|  |     $payment = \WePayV3\Cert::instance($config); | ||||||
|  | 
 | ||||||
|  |     $payment->download(); | ||||||
|  | 
 | ||||||
|  | } catch (\Exception $exception) { | ||||||
|  |     // 出错啦,处理下吧
 | ||||||
|  |     echo $exception->getMessage() . PHP_EOL; | ||||||
|  | } | ||||||
							
								
								
									
										16
									
								
								_test/pay-v3-config.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								_test/pay-v3-config.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | return [ | ||||||
|  |     'mch_id'       => '您的商户编号', | ||||||
|  |     'mch_v3_key'   => '您的V3接口密码', | ||||||
|  |     'cert_public'  => <<<EOF | ||||||
|  | -----BEGIN CERTIFICATE----- | ||||||
|  | 您的证书内容 | ||||||
|  | -----END CERTIFICATE----- | ||||||
|  | EOF, | ||||||
|  |     'cert_private' => <<<EOF | ||||||
|  | -----BEGIN PRIVATE KEY----- | ||||||
|  | 您的证书内容 | ||||||
|  | -----END PRIVATE KEY----- | ||||||
|  | EOF, | ||||||
|  | ]; | ||||||
							
								
								
									
										30
									
								
								_test/pay-v3-order-create.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								_test/pay-v3-order-create.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | |||||||
|  | <?php | ||||||
|  | try { | ||||||
|  |     // 1. 手动加载入口文件
 | ||||||
|  |     include "../include.php"; | ||||||
|  | 
 | ||||||
|  |     // 2. 准备公众号配置参数
 | ||||||
|  |     $config = include "./pay-v3-config.php"; | ||||||
|  | 
 | ||||||
|  |     // 3. 创建接口实例
 | ||||||
|  |     $payment = \WePayV3\Order::instance($config); | ||||||
|  | 
 | ||||||
|  |     // 4. 组装支付参数
 | ||||||
|  |     $result = $payment->create('jsapi', json_encode([ | ||||||
|  |         'appid'        => 'wx60a43dd8161666d4', | ||||||
|  |         'mchid'        => $config['mch_id'], | ||||||
|  |         'description'  => '商品描述', | ||||||
|  |         'out_trade_no' => date("YmdHis"), | ||||||
|  |         'notify_url'   => 'https://thinkadmin.top', | ||||||
|  |         'payer'        => ['openid' => 'o38gps3vNdCqaggFfrBRCRikwlWY'], | ||||||
|  |         'amount'       => ['total' => 1, 'currency' => 'CNY'], | ||||||
|  |     ], JSON_UNESCAPED_UNICODE)); | ||||||
|  | 
 | ||||||
|  |     echo '<pre>'; | ||||||
|  |     echo "\n--- 创建预支付码 ---\n"; | ||||||
|  |     var_export($result); | ||||||
|  | 
 | ||||||
|  | } catch (\Exception $exception) { | ||||||
|  |     // 出错啦,处理下吧
 | ||||||
|  |     echo $exception->getMessage() . PHP_EOL; | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user