1.首先,我们要辞官方下载人家做好的DEMO,链接是https://pay.weixin.qq.com/wiki/doc/api/jsapi.PHP?chapter=11_1
2.下载好往后,我们可以把里面的东西放到app\Wechat里(文件夹须要新建)
3.打开lib/WxPay.Config.php,把里面四个主要参数改本钱身的,下面有两个证书路径,如果有退款什么的才须要,光支付就不管他

const APPID = '在你的"大众年夜众号里看';
const MCHID = '商户ID';
const KEY = '自己在"大众号设置的32位';
const APPSECRET = '也在"大众号里看';
4.在resource/view/下新建文件夹wechat,里面视图就把app\Wechat里面的jsapi.php改名为jsapi.blade.php放进去,这里要说一下,引用的文件路径,必须是我这种格式“../app/Wechat”下才能精确读取,而且把所有app/Wechat下的各种文件都打开,里面只要用到路径,你就在require_once后面加个“../app/Wechat/”
<?php
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once\公众../app/Wechat/lib/WxPay.Api.php\"大众;
require_once\"大众../app/Wechat/example/WxPay.JsApiPay.php\公众;
require_once'../app/Wechat/example/log.php';
//①、获取用户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid(\公众/wechat?money=\"大众.$money); //传参要这样传,还要改一下example/WxPay.JsApiPay.php文件
//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody(\"大众微信支付\"大众);
$input->SetAttach(\公众微信支付\"大众);
$input->SetOut_trade_no(WxPayConfig::MCHID.date(\"大众YmdHis\公众));
$input->SetTotal_fee($money);
$input->SetTime_start(date(\"大众YmdHis\公众));
$input->SetTime_expire(date(\"大众YmdHis\"大众, time() + 600));
$input->SetGoods_tag(\"大众\"大众);
$input->SetNotify_url(\"大众http://aaa.com/wechat/notify\"大众);//这里填你的回调路径,是绝对路径,例如http://aaa.com/wechat/notify
$input->SetTrade_type(\"大众JSAPI\公众);
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
// echo '<font color=\"大众#f00\"大众><b>统一下单支付单信息</b></font><br/>';
// printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);
//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();
//③、在支持成功回调关照中处理成功之后的事宜,见 notify.php
/
把稳:
1、当你的回调地址不可访问的时候,回调关照会失落败,可以通过查询订单来确认支付是否成功
2、jsapi支付时须要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信"大众年夜众平台“网页授权接口”,
参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
/
?>
<html>
<head>
<meta http-equiv=\"大众content-type\"大众 content=\公众text/html;charset=utf-8\公众/>
<meta name=\"大众viewport\"大众 content=\公众width=device-width, initial-scale=1\公众/>
<title>微信支付</title>
<script src=\"大众http://7xwdxi.com1.z0.glb.clouddn.com/lib/js/jquery-2.0.3.min.js\公众></script>
<script type=\"大众text/javascript\"大众>
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<?php echo$jsApiParameters; ?>,
function(res){
//这底下的东西便是上面$input->SetNotify_url(\"大众http://aaa.com/notify\公众);这个传过来的
WeixinJSBridge.log(res.err_msg);
// alert(res.err_code+res.err_desc+res.err_msg);
if (res.err_msg == \"大众get_brand_wcpay_request:ok\"大众) { //如果微信支付成功
// message: \"大众微信支付成功!\"大众
alert(\"大众支付成功!
\"大众);
window.location.href=\"大众/wechat/do;//你成功后要跳转的页面或掌握器
}elseif(res.err_msg == \公众get_brand_wcpay_request:cancel\"大众){ //如果取消微信支付
alert(\"大众您已取消支付\"大众);
}
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == \"大众undefined\"大众){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}elseif (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall();
}
}
</script>
<script type=\"大众text/javascript\"大众>
//获取共享地址
function editAddress()
{
WeixinJSBridge.invoke(
'editAddress',
<?php echo$editAddress; ?>,
function(res){
var value1 = res.proviceFirstStageName;
var value2 = res.addressCitySecondStageName;
var value3 = res.addressCountiesThirdStageName;
var value4 = res.addressDetailInfo;
var tel = res.telNumber;
alert(value1 + value2 + value3 + value4 + \"大众:\"大众 + tel);
}
);
}
window.onload = function(){
if (typeof WeixinJSBridge == \公众undefined\公众){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', editAddress, false);
}elseif (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', editAddress);
document.attachEvent('onWeixinJSBridgeReady', editAddress);
}
}else{
editAddress();
}
};
</script>
</head>
<body>
<div class=\公众title-bar\公众>
<span><a href=\"大众/home/student/pay\公众><img src=\公众http://7xwdxi.com1.z0.glb.clouddn.com/images/back-icon.png\"大众></a></span>
<h1>充值</h1>
</div>
<br/>
<font color=\公众#9ACD32\"大众><b>该笔订单支付金额为<span style=\"大众color:#f00;font-size:50px\"大众><?php $sum=$money/100; ?>{{$sum}}</span>元钱</b></font><br/><br/>
<div align=\公众center\"大众>
<button style=\"大众width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;\"大众 type=\"大众button\公众 onclick=\公众callpay()\"大众 >立即支付</button>
</div>
</body>
</html>
5.写掌握器,新建掌握器叫WechatController,紧张是回调函数里写好你的逻辑(官方无回调文档,很坑)
...
publicfunction index(Request $request){
if(!is_numeric(Input::get('money'))){
return Redirect::back();
}
$money = Input::get('money')10;
//由于微信的钱是按分为单位,以是传进来,先10,然后微信会回调回来,在实行一次这个方法,以是再10,如果是5元,传个5就变成500分,便是5元钱了
return view(\"大众home.student.jsapi\"大众)
->withMoney($money);//把money作为参数带到jsapi.blade.php
}
publicfunction notify(){ //这里是你的回调函数,这个很坑,官方都没有文档的
publicfunction notify(Request $request){
$streamData = isset($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; //拿到微信回调回来的信息判断支付成功没
if(empty($streamData)){
$streamData = file_get_contents('php://input');
}
if($streamData!=''){
$streamData=xmlToArray($streamData);
$Data=json_encode($streamData);
Log::debug('Alipay notify post data verification fail.', [ //写入做事器文档,你不加这个也行
'data' => $Data.'xxxxxx'
]);
if($streamData['return_code'] == 'SUCCESS' && $streamData['result_code'] == 'SUCCESS'){ //支付成功
try { //开始事务
//支付成功,你要干些什么都写这里,例如增加余额的操作什么的
} catch (Exception $e) {
//如果try里面的东西涌现问题的话,进行数据库回滚
throw$e;
}
}
}else{
$ret = false; //支付失落败
}
}
}
6.注册路由(这里最好用any的方法,由于发过去是post,回来的是get)
Route::any(‘wechat’,'WechatController@index');
Route::any(‘wechat/notify’,'WechatController@notify');
7.app/Wechat/example/WxPay.JsApiPay.php里面的getopenid方法还要改一下,否则不能传参数
...
publicfunction GetOpenid($addUrl=\公众\公众)
{
//通过code得到openid
if (!isset($_GET['code'])){
//触发微信返回code码
$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$addUrl);
$url = $this->__CreateOauthUrlForCode($baseUrl);
Header(\公众Location: $url\公众);
exit();
} else {
//获取code码,以获取openid
$code = $_GET['code'];
$openid = $this->getOpenidFromMp($code);
return$openid;
}
}
...8.这样程序这块基本就完事了,访问时你要花的钱要这么传http://aaa.com/wechat?money=5(这样的话是充5元钱)
9.下面就要说说微信"大众年夜众平台上,一共须要修正3个地方
(1)在开拓者工具下面,修正网页账号(否则通过不了Oauth2.0验证会报redirect_url缺点)
(2)验证你的URL,URL便是这个验证文件在项目中的位置,如果放到public文件夹下,你就直接写http://nba.com/wx_sample.php
验证文件如下:
wx_sample.php
<?php
/
wechat php test
/
//define your token
define(\公众TOKEN\公众, \公众weixin\"大众);//和你在"大众年夜众号中填的TOKEN相同
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
publicfunction valid()
{
$echoStr = $_GET[\"大众echostr\"大众];
//valid signature , option
if($this->checkSignature()){
echo$echoStr;
exit;
}
}
publicfunction responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS[\公众HTTP_RAW_POST_DATA\"大众];
//extract post data
if (!empty($postStr)){
/ libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself /
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = \"大众<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>\"大众;
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;
}
}
privatefunction checkSignature()
{
// you must define TOKEN by yourself
if (!defined(\公众TOKEN\公众)) {
thrownew Exception('TOKEN is not defined!');
}
$signature = $_GET[\公众signature\"大众];
$timestamp = $_GET[\公众timestamp\公众];
$nonce = $_GET[\公众nonce\公众];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
(3)修正开拓配置(支付授权目录和测试授权目录),这里要把稳,如果你的支付路径是http://nba.com/index.php/wechat,你就填http://nba.com/index.php/,把稳做后的“/”去掉。