Small micro-channel micro-channel payment module program ------

Recent projects involve small program development: the need for micro-channel payment module, followed by the narrative, the record about the development of micro-channel micro-payment module of the letter applet to read and use in the future.

Study Guide ---------- micro-channel pay development documents: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_11&index=2

1, prior to the development of small micro-channel payment program, you first need to apply for an account, sign up for a small program developer account, and micro-certified letter.

2, a small program launched micro-channel pay

Get to appid (applet ID), AppSecret, MchID (merchant ID), API Key

Applet pay interaction diagrams

 

Merchant payment systems and micro-channel main interactive system:

1, applets login interface to get openid (applet Login)

2, call the merchant end unified under the single payment interfaces, return prepaid ticket information (unified under a single)

3, applet-side display of two-dimensional code, scan code the customer to pay, end authentication applet invoking payments, returns the result of payment applet to end (Signed again)

4, push to pay a small program end result (the result of payment notification API)

5, merchants pay a query result (Query Order API)

 

important point:

1, signature required for all non-null parameter by parameter name ASSIC lexicographical sorting, stitching API key MD5 encrypted transfer of capital.

2, all the necessary parameters will be transferred in xml using curl initiation request. Then the return value obtained from conversion values ​​xml array form. Note that the return parameters are capitalized.

 

The following generic function method:

	/ ** 
	 * Signature Algorithm 
	 * 1, in accordance with the parameters of the format key = value, and in accordance with the parameter name ASCII lexicographical ordering 
	 * ◆ ASCII code parameter name in ascending order (dictionary order); 
		◆ If the parameter is not involved in air signature; 
		◆ parameter names are case-sensitive; 
		◆ authentication when it returns or micro-channel proactive notification signature, sign parameter transfer does not participate in the signature, and the signature generated sign value for calibration. 
		◆ micro-channel interfaces may increase the field, it is necessary to support increased extension field during signature verification 
	 * 2, key splicing, MD5 encryption, string and turn all uppercase, stitching API key 
	 * 
	 * @param Unknown $ POST 
	 * / 
	function getSign ($ POST) { 
		$ stringA = ''; 
		ksort ($ POST); // Key parameter name in ASCII values are sorted in ascending order (lexicographically) 
		the foreach ($ POST AS Key $ => $ value) { 
			IF (! value $) Continue; 
			! IF ($ stringA) { 
				$ stringA Key $ = "=" $ value;.. 
			} the else { 
				$ stringA = "&" $ Key "=" $ value;.... 
			} 
		} 
		$ stringA. = '&
		$sign = strtoupper(md5($stringA));
		return $sign;
	}

 

    / * * 
     * 32-bit random string 
     * @param Unknown $ len 
     * / 
    Private  function getNonceStr ( $ len ) {
         $ A = Range ( 'A', 'Z' );
         $ B = Range ( 'A', ' the Z ' );
         $ C = Range (' 0 ','. 9 ' );
         $ chars = The array_merge ( $ A , $ B , $ C );
         $ charslen = COUNT ( $ chars ) -1 ;
         shuffle ( $ chars);
        $ nonce_str = '' ;
        for ( $ i = 0; $ i < $ len ; $ i ++ ) {
             $ nonce_str . = $ chars [ mt_rand (0, $ charslen )]; 
        } 
        Return  $ nonce_str ; 
    }

 

  

 

    /**
	 * curl请求
	 * @param unknown $url
	 * @param unknown $data
	 * @param array $headers
	 */
	private function http_request($url,$data = null,$headers=array()) {
		$curl = curl_init();
		if( count($headers) >= 1 ){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		}
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		if (!empty($data)){
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}

  

private function getIP(){
		if(!empty($_SERVER['HTTP_CDN_SRC_IP'])){
			$ip = $_SERVER['HTTP_CDN_SRC_IP'];
	
		}else if (getenv('HTTP_CLIENT_IP')){
			$ip = getenv('HTTP_CLIENT_IP');
	
		}else if (getenv('HTTP_X_FORWARDED_FOR')){ //获取客户端用代理服务器访问时的真实ip 地址
			$ip = getenv('HTTP_X_FORWARDED_FOR');
	
		}else if (getenv('HTTP_X_FORWARDED')){
			$ip = getenv('HTTP_X_FORWARDED');
	
		}else if (getenv('HTTP_FORWARDED_FOR')){
			$ip = getenv('HTTP_FORWARDED_FOR');
	
		}else if (getenv('HTTP_FORWARDED')){
			$ip = getenv('HTTP_FORWARDED');
	
		}else{
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}

  

 

 

Guess you like

Origin www.cnblogs.com/chxrs/p/11078669.html