/** * 組合微信app支付 獲得prepayid * @param int $order_num */ private function _wxpay_request($order_num = 0) { //判斷訂單編號必須是數組並且不為0 check_order_num($order_num) ...
/**
* 組合微信app支付 獲得prepayid
* @param int $order_num
*/
private function _wxpay_request($order_num = 0)
{
//判斷訂單編號必須是數組並且不為0
check_order_num($order_num);
//引入微信支付類
libraries_include("wxpay/", "WxPayHelper.app.php");
//支付介面發起url
$pay_url = $this->config->item("PAY_URL");
//通知地址
$notify_url = $this->config->item("WEIXIN_NOTIFY_URL");
//微信配置
$pay_config = $this->config->item("WEIXIN_PAY_NEED");
$helper = new WxPayHelper();
//隨機字元串
$nonce_str = $helper->getRandChar(32);
//獲得訂單數據
$order_data = $this->order_model->get_one($order_num);
$data["appid"] = $pay_config['appid'];//微信開放平臺審核通過的應用APPID
$data["body"] = $pay_config['body'];//商品或支付單簡要描述
$data["mch_id"] = $pay_config['mch_id'];//商戶號
$data["nonce_str"] = $nonce_str;//隨機字元串
$data["notify_url"] = $notify_url;//通知地址
$data["out_trade_no"] = $order_data["order_num"];//商戶訂單號
$data["spbill_create_ip"] = $helper->get_client_ip();//終端IP
$data["total_fee"] = $order_data['total'] * 100;//總金額
$data["trade_type"] = "APP";//交易類型
$data["sign"] = $helper->getSign($data, $pay_config['partner']);//簽名
$xml = $helper->arrayToXml($data);
$response = $helper->postXmlCurl($xml, $pay_url);
//將微信返回的結果xml轉成數組
$responseArr = $helper->xmlToArray($response);
if(isset($responseArr["return_code"]) && $responseArr["return_code"]=='SUCCESS' && isset($responseArr['result_code']) && $responseArr["result_code"]=='SUCCESS'){
$data_pay["appid"] = $pay_config['appid'];
$data_pay["noncestr"] = $nonce_str;
$data_pay["package"] = "Sign=WXPay";
$data_pay["partnerid"] = $pay_config['mch_id'];
$data_pay["prepayid"] = $responseArr['prepay_id'];
$data_pay["timestamp"] = time();
$data_pay["sign"] = $helper->getSign($data_pay, $pay_config['partner']);//二次簽名
$this->response = array('status'=>0, 'msg'=>'success', 'data'=>$data_pay);
}else{
$return_msg = $responseArr['err_code_des'];
$this->response = array('status'=>0, 'msg'=>$return_msg, 'data'=>$responseArr);
}
}
//helper.php
<?php
class WxPayHelper{
/**
* 驗證簽名
* @param array $data
* @param string $key
* @return string
*/
function getVerifySign($data, $key)
{
$String = $this->formatParameters($data, false);
//簽名步驟二:在string後加入KEY
$String = $String . "&key=" . $key;
//簽名步驟三:MD5加密
$String = md5($String);
//簽名步驟四:所有字元轉為大寫
$result = strtoupper($String);
return $result;
}
function formatParameters($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if($k=="sign"){
continue;
}
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar;
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
/**
* 得到簽名
* @param object $obj
* @param string $api_key
* @return string
*/
function getSign($obj, $api_key)
{
foreach ($obj as $k => $v)
{
$Parameters[strtolower($k)] = $v;
}
//簽名步驟一:按字典序排序參數
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//簽名步驟二:在string後加入KEY
$String = $String."&key=".$api_key;
//簽名步驟三:MD5加密
$result = strtoupper(md5($String));
return $result;
}
/**
* 獲取指定長度的隨機字元串
* @param int $length
* @return Ambigous <NULL, string>
*/
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
* @param array $arr
* @return string
*/
function arrayToXml($arr)
{
header("Content-type: text/xml");
$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方式提交xml到對應的介面url
*
* @param string $xml 需要post的xml數據
* @param string $url url
* @param bool $useCert 是否需要證書,預設不需要
* @param int $second url執行超時時間,預設30s
* @throws WxPayException
*/
function postXmlCurl($xml, $url, $second=30, $useCert=false, $sslcert_path='', $sslkey_path='')
{
$ch = curl_init();
//設置超時
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
//設置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求結果為字元串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
if($useCert == true){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗
//設置證書
//使用證書:cert 與 key 分別屬於兩個.pem文件
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $sslcert_path);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $sslkey_path);
}
//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);
return false;
}
}
/**
* 獲取當前伺服器的IP
* @return Ambigous <string, unknown>
*/
function get_client_ip()
{
if (isset($_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 = "127.0.0.1";
}
return $cip;
}
/**
* 將數組轉成uri字元串
* @param array $paraMap
* @param bool $urlencode
* @return string
*/
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轉數組
* @param unknown $xml
* @return mixed
*/
function xmlToArray($xml)
{
//將XML轉為array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
}
?>
//非同步通知
/**
* 微信消息地址
*/
public function weixin_notify()
{
libraries_include("wxpay/", "WxPayHelper.app.php");
$helper = new WxPayHelper();
//微信配置
$pay_config = $this->config->item("WEIXIN_PAY_NEED");
$xml = file_get_contents("php://input");
if(!$xml){
exit('<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[ERROR]]></return_msg></xml>');
}
$wx_back = $helper->xmlToArray($xml);
if(empty($wx_back)){
exit('<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[ERROR]]></return_msg></xml>');
}
$checkSign = $helper->getVerifySign($wx_back, $pay_config['partner']);
//驗證簽名
if($checkSign==$wx_back['sign']){
if (isset($wx_back['result_code']) && $wx_back['result_code']=='SUCCESS') {
$requestReturnData = file_get_contents("php://input");
//商戶訂單號
$out_trade_no = $wx_back['out_trade_no'];
//第三方訂單編號
$third_order_num = $wx_back["transaction_id"];
//交易狀態
$trade_status = $wx_back['result_code'];
//訂單金額 保留小數點後兩位
$total_fee = sprintf("%.2f", $wx_back['total_fee']/100);
//公司業務處理
//處理後同步返回給微信
exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
}
}
exit('<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[ERROR]]></return_msg></xml>');
}