NotifyController.class.php是异步
<?php
namespace Home\Controller;
use Think\Controller;
use Think\Hook;
class NotifyController extends Controller {
//初始化
public function _initialize()
{
vendor('WxPayPubHelper.WxPayPubHelper');
}
// 支付回调
public function notify(){
$log_name= "./Public/Pay/notify_url.log";//log文件路径
$wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'appid'))->getField('value');
$wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'mchid'))->getField('value');
$wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'paysignkey'))->getField('value');
$wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'appsecret'))->getField('value');
//存储微信的回调
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
//使用通用通知接口
$notify = new \Notify_pub($wechatinfo);
$notify->saveData($xml);
//验证签名,并回应微信。
//对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
//微信会通过一定的策略(如30分钟共8次)定期重新发起通知,
//尽可能提高通知的成功率,但微信不保证通知最终能成功。
if($notify->checkSign() == FALSE){
$notify->setReturnParameter("return_code","FAIL");//返回状态码
$notify->setReturnParameter("return_msg","签名失败");//返回信息
}else{
$notify->setReturnParameter("return_code","SUCCESS");//设置返回码
}
$returnXml = $notify->returnXml();
if($notify->checkSign() == TRUE)
{
if (($notify->data["return_code"] == "SUCCESS")&&($notify->data["result_code"] == "SUCCESS")) {
$where['number'] = $notify->data["out_trade_no"];
$data['wx_number'] = $notify->data["transaction_id"];
$data['deal_time'] = $notify->data["time_end"];
$data['is_pay'] = 1;
$data['access'] = 1;
// 支付完成后清除session的所有信息
session('Cart',null);
// 清除总价
session('total',null);
// 清除数量
session('num',null);
// 商品的id
session('shop_id',null);
if(!D('order')->where($where)->save($data)){
file_put_contents($log_name,"【支付成功 , 数据库修改失败信息】:\n".json_encode($notify->data)."\n", FILE_APPEND);
}
}else{
file_put_contents($log_name,"【支付失败通知】:\n".json_encode($notify->data)."\n", FILE_APPEND);
}
}
}
}
//WxpayController是支付方法
<?php
namespace Home\Controller;
use Think\Controller;
use Think\Hook;
class WxpayController extends Controller {
//初始化
public function _initialize()
{
if(session('userinfo.openid')){
vendor('WxPayPubHelper.WxPayPubHelper');
}else{
cookie('controller',CONTROLLER_NAME);
cookie('action',ACTION_NAME);
$this->redirect('Base/getsession');
}
}
/*
前台给支付传一个id即可
*/
// 统一支付
public function pay(){
if($_GET['orderid']){
$orderinfo = D('order')->find($_GET['orderid']);
if(($orderinfo['is_pay'] == 0) && ($orderinfo['status'] == 1)){
$wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appid'))->getField('value');
$wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'mchid'))->getField('value');
$wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'paysignkey'))->getField('value');
$wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appsecret'))->getField('value');
//统一支付
$unifiedOrder = new \UnifiedOrder_pub($wechatinfo);
$unifiedOrder->setParameter("openid",session('userinfo.openid'));
$unifiedOrder->setParameter("body",'谢谢光顾');
$unifiedOrder->setParameter("out_trade_no",$orderinfo['number']);
$unifiedOrder->setParameter("total_fee",$orderinfo['cash_fee']);
$unifiedOrder->setParameter("notify_url", C('HTTP_HOST') . '/index.php/Home/Notify/notify');
$unifiedOrder->setParameter("trade_type","JSAPI");
$prepay_id = $unifiedOrder->getPrepayId();
$jsApi = new \JsApi_pub($wechatinfo);
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();
$this->assign('jsApiParameters',$jsApiParameters)->assign('money',$orderinfo['cash_fee']/1000);
$this->display();
}
}
}
// 退款
public function refundmoney() {
if($_GET['orderid']){
$orderinfo = D('order')->find($_GET['orderid']);
// 订单状态必须是已支付,申请退款,订单未结束
if(($orderinfo['refund'] == 1) && ($orderinfo['is_pay'] == 1) && ($orderinfo['status'] == 1)){
// 查出商户信息
$wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appid'))->getField('value');
$wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'mchid'))->getField('value');
$wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'paysignkey'))->getField('value');
$wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appsecret'))->getField('value');
$sslcert = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'sslcert'))->getField('value');
$sslkey = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'sslkey'))->getField('value');
$wechatinfo['sslcert_path'] = '/cert/apiclient_'.$orderinfo['mpid'].'_cert.pem';
$wechatinfo['sslkey_path'] = '/cert/apiclient_'.$orderinfo['mpid'].'_key.pem';
$dirpath = explode('\App',dirname(__FILE__));
$sslcertpath = $dirpath[0].'\ThinkPHP\Library\Vendor\WxPayPubHelper\cert\apiclient_'.$orderinfo['mpid'].'_cert.pem';
$sslkeypath = $dirpath[0].'\ThinkPHP\Library\Vendor\WxPayPubHelper\cert\apiclient_'.$orderinfo['mpid'].'_key.pem';
file_put_contents($sslcertpath, isset($sslcert) ? $sslcert : '');
file_put_contents($sslkeypath, isset($sslkey) ? $sslkey : '');
// 退款
$refundpay = new \Refund_pub($wechatinfo);
$refundpay->setParameter('transaction_id', $orderinfo['wx_number']);
$refundpay->setParameter('out_refund_no', $orderinfo['cash_fee'].$orderinfo['wx_number']);
$refundpay->setParameter('total_fee', $orderinfo['total_fee']);
$refundpay->setParameter('refund_fee', $orderinfo['cash_fee']);
$refundpay->setParameter('op_user_id', '920518');
$result = $refundpay->getResult();
$log_name= "./Public/Pay/refund_url.log";//log文件路径
// 成功
if (isset($result['return_code']) && isset($result['result_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
$data['id'] = $_GET['orderid'];
$data['wxrefund_number'] = $result['refund_id'];
$data['refund'] = 2;
$data['status'] = 2;
if(D('order')->save($data)){
$return['return_code'] = 200;
$return['return_msg'] = '退款成功';
}else{
file_put_contents($log_name,"【退款成功 , 数据库修改失败通知】:\n".json_encode($result)."\n", FILE_APPEND);
$return['return_code'] = 201;
$return['return_msg'] = '退款成功,但是数据库修改失败';
}
}else{
file_put_contents($log_name,"【退款失败通知】:\n".json_encode($result)."\n", FILE_APPEND);
$return['return_code'] = 400;
$return['return_msg'] = '退款失败';
}
unlink($sslcertpath);
unlink($sslkeypath);
$this->ajaxReturn($return);
}
}
}
// 二维码支付
public function qrcodepay(){
if($_GET['orderid'] && is_int($_GET['orderid'])){
$orderinfo = D('order')->find($_GET['orderid']);
if(!$orderinfo){
$return['return_code'] = 201;
$return['return_msg'] = '订单不存在';
}else{
if(($orderinfo['is_pay'] == 0) && ($orderinfo['status'] == 1)){
$wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appid'))->getField('value');
$wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'mchid'))->getField('value');
$wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'paysignkey'))->getField('value');
$wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appsecret'))->getField('value');
//统一支付
$unifiedOrder = new \UnifiedOrder_pub($wechatinfo);
$unifiedOrder->setParameter("body",'谢谢光顾');
$unifiedOrder->setParameter("out_trade_no",$orderinfo['number']);
$unifiedOrder->setParameter("total_fee",$orderinfo['cash_fee']);
$unifiedOrder->setParameter("notify_url", C('HTTP_HOST') . '/index.php/Home/Notify/notify');
$unifiedOrder->setParameter("trade_type","NATIVE");
$unifiedOrder->setParameter("product_id",$orderinfo['number']);
$prepay_url = $unifiedOrder->getPrepayUrl();
$data['qrcodeimage'] = 'http://paysdk.weixin.qq.com/example/qrcode.php?data='.urlencode($prepay_url);
$return['return_code'] = 200;
$return['return_msg'] = '订单二维码';
$return['return_data'] = $data;
$this->ajaxReturn($return);
}else{
if($orderinfo['is_pay'] == 1){
$return['return_code'] = 201;
$return['return_msg'] = '订单不可重复支付';
}
}
}
}else{
$return['return_code'] = 400;
$return['return_msg'] = '订单格式错误';
}
}
public function qecodeindex(){
$this->display();
}
// 创建订单
public function createorder(){
$money = I('post.money');
$type = I('post.type');
$mpid = I('post.mpid');
$data['number'] = time('YmdHis').rand(100,999).session('userinfo.id');
$data['total_fee'] = $money*100;
$data['cash_fee'] = $money*100;
$data['type'] = 1;
$data['mpid'] = session('userinfo.mpid');
$data['status'] = 1;
$data['userid'] = session('userinfo.id');
$orderid = D('order')->add($data);
if($orderid){
$order['id'] = $orderid;
$return['return_code'] = 200;
$return['return_msg'] = '添加成功';
$return['return_data'] = $order;
}else{
$return['return_code'] = 400;
$return['return_msg'] = '添加失败';
}
$this->ajaxReturn($return);
}
// 饭厅生成二维码
public function getqrcode(){
vendor('phpqrcode.qrcodecreate');
$qrcodeimage = new \QRcodeCreate();
$result = $qrcodeimage->projectqrcode();
$QR = imagecreatefromgd(file_get_contents($result));
//输出图片
// imagepng($QR, 'qrcode.png');
// imagedestroy($QR);
// return '<img src="qrcode.png" alt="使用微信扫描支付">';
file_put_contents('./Public/getqrcode',$QR);
}
}
tp3.2.rar
存放微信支付扩展