导入SDK
第一步是导入集成好的SDK,路径是/ThinkPHP/Library/Vendor/Weixin/WeChatPay
配置项
导入以后,主要的还是配置我们微信支付所需要的参数/Application/Common/Conf/config.php,我的项目中将微信支付参数单独配置,然后在config中使用LOAD_EXT_CONFIG加载数据配置
调用
这块因为是调用微信的JSAPI接口来调起支付,所以我使用ajax来请求接口:/Application/Home/Controller/PayController.class.php
public function pay()
{
//部分代码逻辑省略
vendor(‘Weixin.WeChatPay’);
$config = C(‘WECHAT’);
m
o
d
e
l
=
n
e
w
W
e
C
h
a
t
P
a
y
(
model = new WeChatPay(
model=newWeChatPay(config[‘mchId’],
c
o
n
f
i
g
[
′
a
p
p
I
d
′
]
,
config['appId'],
config[′appId′],config[‘apiKey’]);
$result =
m
o
d
e
l
−
>
c
r
e
a
t
e
J
s
B
i
z
P
a
c
k
a
g
e
(
model->createJsBizPackage(
model−>createJsBizPackage(user[‘wx_openid’],
o
r
d
e
r
[
′
m
o
n
e
y
′
]
,
order['money'],
order[′money′],order[‘order_sn’],‘syz’,U(‘pay/notify’,[],[],true),time());
$this->ajaxReturn([‘code’ => 2000,‘result’ => $result]);
}
调用createJsBizPackage方法来进行支付的时候,需要传递用户的openid,订单金额,订单号,以及订单名称,回调地址,以及时间戳
既然是js调用当然也需要html页面的支持:/Application/Home/View/Pay/index.html
微信支付回调是异步的:/Application/Home/Controller/PayController.class.php,通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒,异步直接处理业务逻辑就可以
/**
-
微信支付回调
*/
public function notify()
{
$postStr = file_get_contents(“php://input”);
p o s t = j s o n d e c o d e ( j s o n e n c o d e ( s i m p l e x m l l o a d s t r i n g ( post = json_decode(json_encode(simplexml_load_string( post=jsondecode(jsonencode(simplexmlloadstring(postStr, ‘SimpleXMLElement’, LIBXML_NOCDATA)), true);
if($post[‘result_code’] == ‘SUCCESS’ && $post[‘return_code’] == ‘SUCCESS’) {
// 支付成功扭转订单状态
$order_sn = p o s t [ ′ o u t t r a d e n o ′ ] ; / / post['out_trade_no']; // post[′outtradeno′];//order_sn = ‘SY201805121145102321380644’;//完成你的业务逻辑 修改状态之类的 //给微信返回 防止重复通知 return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
}else{
return [‘msg’ => ‘支付失败’];
}
}