微信扫码模式二实现支付以及回调

浏览:12741 发布日期:2016/06/03 分类:功能实现 关键字: 微信扫码模式二支付以及回调实现
微信扫码支付模式二 以及回调实现
控制器:
<?php
namespace Home\Controller;
use Think\Controller;
/*********微信扫码模式二支付*********/
class WxpayController extends Controller {
//在类初始化方法中,引入相关类库
public function _initialize() { //这里是微信官方的demo文件 我将其他文件都引入到api文件中了 自己可以一个一个引入随自己喜好
Vendor('Wxpay.lib.WxPay#Api');
}
//支付
public function index(){
R('Public/header');
$host='http://'.$_SERVER['HTTP_HOST']; //获取当前域名
$notify = new \NativePay(); //实例化微信类
$WxPayConfig=new \WxPayConfig(); //实例化微信类
$input = new \WxPayUnifiedOrder(); //实例化微信类
$input->SetBody($_POST['body']); //订单描述
$input->SetOut_trade_no($_POST['out_trade_no']);//账单号
$input->SetTotal_fee($_POST['total_fee']*100); //付款金额微信是以分为单位的,所以传过来的参数乘以100
$input->SetNotify_url($host."/Wxpay/notify.html");//回调地址
$input->SetTrade_type("NATIVE");
$input->SetProduct_id($_POST['product_id']);//商品id
$result = $notify->GetPayUrl($input); //扫码模式二
$codeurl = $result["code_url"];
$this->assign('codeurl',$codeurl);
//这里可以加入你的订单入库代码
$this->display();
}

//支付成功后回调
public function notify(){
$xml = $GLOBALS['HTTP_RAW_POST_DATA']; //返回的xml
file_put_contents(dirname(__FILE__).'/xml.txt',$xml); //记录日志 支付成功后查看xml.txt文件是否有内容 如果有xml格式文件说明回调成功
//file_get_contents(dirname(__FILE__).'/xml.txt');
$xmlObj=simplexml_load_string($xml,'SimplexmlElement',LIBxml_NOCDATA);
$xmlArr=json_decode(json_encode($xmlObj),true);
$out_trade_no=$xmlArr['out_trade_no']; //订单号
$total_fee=$xmlArr['total_fee']/100; //回调回来的xml文件中金额是以分为单位的
$result_code=$xmlArr['result_code']; //状态
if($result_code=='SUCCESS'){ //数据库操作
//处理数据库操作 例如修改订单状态 给账户充值等等
echo 'SUCCESS'; //返回成功给微信端 一定要带上不然微信会一直回调8次
exit;
}else{ //失败
return;
exit;
}
}

}

模板文件:
<div style="text-align:center;"><h3>请扫描下面的二维码完成充值</h3></div><br/>

<img style="width:150px;height:150px; margin:0 auto 260px;display:block;" alt="扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data={$codeurl}"/>




Wxpay.zip 放在 ThinkPHP\Library\Vendor 下面 在Wxpay\lib里面的WxPay.Config.php配置自己的信息\Wxpay\cert 的文件自己去自己的商户平台下载放进去

附件 Wxpay.zip ( 279.72 KB 下载:431 次 )

评论( 相关
后面还有条评论,点击查看>>