标签:
/**
* 组合微信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>‘);
}
标签:
原文地址:http://www.cnblogs.com/zhaoxueru/p/5832910.html