原文地址 http://baijunyao.com/article/78 白俊遥博客http://baijunyao.com
公众号支付是指在微信app中访问的页面通过js直接调起微信支付;
因此页面必须是在微信中打开的;
示例项目:https://github.com/baijunyao/thinkphp-bjyadmin
一:设置域名
登录微信公众平台;
微信支付中设置支付授权目录;把域名改为自己的;
注意最后是有一个斜线的 /
设置授权域名;
二:导入sdk
/ThinkPHP/Library/Vendor/Weixinpay
好吧;还是没忍住要吐槽;鹅厂的sdk那酸爽谁用谁知道;项目中的sdk是我根据官方文档重构精简打造而成的;
需要注意的是170行处的商品数据需要根据业务实际情况从数据库中获取;
1
2
3
4
5
6
7
8
9
10
|
$openid
=
$result
[
'openid'
];
// 订单数据 请根据订单号out_trade_no 从数据库中查出实际的body、total_fee、out_trade_no、product_id
$order
=
array
(
'body'
=>
'test'
,
// 商品描述(需要根据自己的业务修改)
'total_fee'
=>1,
// 订单金额 以(分)为单位(需要根据自己的业务修改)
'out_trade_no'
=>
$out_trade_no
,
// 订单号(需要根据自己的业务修改)
'product_id'
=>
'1'
,
// 商品id(需要根据自己的业务修改)
'trade_type'
=>
'JSAPI'
,
// JSAPI公众号支付
'openid'
=>
$openid
// 获取到的openid
);
|
三:配置项
/Application/Common/Conf/config.php
1
2
3
4
5
6
7
|
'WEIXINPAY_CONFIG'
=>
array
(
'APPID'
=>
''
,
// 微信支付APPID
'MCHID'
=>
''
,
// 微信支付MCHID 商户收款账号
'KEY'
=>
''
,
// 微信支付KEY
'APPSECRET'
=>
''
,
// 公众帐号secert (公众号支付专用)
'NOTIFY_URL'
=>
'http://baijunyao.com/Api/Weixinpay/notify'
, // 接收支付状态的连接
),
|
在微信公众平台和微信支付平台凑齐上面这些参数;
四:支付方法
/Application/Api/Controller/WeixinpayController.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 公众号支付 必须以get形式传递 out_trade_no 参数
* 示例请看 /Application/Home/Controller/IndexController.class.php
* 中的wexinpay_js方法
*/
public
function
pay(){
// 导入微信支付sdk
Vendor(
'Weixinpay.Weixinpay'
);
$wxpay
=
new
\Weixinpay();
// 获取jssdk需要用到的数据
$data
=
$wxpay
->getParameters();
// 将数据分配到前台页面
$assign
=
array
(
'data'
=>json_encode(
$data
)
);
$this
->assign(
$assign
);
$this
->display();
}
|
需要html的配合:/tpl/Api/Weixinpay/pay.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>Title</
title
>
</
head
>
<
body
style
=
"text-align: center;"
>
<
button
onclick
=
"getOrder()"
>购买</
button
>
<
jquery
/>
<
script
>
function onBridgeReady(){
var data={$data};
WeixinJSBridge.invoke(
'getBrandWCPayRequest', data,
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
// 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}else{
alert(res.err_code+res.err_desc+res.err_msg); // 显示错误信息
}
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
</
script
>
</
body
>
</
html
>
|
调用示例:/Application/Home/Controller/IndexController.class.php 中的wexinpay_js方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* 微信 公众号jssdk支付
*/
public
function
wexinpay_js(){
// 此处根据实际业务情况生成订单 然后拿着订单去支付
// 用时间戳虚拟一个订单号 (请根据实际业务更改)
$out_trade_no
=time();
// 组合url
$url
=U(
'Api/Weixinpay/pay'
,
array
(
'out_trade_no'
=>
$out_trade_no
));
// 前往支付
redirect(
$url
);
}
|
五:异步接收通知
/Application/Api/Controller/WeixinpayController.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* notify_url接收页面
*/
public
function
notify(){
// 导入微信支付sdk
Vendor(
'Weixinpay.Weixinpay'
);
$wxpay
=
new
\Weixinpay();
$result
=
$wxpay
->notify();
if
(
$result
) {
// 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单id
}
}
|
//*********************************增加curl_get_contents函数的分割线****************************
如果是整合到自己的项目中;则需要在自己的公共函数中增加curl_get_contents;
/Application/Common/Common/function.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 使用curl获取远程数据
* @param string $url url连接
* @return string 获取到的数据
*/
function
curl_get_contents(
$url
){
$ch
=curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
//设置访问的url地址
// curl_setopt($ch,CURLOPT_HEADER,1); //是否显示头部信息
curl_setopt(
$ch
, CURLOPT_TIMEOUT, 5);
//设置超时
curl_setopt(
$ch
, CURLOPT_USERAGENT,
$_SERVER
[
'HTTP_USER_AGENT'
]);
//用户访问代理 User-Agent
curl_setopt(
$ch
, CURLOPT_REFERER,
$_SERVER
[
'HTTP_HOST'
]);
//设置 referer
curl_setopt(
$ch
,CURLOPT_FOLLOWLOCATION,1);
//跟踪301
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, 1);
//返回结果
$r
=curl_exec(
$ch
);
curl_close(
$ch
);
return
$r
;
}
|
//*************************关于签名错误的补充*********************************
如果出现签名错误;
可以使用官方的 微信公众平台支付接口调试工具
跟自己生产的签名对比;
然后对比配置;查找不一致的地方;
//*****************关于不知道怎么查看异步发过来的数据的补充*****************
2016.10.28:
好多童鞋在问支付后;不知道怎么查看接收到的支付状态通知;
这里做个补充;首先;我们的服务器必须是外网可以正常访问到的;
必须注意不能有 登录或者权限之类的拦截;
另外补充一个简单的查看收到的内容的方法用于测试;
五:异步接收通知
/Application/Api/Controller/WeixinpayController.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* notify_url接收页面
*/
public
function
notify(){
// 下面的file_put_contents是用来简单查看异步发过来的数据 测试完可以删除;
file_put_contents
(
'./notify.text'
, json_encode(
$_POST
));
// 导入微信支付sdk
Vendor(
'Weixinpay.Weixinpay'
);
$wxpay
=
new
\Weixinpay();
$result
=
$wxpay
->notify();
if
(
$result
) {
// 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单id
}
}
|
本文为白俊遥原创文章,转载无需和我联系,但请注明来自白俊遥博客http://baijunyao.com