<?php
class WeixinPay{
protected $appid;
protected $mch_id;
protected $key;
protected $openid;
function __construct($appid,$openid,$mch_id,$key){
$this->appid=$appid;
$this->openid=$openid;
$this->mch_id=$mch_id;
$this->key=$key;
}
public function pay(){
$return=$this->weixinapp();
return $return;
}
private function unifiedorder(){
$url='https:
$parameters=array(
'appid'=>$this->appid,
'mch_id'=>$this->mch_id,
'nonce_str'=>$this->createNoncestr(),
'body'=>'测试',
'out_trade_no'=>'2015450806125346',
'total_fee'=>floatval(0.01*100),
'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
'notify_url'=>'http:
'openid'=>$this->openid,
'trade_type'=>'JSAPI'
);
$parameters['sign']=$this->getSign($parameters);
$xmlData=arrayToXml($parameters);
$return=xmlToArray(postXmlSSLCurl($xmlData,$url,60));
return $return;
}
private function weixinapp(){
$unifiedorder=$this->unifiedorder();
$parameters=array(
'appId'=>$this->appid,
'timeStamp'=>''.time().'',
'nonceStr'=>$this->createNoncestr(),
'package'=>'prepay_id='.$unifiedorder['prepay_id'],
'signType'=>'MD5'
);
$parameters['paySign']=$this->getSign($parameters);
return $parameters;
}
private 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;
}
private function getSign($Obj){
foreach ($Obj as $k => $v){
$Parameters[$k] = $v;
}
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
$String = $String."&key=".$this->key;
$String = md5($String);
$result_ = strtoupper($String);
return $result_;
}
private function formatBizQueryParaMap($paraMap, $urlencode){
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v){
if($urlencode)
{
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar;
if (strlen($buff) > 0){
$reqPar = substr($buff, 0, strlen($buff)-1);
}
return $reqPar;
}
}