【微信開發】PHP服務端微信支付及支付回調


<?php
//ecshop項目文件,非必須
define('IN_ECS', true);
header("Content-type: text/html; charset=utf-8");

$user_id  = isset($_REQUEST['user_id']) ? trim($_REQUEST['user_id']) : '85';

//獲取支付的金額
$money = isset($_REQUEST['money']) ? trim($_REQUEST['money']) : 1;
//微信中金額以分為單位,所以*100
$money = $money*100;
if($user_id == 0 || $money ==0){ $result = array('code' => 1,'data'=>'缺少參數' ); die(json_encode($result)); }else{
 //ecshop模板文件,非必須項,以實際項目為准 require(dirname(__FILE__) . '../../includes/init.php'); include_once(ROOT_PATH .'includes/lib_clips.php'); $user_m = get_user_info($user_id); } $user_name = $user_m['user_name']; $sn = chongzhi_sn(); $orderBody = "微信充值"; $tade_no = $sn; $total_fee = $money;//以分為單位 $user_name = $user_name; $WxPayHelper = new WxPayHelper();
//獲取預支付訂單號 $response = $WxPayHelper->getPrePayOrder($orderBody, $tade_no, $total_fee); //print_r($response); $bizcontent = array( 'user_id' => $user_id, 'amount' => $money/100, 'user_name' => $user_m['user_name'], 'payment' => "微信", 'cz_sn' => $sn, 'add_time' => gmtime() ); $x = $WxPayHelper->getOrder($response); if($GLOBALS['db']->autoExecute($ecs->table('user_chongzhi'), $bizcontent, 'INSERT')){ if($x){ $result = array('code'=>0,'data'=>$x); }else{ $result = array('code'=>1,'data'=>'生成訂單失敗'); } die(json_encode($result)); }else{ $result = array('code'=>1,'data'=>'生成訂單失敗'); die(json_encode($result)); } // p_val($x); /** * convert xml string to php array - useful to get a serializable value * * @param string $xmlstr * @return array * @author Adrien aka Gaarf */ class WxPayHelper{ /* 配置參數 */ var $config = array( 'appid' => "wx123456", /*微信開放平台上的應用id*/ 'mch_id' => "123456", /*通過微信支付商戶資料審核之后郵件中的商戶id*/ 'key' => "123456", /*在微信商戶平台上自己設定的api密鑰 32位*/ 'notify_url' => '項目地址/wxbac.php' /*自定義的回調程序地址*/ ); public function __construct() { } //獲取預支付訂單 public function getPrePayOrder($body, $out_trade_no, $total_fee){ $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $notify_url = $this->config["notify_url"]; $onoce_str = $this->getRandChar(16); $data["appid"] = $this->config["appid"]; $data["body"] = $body; $data["mch_id"] = $this->config['mch_id']; $data["nonce_str"] = $onoce_str; $data["notify_url"] = $notify_url; $data["out_trade_no"] = $out_trade_no; $data["spbill_create_ip"] = $this->get_client_ip(); $data["total_fee"] = $total_fee; $data["trade_type"] = "APP"; /*自定義*/ //$data["user_name"] = $user_name; /*end*/ $s = $this->getSign($data, false); $data["sign"] = $s; $xml = $this->arrayToXml($data); $response = $this->postXmlCurl($xml, $url); //將微信返回的結果xml轉成數組 return $this->xmlstr_to_array($response); } //執行第二次簽名,才能返回給客戶端使用 public function getOrder($prepayId){ $data["appid"] = $this->config["appid"]; $data["noncestr"] = $prepayId["nonce_str"]; $data["package"] = "Sign=WXPay"; $data["partnerid"] = $this->config['mch_id']; $data["prepayid"] = $prepayId["prepay_id"]; $data["timestamp"] = (String)time(); $s = $this->getSign($data, false); $data["sign"] = $s; return $data; } /* 生成簽名 */ function getSign($Obj) { foreach ($Obj as $k => $v) { $Parameters[strtolower($k)] = $v; } //簽名步驟一:按字典序排序參數 ksort($Parameters); $String = $this->formatBizQueryParaMap($Parameters, false); //echo "【string】 =".$String."</br>"; //簽名步驟二:在string后加入KEY $String = $String."&key=".$this->config['key']; // echo "<textarea style='width: 50%; height: 150px;'>$String</textarea> <br />"; //簽名步驟三:MD5加密 $result_ = strtoupper(md5($String)); return $result_; } //獲取指定長度的隨機字符串 function getRandChar($length){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($strPol)-1; for($i=0;$i<$length;$i++){ $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介於min和max兩個數之間的一個隨機整數 } return $str; } //數組轉xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)) { $xml.="<".$key.">".$val."</".$key.">"; } else $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } $xml.="</xml>"; return $xml; } //post https請求,CURLOPT_POSTFIELDS xml格式 function postXmlCurl($xml,$url,$second=30) { //初始化curl $ch = curl_init(); //超時時間 curl_setopt($ch,CURLOPT_TIMEOUT,$second); //這里設置代理,如果有的話 //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8'); //curl_setopt($ch,CURLOPT_PROXYPORT, 8080); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); //設置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //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); echo "curl出錯,錯誤碼:$error"."<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>錯誤原因查詢</a></br>"; curl_close($ch); return false; } } /* 獲取當前服務器的IP */ function get_client_ip() { if ($_SERVER['REMOTE_ADDR']) { $cip = $_SERVER['REMOTE_ADDR']; } elseif (getenv("REMOTE_ADDR")) { $cip = getenv("REMOTE_ADDR"); } elseif (getenv("HTTP_CLIENT_IP")) { $cip = getenv("HTTP_CLIENT_IP"); } else { $cip = "unknown"; } return $cip; } //將數組轉成uri字符串 function formatBizQueryParaMap($paraMap, $urlencode) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if($urlencode) { $v = urlencode($v); } $buff .= strtolower($k) . "=" . $v . "&"; } $reqPar; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar; } /** xml轉成數組 */ function xmlstr_to_array($xmlstr) { $doc = new DOMDocument(); $doc->loadXML($xmlstr); return $this->domnode_to_array($doc->documentElement); } function domnode_to_array($node) { $output = array(); switch ($node->nodeType) { case XML_CDATA_SECTION_NODE: case XML_TEXT_NODE: $output = trim($node->textContent); break; case XML_ELEMENT_NODE: for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { $child = $node->childNodes->item($i); $v = $this->domnode_to_array($child); if(isset($child->tagName)) { $t = $child->tagName; if(!isset($output[$t])) { $output[$t] = array(); } $output[$t][] = $v; } elseif($v) { $output = (string) $v; } } if(is_array($output)) { if($node->attributes->length) { $a = array(); foreach($node->attributes as $attrName => $attrNode) { $a[$attrName] = (string) $attrNode->value; } $output['@attributes'] = $a; } foreach ($output as $t => $v) { if(is_array($v) && count($v)==1 && $t!='@attributes') { $output[$t] = $v[0]; } } } break; } return $output; } } function chongzhi_sn() { /* 選擇一個隨機的方案 */ mt_srand((double) microtime() * 1000000); return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); } ?>

  支付回調頁面

<?php
define('IN_ECS', true);

//微信返回的數據
$input = file_get_contents("php://input");

$myfile = fopen("wxtestfile.txt", "a");
fwrite($myfile, "\r\n");
fwrite($myfile, $input);

if($input){
	$xml = simplexml_load_string($input);
	$money = (string)$xml->total_fee;
	$return_code = (string)$xml->return_code;
	$attach = (string)$xml->attach;
	$user_id = (string)$xml->user_id;
	$out_trade_no = (string)$xml->out_trade_no;
}

if($return_code){
	//業務處理
      //修改訂單/用戶狀態      //業務處理 echo 'success'; }else{ echo 'fail'; } fclose($myfile); ?>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM