微信支付封装php,2016封装的微信支付,太好用了

//

//

// %s

//

//

// 0

// ";

// if(!empty( $keyword ))

// {

// $msgType = "text";

// $contentStr = "Welcome to wechat world!";

// $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

// echo $resultStr;

// }else{

// echo "Input something...";

// }

//

// }else {

// echo "";

// exit;

// }

// }

public function getaccesstoken()

{

$access_token = S('access_token');

if(!empty($access_token)){

return $access_token;

}else{

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret;;

$ret = $this->curlget($url);

$ret = json_decode($ret,true);

S('access_token',$ret['access_token'],7100);

return $ret['access_token'];

}

}

//根据openid拉取用户信息

public function userinfo($openid){

$access_token = $this->getaccesstoken();

$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";

$userinfo = json_decode($this->curlget($url),true);

return $userinfo;

}

function p($arr){

echo "";

print_r($arr);

}

function curlget($url)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

$output = curl_exec($ch);

curl_close($ch);

return $output;

}

public function jingmo($url,$code,$state){

if($code){

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";

$arrtemp = json_decode($this->curlget($url),true);

$ret = $this->getuserinfo($arrtemp['openid'],$arrtemp['access_token']);

return $ret;

}else{

$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$url."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";

header("Location: $url");

}

}

public function getuserinfo($openid,$user_access_token){

$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$user_access_token."&openid=".$openid."&lang=zh_CN";

$userinfo = json_decode($this->curlget($userurl),true);

return $userinfo;

}

public function getjsapi_ticket(){

$js_ticket = S("js_ticket");

if(!empty($js_ticket)){

return $js_ticket;

}else{

$access_token = $this->getaccesstoken();

$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";

$res = json_decode($this->curlget($url),true);

S("js_ticket",$res['ticket'],7100);

return $res['ticket'];

}

}

public function getjssign(){

echo "";

$js_ticket = $this->getjsapi_ticket();

$time = time();

$arr = array(

'noncestr'=>md5($time),

'jsapi_ticket'=>$js_ticket,

'timestamp'=>$time,

'url'=>'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],

);

$this->p($arr);

ksort($arr);

$string="";

$i=1;

foreach($arr as $key=>$val){

if($i==1){

$string.=$key."=".$val;

}else{

$string.="&".$key."=".$val;

}

$i++;

echo $i;

}

// echo $string;

$signature =strtolower($string);

$signature = sha1($string);

$jsconfig = array(

'debug'=>true,

'appId'=>$this->appid,

'timestamp'=>$time,

'nonceStr'=>md5($time),

'signature'=>$signature,

' jsApiList'=>"['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ']",

);

$json = json_encode($jsconfig);

return $json;

}

//生成大写签名

function createsign($arr,$biaozhi){

ksort($arr);

$string="";

$i=1;

foreach($arr as $key=>$val){

if($i==1){

$string.=$key."=".$val;

}else{

$string.="&".$key."=".$val;

}

$i++;

}

$signtemp = "$string&key=" . $this->wechatkey;

$sign = strtoupper(MD5($signtemp));

$arr[$biaozhi] = $sign;

//$json = json_encode($arr);

return $arr;

}

//支付

//发起支付

public function sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url)

{

$time = time();

$arr = array(

'appid' => $this->appid,

'mch_id' => $this->mchid,

'nonce_str' => md5($time),

'body' => "kjhk",

'out_trade_no' => $out_trade_no,

'total_fee' => $total_fee,

'spbill_create_ip' => "127.0.0.1",

'notify_url' => $notify_url,

'trade_type' => "JSAPI",

'openid' => $openid,

);

$biaozhi ='sign';

$arr = $this->createsign($arr,$biaozhi);

$xml = "";

foreach ($arr as $key=>$val) {

$xml.="".$val."".$key.">";

}

$xml.="";

$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

$resxml = $this->postCurlTransfer($url,$xml);

$res = simplexml_load_string($resxml);

$cutime = time();

$jsapiarr=array(

'appId'=>$this->appid,

'timeStamp'=>"$cutime",

'nonceStr'=>md5($cutime),

'package'=>"prepay_id=".$res->prepay_id,

'signType'=>"MD5",

);

$biaozhi = 'paySign';

$jsapi = $this->createsign($jsapiarr,$biaozhi);

// $jsapijson = json_encode($jsapi);

return $jsapi;

}

public function getpaysign(){

$time = time();

$arr = array(

'appid'=>$this->appid,

'mch_id'=>$this->mchid,

'nonce_str'=>md5($time),

'body'=>"sdf",

'out_trade_no'=>"2016".$time,

'total_fee'=>1,

'spbill_create_ip'=>"127.0.0.1",

'notify_url'=>"http://www.baidu.com",

'trade_type'=>"JSAPI",

);

ksort($arr);

$string="";

$i=1;

foreach($arr as $key=>$val){

if($i==1){

$string.=$key."=".$val;

}else{

$string.="&".$key."=".$val;

}

$i++;

}

$string.="&key=".$this->wechatkey;

$sign = strtoupper(md5($string));

$arr['sign'] = $sign;

return $arr;

}

function Post($curlPost,$url){

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_NOBODY, true);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);

$return_str = curl_exec($curl);

curl_close($curl);

return $return_str;

}

private function postCurlTransfer($url, $data)

{

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$str = curl_exec($curl);

curl_close($curl);

return $str;

}

//发送模板消息

public function sendtpl($openid, $url, $template_id, $content, $topcolor="#FF0000")

{

$arr = array(

'touser' => $openid,

'template_id' => $template_id,

'url' => $url,

'topcolor' => $topcolor,

'data' => $content,

);

$arrjson = json_encode($arr);

$accesstoken = $this->getAccessToken();

$sendurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$accesstoken;

return $this->postCurlTransfer($sendurl, $arrjson);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值