/**
* Combine WeChat app payment to get prepayid
* @param int $order_num
*/
private function _wxpay_request($order_num = 0)
Payment E libraries_include ("wxpay/", "wxpayhelper.app.php");
// Pay interface initiate URL
$ pay_url = $ this- & gt; config- & gt; "pay_url"); Notice Address
$notify_url = $this->config->item("WEIXIN_NOTIFY_URL"); use using using using using using using using using using out out off out out out out of's'' being ‐ ‐ ‐ ‐‐‐ – ("WEIXIN_PAY_NEED");
$helper = order_data = $this->order_model-> get_one($order_num);
$data["appid"] ;//Brief description of the product or payment order
$data["mch_id"] ["notify_url"] = $notify_url;//Notification address
$data["out_trade_no"] = $order_data["order_num"];//Merchant order number
$data["spbill_create_ip"] = $helper->get_client_ip ();//Terminal IP
$data["total_fee"] ["sign"] = $helper->postXmlCurl($xml, $pay_url); responseArr ["return_code"]) && $responseArr["return_code"]=='SUCCESS' && isset($responseArr['result_code']) && $responseArr["result_code"]=='SUCCESS'){
$data_pay ["appid"] = $pay_config['appid'];
$data_pay["noncestr"] $data_pay["partnerid"] = $ pay_config['mch_id']; $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
class WxPayHelper{
/**
* Verify signature
* @param array $data
* @param string $key
* @return string
*/
function getVerifySign($data, $key)
{
$String = $this->formatParameters($data, false);
//Signature step 2: Add KEY after string Four: Convert all characters to uppercase
$result = strtoupper($String);
’ out out out out out out out out out out out out out out out out out out in ’ in in in in ’ s in ’ s in 1 in 1 ’s in 1 in 1 ‐ in ‐ ‐ t = "";
ksort($paraMap) ;
foreach ($paraMap as $k => $v) {
$v = urlencode($v); " $reqPar = substr($buff, 0 , Strlen ($ BUFF) -1);
}
Return $ REQPAR;
}
/**
* Get signature
* @param object $obj
* @param string $api_key
* @return string
*/
Function GetSign ($ obj, $ api_key) {
Foreach ($ Obj as $ k = > = $this-> ;formatBizQueryParaMap($Parameters, false);
//Signature step two: add KEY after string
(md5 ($ string));
Return $ Result;}}
/**
* Get a random string of specified length
* @param int $length
* /
Function Getrandchar ($ LENGTH) {
$ Str = NULL; OPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
$ max = Strlen ($ Strpol) -1;
for ($ i = 0; $ i & lt; $ length; $ i ++) {
$ Str. = $ Strpol [Rand (0, $ max)]; // RAND ($ min ($ min)] ,$max) Generate a random integer between min and max
}
return $str; header("Content-type: text/xml");
$xml = '
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."".$key.">";
}
else
$xml.="<".$key.">".$key.">";
}
$xml.= '
return $xml;
}
/**
* Submit xml to the corresponding interface url in post mode
*
* @param string $xml The xml data required for post
* @param string $url url
* @param bool $useCert Whether a certificate is required, not required by default
* @param int $second URL execution timeout, default 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
*/
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;
}
/**
* Convert array to uri string
* @param array $paraMap
* @param bool $urlencode
*/
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 to array
* @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;
}
}
?>
//异步通知
/**
* WeChat message address
*/
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('
}
$wx_back = $helper->xmlToArray($xml);
if(empty($wx_back)){
exit('
}
$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('
}
}
exit('
}