/**
* 組合微信app付款 取得prepayid
* @param int $order_num
*/
private function _wxpay_request($order_num = 0)
{
check_order_num($order_num);
//引入微信支付類
libraries_include("wxpay/", "WxPayHelper.app.php");
("PAY_URL");
//通知位址
$notify_url = $this->config->item("WEIXIN_NOTIFY_URL");
"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[
$data["mch_id"] = $pay_config['mch_id'];//商家編號
$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"] $data["trade_type"] $data["trade_type"] = $helper-> getSign($data, $pay_config['partner']);//簽章
$xml = $helper->arrayToXml XmlCurl($xml, $pay_url) ;
//將微信回傳的結果xml轉換為陣列
$ Arr["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["partnerid"] = $pay_config['mch_id'];
$data_pay["prepaypayid "] = $responseArr['prepay_id'];
$data_pay["timestamp"] = time();
$data_pay["sign"] = $helper->getSign($data_pay, $pay_config['partner']);//二次簽章
$this->response = array('status'=>; 0, 'msg'=>'成功', 'data'=>$data_pay);
}else{
$this->response = array('status'=>0, 'msg'=>$return_msg, 'data'=>$responseArr
}
//helper.php
/**&#&*/ // //處理處理同步回傳至微信微信微信
class WxPayHelper{
/**
* 驗證簽章
* @param array $data
* @param string $key
* @return c*/
function getVerifySign($data, $00)
function getVerifySign($data, $00)
function getVerifySign($data, $this) ($data, false);
//簽章步驟二:string後加入KEY
$String = $String . "&key=" . $key;
//簽章步驟三:MD5步驟四:所有字元都轉為大寫
$result = strtoupper($String);
return $result;
@ {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if($k=="sign"){
if ($urlencode) {
$v = urlencode($ ); $buff .= $k . "=" . $v . "&";
}
$reqPar;
qPar = substr($buff, 0, strlen($ buff) - 1);
}
return $reqPar;
}
, $p.
{
foreach ($obj as $k => $v)
{
$Parameters[strtolower($k)] = $v;
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//簽章步驟二:string後加入KEY
$String = $String."&key=".$api md5($String) );
return $result;
}
/**
* 取得簽名
* @param object $obj
* @param string $api_key
* @return string */
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol) -1;
for($i=0;$i $str.=$strPol[rand(0,$max);//max($min,$/max);於min與max兩個數字之間的一個隨機整數
}
return $str;
}
/$& &/arrat#)//arr;
header("內容類型:text/xml");
$ $key=>$val)
{
if (is_numeric($val))🎠
$xml.="”.$val。 」".$key.">";
}
CDATA[".$val."]]>".$ key.">";
}
$xml.= '';
函數postXmlCurl($xml, $url, $second= 30, $useCert=false, $sslcert_path='', $sslkey_path='')
{
$ch = curl_init ($ch, CURLOPT_TIMEOUT, $second);
curl_setopt( $ch,CURLOPT_URL, $url);
//設定header
curl _setopt($ch, CURLOPT_HEADER, FAL curl _setopt($ch, CURLOPT_HEADER, FAL curl _setopt($ch, CURLOPT_HEADER, FALSE) ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) ;
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,C URLOPT_SSL_VERIFYST, 如果($useCert == true){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格驗證
//設定憑證
($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $sslcert_path);
curl_setopt($ch,CURLOPT_SSLKEY TYPE,'PEM'); ;
}
//post提交方式
curl_setopt($ch, CURLOPTPT_POST, TRUE) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//運行curl
//回傳結果
if($data){
curl_close($ch);
$data;
} else {
$error = curl_errno($ch);
) 返回
}
}
/**
* 取得目前伺服器的IP
* @return Ambigous
*/
function get_client_ip()
$cip = $_SERVER[' DDR '];
} elseif (getenv("REMOTE_ADDR")) {
$cip = getenv("REMOTE_ADDR"); IP")) {
$cip = getenv(" HTTP_CLIENT _IP");
} else {
$cip = "127.0.0.1";
turn
/**
* 將陣列轉成uri字串
* @param array $paraMap
* @param bool $urlencode
* @param bool $urlencode */
function formatBizQueryParaMap($paraMap, $ urlencode)
{
";
ksort($paraMap);
foreach ($paraMap as $k => $v)
{
$v = urlencode ($ v);
er ($k) 。 $reqPar = substr($buff, 0, strlen($buff )-1);
}
返回$reqPar ;
}
{
//XML 將轉換為陣列
$array_data = json_decode(json_encode(simplexml_load_string ) ($xml, 'SimpleXMLElement) ', LIBXML_NOCDATA)) , true);
return $array_data;
}
🠜}
//非同步通知
公共功能weixin_notify()
{
//微訊號設定
$pay_config = $this->config->item( “WEIXIN_PAY_NEED”);
$xml = file_get_contents("php://input");
if(!$xml){
exit('
}
$wx_back = $helper->xmlToAr if(空白($wx) _back)){
退出('
}
}
'partner']);
//驗證上簽名
if (isset($wx_back['result_code']) && $wx_back['結果碼' ]=='SUCCESS') {
$requestReturnData = file_get_contents("php://input");
//商戶訂單號
$ out_trade_no = $wx_back['out_trade_no'];
//第三方訂單編號
$third_order_num = $wx_back["trans action_id"];
//交易狀態
$trade_status = $wx_back['result_code '];
//使用上使用折扣值, $wx_back['total_fee']/100);
//公司業務處理
]>');
}
exit('