底层封装
<?php
namespace systemextendwxpay;
use systemcoreCurl;
use systemcoreApp;
/**
* 微信支付
*/
class WxPay {
private $values = array();
// CURL对象
private $curl;
// xml数据
private $xml;
// 发送返回结果
public $result;
// 商户微信支付参数
private $seller = array();
// 微信支付URL
private $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
public function __construct() {
$conf = App::config('pay');
$this->seller['appid'] = $conf['wxpay']['appid'];
$this->seller['mch_id'] = $conf['wxpay']['mch_id'];
$this->curl = new Curl();
$this->curl->url($this->url);
}
/**
* 绑定参数
* @param string $openid 支付者openID
* @param string $attch 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
* @param string $body 商品描述
* @param int $total_fee 订单总金额,单位为分
* @param string $out_trade_no 商户系统内部的订单号
* @param string $detail 商品详情
*/
public function bindParam($openid, $attch, $body, $total_fee, $out_trade_no, $detail) {
// 参数赋值
$this->values['appid'] = $this->seller['appid'];
$this->values['mch_id'] = $this->seller['mch_id'];
$this->values['attach'] = $attch;
$this->values['openid'] = $openid;
$this->values['body'] = $body;
$this->values['device_info'] = 'WEB';
$this->values['openid'] = $openid;
$this->values['nonce'] = $this->string(8);
$this->values['openid'] = $openid;
$this->values['out_trade_no'] = $out_trade_no;
$this->values['detail'] = $detail;
$this->values['total_fee'] = $total_fee;
$this->values['time'] = (string)time();
// 组装XML
$this->xml = "
<xml>
<appid><![CDATA[%s]]></appid>
<attach><![CDATA[%s]]></attach>
<body><![CDATA[%s]]></body>
<mch_id><![CDATA[%s]]></mch_id>
<device_info><![CDATA[%s]]></device_info>
<nonce_str><![CDATA[%s]]></nonce_str>
<notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url>
<out_trade_no><![CDATA[%s]]></out_trade_no>
<spbill_create_ip>14.23.150.211</spbill_create_ip>
<total_fee><![CDATA[%s]]></total_fee>
<trade_type>JSAPI</trade_type>
<detail><![CDATA[%s]]></detail>
<openid><![CDATA[%s]]></openid>
</xml>";
$this->xml = sprintf($this->xml, $this->values['appid'], $this->values['attach'], $this->values['body'], $this->values['mch_id'], $this->values['device_info'], $this->values['nonce'], $this->values['out_trade_no'], $this->values['total_fee'], $this->values['detail'], $this->values['openid']);
}
/**
* 生成随机字符串
* @return 返回随机字符串
*/
private function string($length) {
$string = "0123456789abcdefghigklmnopqrstuvwxyz0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";
$textlength = strlen ( $string ) - 1;
$data = '';
for($i = 0; $i < $length; $i ++) {
$data .= $string [mt_rand ( 0, $textlength )];
}
return $data;
}
/**
* 生成签名
*/
public function createSign() {
$this->xml = simplexml_load_string($this->xml);
// 数组转换
$array = array();
foreach($this->xml as $key => $value) {
$array[$key]=$value;
}
// 字典排序
ksort($array);
// 字符串组装
$string = '';
foreach ($array as $key => $value) {
$string .= $key.'='.$value.'&';
}
// MD5加密
$this->values['sign'] = strtoupper(md5($string."key=ASD20150723CMD123CHENSHUORECWORD"));
$this->xml->addChild("sign", $this->values['sign']);
$this->xml = $this->xml->asXML();
}
/**
* 返回支付参数 h5页面配置参数
* @return array
*/
public function output() {
$this->createSign();
$data = $this->curl->pay($this->xml);
$this->result = simplexml_load_string($data);
$this->values['prepay_id'] = $this->result->prepay_id;
$string = "appId=".$this->values['appid']."&nonceStr=".$this->values['nonce']."&package=prepay_id=".$this->values['prepay_id']."&signType=MD5&timeStamp=".$this->values['time']."&key=ASD20150723CMD123CHENSHUORECWORD";
$this->values['signPay'] = strtoupper(md5($string));
return $this->values;
}
}
?>