真在的只需要一個class類文件幾個函數就可以調用微信的所有支付,不管是微信公眾號、h5、app、小程序支付,加密方式都是一樣的,對比下微信的支付文檔,只需要修改第一步傳入的參數不同,下單地址不同,加密方式都是一樣的,所以只要看懂下面的第一個函數,將搞定微信的任何支付。
微信支付class類,第一個函數是調用微信支付,第二個函數是支付后回調,其它函數都是加密相關,無法關注
<?php /** * 微信支付 * @author 潤雪科技 * */ class pay { /** * 獲取支付參數 */ public function pay() { //基本的訂單參數 $param['appid'] = 'xxxxx' ; //微信公眾號或小程序的appid $param['mch_id'] = 'xxxxx' ; //商戶號id $param['nonce_str'] = 'xxxxx' ; //隨機字符轉,可用弄10位長度 $param['body'] = '購買商品' ; //商品描述,就寫購買商品或商品名詞都可以 $param['attach'] = 'xxxxx' ; //附加數據,一般都是傳訂單號或支付類型,如果是多個參數可通過一定方式拼接成字符串,等異步回調的時候通過相同方式解開即可 $param['out_trade_no'] = 'xxxxx' ; //訂單號,在我們自己的系統中必須是唯一的 $param['total_fee'] = 100 ; //支付金額,調用微信支付單位是分,所以這一般我們都要用脂肪層金額來乘以100 $param['spbill_create_ip'] = 'xxxxx' ; //用戶的ip $param['notify_url'] = 'https://www.runxuekeji/' ; //回調url $param['trade_type'] = 'JSAPI' ; //交易類型 $param['sign'] = $this->make_sign( $param , '商戶中設置的32位密鑰key' ) ; //生成簽名 //調用微信下單地址返回訂單號等 $xml = $this->to_xml( $param ) ; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' ; //生成訂單的地址 $response = $this->from_xml( $this->post_xml_curl( $xml , $url ) ) ; //組裝支付需要的參數 $result['appid'] = 'xxxxx' ; //微信公眾號或小程序的appid $result['partnerid'] = 'xxxxx' ; //商戶號id $result['prepayid'] = $response['prepay_id'] ; $result['package'] = 'Sign=WXPay'; $result['noncestr'] = 'xxxxx' ; //隨機字符串 $result['timestamp'] = time() ; //時間戳 $result['sign'] = $this->make_sign( $result , '商戶中設置的32位密鑰key' ) ; //生成簽名 return result($result,1,'獲取成功'); } /** * 生成簽名 * @param $data array 生成前面要傳的參數 * @param $key string 商戶key * @return 簽名,本函數不覆蓋sign成員變量,如要設置簽名需要調用SetSign方法賦值 */ private function make_sign( $data , $key ) { //簽名步驟一:按字典序排序參數 ksort( $data ); $string = $this->to_url_params( $data ); //簽名步驟二:在string后加入KEY $string = $string . "&key=" . $key; //簽名步驟三:MD5加密 $string = md5($string); //簽名步驟四:所有字符轉為大寫 $result = strtoupper($string); return $result; } /** * 格式化參數格式化成url參數 * @return string */ private function to_url_params( $data ) { $buff = ""; foreach ($data as $k => $v) { if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; } /** * 輸出xml字符 * @throws WxPayException **/ private function to_xml( $param ) { if(!is_array($param) || count($param) <= 0) { throw new WxPayException("數組數據異常!"); } $xml = "<xml>"; foreach ($param as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } /** * 將xml轉為array * @param string $xml * @throws WxPayException */ private function from_xml($xml) { libxml_disable_entity_loader(true); return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); } /** * 以post方式提交xml到對應的接口url * * @param string $xml 需要post的xml數據 * @param string $url url * @param bool $useCert 是否需要證書,默認不需要 * @param int $second url執行超時時間,默認30s * @throws WxPayException */ private function post_xml_curl($xml, $url, $useCert = false, $second = 30 , $curl_proxy = false ) { $ch = curl_init(); //設置超時 curl_setopt($ch, CURLOPT_TIMEOUT, $second); //如果有配置代理這里就設置代理 if( $curl_proxy ){ curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST); curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT); } curl_setopt($ch,CURLOPT_URL, $url); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); } else { curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗 } //設置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if($useCert == true){ //設置證書 curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); //curl_setopt($ch,CURLOPT_SSLCERT, dirname(__FILE__).'/'. '../apiclient_cert.pem' ); //curl_setopt($ch,CURLOPT_SSLKEY, dirname(__FILE__).'/'.'../apiclient_key.pem'); } //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //運行curl $data = curl_exec($ch); //返回結果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); die( "curl出錯,錯誤碼:$error" ) ; } } }