一直在做微信,遇到過很多坑,今天寫出來,讓大家少走點彎路,第一次寫文字,有點土,如果有不懂的童鞋,可以聯絡我QQ1034100429,或者發私訊。
getaccesstoken 取得access_token
//啟動付款
public function sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url)
sendtpl 發送範本訊息<?php <br />
/**<br>
* 由 PhpStorm 建立。 <br>
* 使用者:管理員<br>
* 日期:2016/4/11<br>
*時間:22:43<br>
*/<br>
namespace Org;<br>
class Weixin<br>
{<br>
private $appid; //微信公號的appid<br>
private $appsecret; //微信公眾號碼的appsecret<br>
private $mchid; //微信公眾號的商家號碼<br>
private $wechatkey; //微信公號的付款金鑰<br>
private $token;<br>
public function __construct()<br>
{<br>
$this->appid = C('WEIXIN_APPID');<br>
$this->appsecret = C("WEIXIN_APP_SECRET");<br>
$this->mchid = C("WEIXIN_MCHID");<br>
$this->wechatkey = C("WEIXIN_KEY");<br>
$this->token = C('WEIXIN_APP_TOKEN');<br>
<br>
}<br>
public function chushi()<br>
{<br>
echo $this->getaccesstoken();<br>
}<br>
public function checkSignature()<br>
{<br>
$signature = $_GET['signature'];<br>
$timestamp = $_GET['timestamp'];<br>
$nonce = $_GET['nonce'];<br>
$token = $this->token;<br>
$arrtemp = array($token, $timestamp, $nonce);<br>
sort($arrtemp, SORT_STRING);<br>
$arrtemp = implode($arrtemp);<br>
$arrtemp = sha1($arrtemp);<br>
if ($arrtemp == $signature) {<br>
return true;<br>
} else {<br>
return false;<br>
}<br>
}<br>
public function valid()<br>
{<br>
$echoStr = $_GET['echostr'];<br>
if ($this->checkSignature()) {<br>
echo $echoStr;<br>
exit;<br>
}<br>
}<br>
////根據回傳的資訊,做出對應的處理<br>
// public function responseMsg()<br>
// {<br>
// $postStr = file_get_contents('php://input');<br>
// if (!empty($postStr)){<br>
// /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,<br>
// the best way is to check the validity of xml by yourself */<br>
// libxml_disable_entity_loader(true);<br>
// $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br>// $fromUsername = $postObj->FromUserName;<br>
// $toUsername = $postObj->ToUserName;<br>
// $keyword = trim($postObj->Content);<br>
// $msgType = $postObj->MsgType;<br>
// $Event=$postObj->Event;<br>
// $time = time();<br>
// $textTpl = "<xml><br>
// <tousername></tousername><br>
// <fromusername></fromusername><br>
// <createtime>%s</createtime><br>
// <msgtype></msgtype><br>
// 內容><br>
// <funcflag>0</funcflag><br>
// </xml>」且;<br>
// if(!empty( $keyword ))<br>
// {<br>
// $msgType = "text";<br>
// $contentStr =「歡迎來到微信世界!」;<br>
// $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);<br>
// echo $resultStr;<br>
// }其他{<br>
// echo 「輸入上一些內容...」;<br>
// }<br>
//<br>
// }否則{<br>
// echo "";<br>
// 退出;<br>
// }<br>
// }<br>
公用函數 getaccesstoken()<br>
{<br>
$access_token = S('access_token');<br>
if(!empty($access_token)){<br>
<br>
返回 $access_token;<br>
}其他{<br>
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid。 “&秘密=”。
$ret = $this->curlget($url);<br> $ret = json_decode($ret,true);<br>
S('access_token',$ret['access_token'],7100);<br>
<br>
返回 $ret['access_token'];<br>
}<br>
}<br>
//依照openid拉取使用者資訊<br>
公用函數使用者資訊($openid){<br>
$access_token = $this->getaccesstoken();<br>
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";<br>
$userinfo = json_decode($this->curlget($url),true);<br>
回 $userinfo;<br>
}<br>
函數 p($arr){<br>
echo "<meta>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">";<br>
print_r($arr);<br>
}<br>
函數curlget($url)<br>
{<br>
$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($ch, CURLOPT_HEADER, 0);<br>
$輸出= curl_exec($ch);<br>
curl_close($ch);<br>
則為$輸出;<br>
}<br>
公用函數 jingmo($url,$code,$state){<br>
如果($代碼){<br>
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=". $code ."&grant_type=authorization_code";<br>
$arrtemp = json_decode($this->curlget($url),true);<br>
$ret = $this->getuserinfo($arrtemp['openid'],$arrtemp['access_token']);<br>
則返回 $ret;<br>
}其他{<br>
$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";<br>
header("位置:$url");<br>
}<br>
}<br>
公用函數 getuserinfo($openid,$user_access_token){<br>
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$user_access_token."&openid=".$openid."&lang=zh_CN";<br>
$userinfo = json_decode($this->curlget($userurl),true);<br>
回 $userinfo;<br>
}<br>
公用函數 getjsapi_ticket(){<br>
$js_ticket = S("js_ticket");<br>
if(!empty($js_ticket)){<br>
返回 $js_ticket;<br>
}其他{<br> $access_token = $this->getaccesstoken();<br>
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";<br>
$res = json_decode($this->curlget($url),true);<br>
S("js_ticket",$res['ticket'],7100);<br>
則返回 $res['ticket'];<br>
}<br>
}<br>
公用函數 getjssign(){<br>
echo "<meta>";<br>
$js_ticket = $this->getjsapi_ticket();<br>
$時間 = 時間();<br>
$arr = 陣列(<br>
'noncestr'=>md5($time),<br>
'jsapi_ticket'=>$js_ticket,<br>
'時間戳記'=>$時間,<br>
'url'=>'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],<br>
<br>
);<br>
$this->p($arr);<br>
ksort($arr);<br>
$string="";<br>
$i=1;<br>
foreach($arr as $key=>$val){<br>
如果($i==1){<br>
$string.=$key."=".$val;<br>
}其他{<br>
$string.="&".$key."=".$val;<br>
}<br>
$i++;<br>
迴聲 $i;<br>
}<br>
// 回顯 $string;<br>
$signature=strtolower($string);<br>
$signature = sha1($string);<br>
$jsconfig=陣列(<br>
'調試'=>true,<br>
'appId'=>$this->appid,<br>
'時間戳記'=>$時間,<br>
'nonceStr'=>md5($time),<br>
'已簽名'=>$簽署,<br>
' jsApiList'=>"['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ']",<br>
);<br>
$json=json_encode($jsconfig);<br>
返回 $json;<br>
}<br>
//產生大寫簽章<br>
函數 createsign($arr,$biaozhi){<br>
ksort($arr);<br>
$string="";<br>
$i=1;<br>
foreach($arr as $key=>$val){<br>
如果($i==1){<br> $string.=$key."=".$val;<br>
}其他{<br>
$string.="&".$key."=".$val;<br>
}<br>
$i++;<br>
}<br>
$signtemp=「$string&key=」。 $this->wechatkey;<br>
$sign = strtoupper(MD5($signtemp));<br>
$arr[$biaozhi] = $sign;<br>
//$json=json_encode($arr);<br>
返回 $arr;<br>
}<br>
//付款<br>
<br>
//啟動付款<br>
公用函數 sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url)<br>
{<br>
$時間 = 時間();<br>
$arr = 陣列(<br>
'appid'=> $this->appid,<br>
'mch_id'=>; $這個->mchid,<br>
'nonce_str'=>; md5($時間),<br>
'身體' => “kjhk”,<br>
'out_trade_no'=>; $out_trade_no,<br>
'total_fee' =>; $total_fee,<br>
'spbil_create_ip' =>; “127.0.0.1”,<br>
'notify_url' =>; $notify_url,<br>
'trade_type' =>; “JSAPI”,<br>
'openid' =>; $openid,<br>
);<br>
$biaozhi ='sign';<br>
$arr = $this->createsign($arr,$biaozhi);<br>
$xml = 「<xml>」;<br>
foreach($arr as $key=>$val){<br>
$xml.="".$val."".$key.">";<br>
}<br>
$xml.="</xml>";<br>
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";<br>
$resxml = $this->postCurlTransfer($url,$xml);<br>
$res = simplexml_load_string($resxml);<br>
$cutime = time();<br>
$jsapiarr=陣列(<br>
'appId'=>$this->appid,<br>
'timeStamp'=>"$cutime",<br>
'nonceStr'=>md5($cutime),<br>
'package'=>"prepay_id=".$res->prepay_id,<br>
'signType'=>"MD5",<br>
);<br> $biaozhi = 'paySign';<br>
$jsapi = $this->createsign($jsapiarr,$biaozhi);<br>
// $jsapijson = json_encode($jsapi);<br>
返回 $jsapi;<br>
}<br>
公用函數 getpaysign(){<br>
$時間 = 時間();<br>
$arr = 陣列(<br>
'appid'=>$this->appid,<br>
'mch_id'=>$this->mchid,<br>
'nonce_str'=>md5($time),<br>
'body'=>"sdf",<br>
'out_trade_no'=>"2016".$time,<br>
'total_fee'=>1,<br>
'spbill_create_ip'=>"127.0.0.1",<br>
'notify_url'=>"http://www.baidu.com",<br>
'trade_type'=>"JSAPI",<br>
);<br>
ksort($arr);<br>
$string="";<br>
$i=1;<br>
foreach($arr as $key=>$val){<br>
如果($i==1){<br>
$string.=$key."=".$val;<br>
}其他{<br>
$string.="&".$key."=".$val;<br>
}<br>
$i++;<br>
}<br>
$string.="&key=".$this->wechatkey;<br>
$sign = strtoupper(md5($string));<br>
$arr['sign'] = $sign;<br>
返回 $arr;<br>
}<br>
函數 Post($curlPost,$url){<br>
$curl=curl_init();<br>
curl_setopt($curl, CURLOPT_URL, $url);<br>
curl_setopt($curl, CURLOPT_HEADER, false);<br>
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<br>
curl_setopt($curl, CURLOPT_NOBODY, true);<br>
curl_setopt($curl, CURLOPT_POST, true);<br>
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);<br>
$return_str = curl_exec($curl);<br>
curl_close($curl);<br>
返回 $return_str;<br>
}<br>
私有函數 postCurlTransfer($url, $data)<br>
{<br>
$curl=curl_init();<br>
curl_setopt($curl, CURLOPT_URL, $url);<br>
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($curl, CURLOPT_POST, 1);<br>
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);<br> $str = curl_exec($curl);<br>
curl_close($curl);<br>
<br>
return $str;<br>
}<br>
//傳送範本訊息<br>
public function sendtpl($openid, $url, $template_id, $content, $topcolor="#FF0000")<br>
{<br>
$arr = array(<br>
'touser' => $openid,<br>
'template_id' => $template_id,<br>
'url' => $url,<br>
'topcolor' => $topcolor,<br>
'data' => $content,<br>
);<br>
$arrjson = json_encode($arr);<br>
$accesstoken = $this->getAccessToken();<br>
$sendurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$accesstoken;<br>
return $this->postCurlTransfer($sendurl, $arrjson);<br>
}<br>
}</pre><div class="contentsignin">登入後複製</div></div>
上面的是微信支付簡單封裝
若發起支付,請先對應微信公眾號的資料。
包括微信支付目錄
我這裡是
http://網域//index.php/Home/Index/
好像是這個
取得access_token的過程<?php <br />
/**<br>
* 由 PhpStorm 建立。 <br>
* 用戶:sks<br>
* 日期:16/7/8<br>
* 時間:下午3:54<br>
*/<br>
namespace HomeController;<br>
use CommonControllerHomebaseController;<br>
Class WeixinController extends HomebaseController{<br>
public function zhanshi(){<br>
$weixin = new OrgWeixin;<br>
$code = $_GET['code'];<br>
$state = $_GET['state'];<br>
$url="";<br>
$userinfo = $weixin->jingmo($url, $code, $state);<br>
session('userinfo',$userinfo);<br>
if($code) {<br>
header("Location: $state");<br>
}else{<br>
}<br>
}<br>
}
公用函數
function.phpfunction getuserinfo(){ //取得使用者資訊<br>
import("weixin");<br>
$weixin = new OrgWeixin;<br>
$info = session('userinfo');<br>
if($info){<br>
return $info;<br>
die;<br>
}else {<br>
$state = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];<br>
$code = '';<br>
$url = 'http://' . $_SERVER['HTTP_HOST'] . U('Home/Weixin/zhanshi');<br>
$url = urlencode($url);<br>
$weixin->jingmo($url, $code, $state);<br>
}<br>
}
測試支付<?php <br />
/**<br>
* Created by PhpStorm.<br>
* User: sks<br>
* Date: 16/7/8<br> * Time: 下午3:29<br>
*/<br>
namespace HomeController;<br>
use CommonControllerHomebaseController;<br>
use ThinkPage;<br>
Class IndexController extends HomebaseController{<br>
<br>
//測試支付<br>
public function zhifu()<br>
{<br>
$userinfo = getuserinfo(); //取得微信使用者資訊以呼叫公用函數<br>
$openid = $userinfo['openid'];<br>
$time = time();<br>
$title = "我支付";<br>
$out_trade_no = date('Y',time()).$time.rand(1000,2000);<br>
$total_fee = 1;<br>
$notify_url = "http://網域/index.php/Home/Index/paynotify";//回呼網址<br>
$weixin = new OrgWeixin;<br>
$result = $weixin->sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url);<br>
$this->assign('result',$result);<br>
$this->display();<br>
}<br>
//這裡是付款結果通知<br>
public function paynotify(){<br>
$weixin = new OrgWeixin;<br>
$xml = file_get_contents("php://input");<br>
$p = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);<br>
$arr = $this->object_array($p);<br>
$out_trade_no = $arr['out_trade_no'];<br>
$transaction_id= $arr['transaction_id'];<br>
$openid = $arr['openid'];<br>
$sign = $arr['sign'];<br>
//處理支付結果<br>
unset($arr['sign']);<br>
$biaozhi = "sign";<br>
$ret = $weixin->createsign($arr, $biaozhi);<br>
if($ret['sign'] === $sign) {<br>
$aa = "成功";<br>
} else {<br>
$aa = "失敗";<br>
}<br>
$file = './logweixin.txt';//要寫入檔案的檔案名稱(可以是任一檔案名稱),若檔案不存在,將會建立一個<br>
$content = $out_trade_no."***".$transaction_id."第一次寫入的內容n";<br>
if($f = file_put_contents($file, $content,FILE_APPEND)){// 這個函數支援版本(PHP 5)<br>
echo "寫入成功。<br>";<br>
}<br>
//這裡處理付款結果的過程<br>
echo "success";<br>
}<br>
<br>
public function getaccesstoken(){<br>
$weixin = new OrgWeixin;<br>
echo $weixin->getaccesstoken();<br>
<br>
}<br>
}