文件
mashangyoujiu/ThinkPHP/Library/Util/Wx/Wxpaysdk.class.php

376 行
13 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
/**
* 微信支付类
*/
namespace Util\Wx;
class Wxpaysdk
{
const UFORDER_URL = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; //获取预支付URL,prepayid.
private $mch_id; //微信支付商户号
private $sub_mch_id; //微信支付子商户号
private $sub_openid; //微信支付子商户号
private $sub_appid; //微信支付子商户号
private $key; //微信支付商户KEY
private $openid; //微信支付用户号
private $appid;
private $appsecret;
private $out_trade_no;
private $total_fee; //总金额
private $notify_url; //ND地址
private $trade_type; //JSAPI
private $curl_timeout;
//动态参数
private $parameters;
//非必填参数,商户可根据实际情况选填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
//$unifiedOrder->setParameter("device_info","XXXX");//设备号
//$unifiedOrder->setParameter("attach","XXXX");//附加数据
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
//$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
//$unifiedOrder->setParameter("openid","XXXX");//用户标识
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
//微信CURL响应
public $response; //微信返回的响应
public $result; //返回参数,类型为关联数组
//
private $prepay_id; //获取prepay_id
public function __construct($options) {
$this->mch_id = isset($options['mch_id']) ? $options['mch_id'] : '';
$this->appid = isset($options['appid']) ? $options['appid'] : '';
$this->out_trade_no = isset($options['out_trade_no']) ? $options['out_trade_no'] : '';
$this->total_fee = isset($options['total_fee']) ? $options['total_fee']*100 : '';
$this->notify_url = isset($options['notify_url']) ? $options['notify_url'] : '';
$this->trade_type = isset($options['trade_type']) ? $options['trade_type'] : 'JSAPI';
$this->curl_timeout = isset($options['curl_timeout']) ? $options['curl_timeout'] : '30';
$this->body = isset($options['body']) ? $options['body']:'';
$this->key = isset($options['key'])?$options['key']:'';
$this->openid = isset($options['openid'])?$options['openid']:'';
if(!empty($options['sub_mch_id'])){
$this->sub_mch_id = isset($options['sub_mch_id'])?$options['sub_mch_id']:'';
}
if(!empty($options['sub_appid'])){
$this->sub_appid = isset($options['sub_appid'])?$options['sub_appid']:'';
}
if(!empty($options['sub_openid'])){
$this->sub_openid = isset($options['sub_openid'])?$options['sub_openid']:'';
}
}
/**
* 获取prepay_id
*/
function getPrepayId() {
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$this->postXml($url); //追入获取链接
/*print_r($this->response);
exit;*/
$this->result = $this->xmlToArray($this->response);
$this->prepay_id = $this->result["prepay_id"];
if(isset($this->result['result_code'])&&$this->result['result_code']=='SUCCESS' && isset($this->result['return_code'])&&$this->result['return_code']=='SUCCESS'){
// print_r($this->result);
$url = $this->getJSAPI();
return $url;
}else{
return $this->result;
}
}
/**
* 作用:设置JSAPI_prepay_id
*/
function setPrepayId($prepayId) {
$this->prepay_id = $prepayId;
}
/**
* 作用:设置jsapi的参数
*/
public function getJSAPI() {
if(!empty($this->sub_appid)){
$wOpt['appId'] = $this->sub_appid;
}else{
$wOpt['appId'] = $this->appid;
}
$timeStamp = time();
$wOpt['timeStamp'] = $timeStamp;
$wOpt['nonceStr'] = $this->createNoncestr(8);
$wOpt['package'] = 'prepay_id=' . $this->prepay_id;
$wOpt['signType'] = 'MD5';
ksort($wOpt, SORT_STRING);
foreach ($wOpt as $key => $v) {
$string .= "{$key}={$v}&";
}
/*echo $string;
exit;*/
$string .= "key=" . $this->key;
// echo $string . "<br><br>";
$wOpt['paySign'] = strtoupper(md5($string));
// echo $wOpt['paySign'];
$string .='&paySign='.$wOpt['paySign'];
$wOpt['url'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/wxpay.php?$string".'&notify_url='.$this->notify_url;
// echo $url;exit;
return $wOpt;
}
/**
* 作用:设置请求参数
*/
function setParameter($parameter, $parameterValue) {
$this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
}
/**
* 作用:设置标配的请求参数,生成签名,生成接口参数xml
*/
function createXml() {
$this->parameters["appid"] = $this->appid; //公众账号ID
$this->parameters["mch_id"] = $this->mch_id; //商户号
$this->parameters['out_trade_no'] = $this->out_trade_no;//商户订单号
$this->parameters['total_fee'] = $this->total_fee;//支付金额
$this->parameters["nonce_str"] = $this->createNoncestr(); //随机字符串
$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR']; //终端ip
$this->parameters['body'] = $this->body;
$this->parameters['trade_type'] = $this->trade_type;
$this->parameters['notify_url'] = $this->notify_url;
if(!empty($this->openid)){
$this->parameters['openid'] = $this->openid;
}
if(!empty($this->sub_mch_id)){
$this->parameters['sub_mch_id'] = $this->sub_mch_id;
}
if(!empty($this->sub_appid)){
$this->parameters['sub_appid'] = $this->sub_appid;
}
if(!empty($this->sub_openid)){
$this->parameters['sub_openid'] = $this->sub_openid;
}
$this->parameters["sign"] = $this->getSign($this->parameters); //签名
return $this->arrayToXml($this->parameters);
}
/**
* 作用:post请求xml
*/
function postXml($url) {
$xml = $this->createXml();
//dump($xml);
$this->response = $this->postXmlCurl($xml, $url, $this->curl_timeout);
//dump($this->response);
return $this->response;
}
//常用工具
function trimString($value) {
$ret = null;
if (null != $value) {
$ret = $value;
if (strlen($ret) == 0) {
$ret = null;
}
}
return $ret;
}
/**
* 作用:产生随机字符串,不长于32位
*/
public function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 作用:格式化参数,签名过程需要使用
*/
function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
//$buff .= strtolower($k) . "=" . $v . "&";
$buff .= $k . "=" . $v . "&";
}
$reqPar;
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
/**
* 作用:生成签名
*/
public function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'</br>';
//签名步骤二:在string后加入KEY
$String = $String . "&key=" . $this->key;
//echo "【string2】".$String."</br>";
//签名步骤三:MD5加密
$String = md5($String);
//echo "【string3】 ".$String."</br>";
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
//echo "【result】 ".$result_."</br>";
return $result_;
}
/**
* 作用:array转xml
*/
function arrayToXml($arr) {
// print_r($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;
}
/**
* 作用:将xml转为array
*/
public function xmlToArray($xml) {
//将XML转为array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
/**
* 作用:以post方式提交xml到对应的接口url
*/
public function postXmlCurl($xml, $url, $second = 30) {
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOP_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);
curl_close($ch);
//返回结果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return false;
}
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
private function http_post($url, $xml, $post_file = false) {
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
/**
* 作用:使用证书,以post方式提交xml到对应的接口url
*/
function postXmlSSLCurl($xml, $url, $second = 30) {
$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);
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, WxPayConf_pub::SSLCERT_PATH);
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);
//post提交方式
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
//返回结果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return false;
}
}
}