native.php
首先,用GET方法把订单号传到example/native.php里,查出订单金额,
在统一下单接口里写入订单金额 :$input->SetTotal_fee("$goods_amount");
然后回调地址:$input->SetNotify_url("http://www.lmtly.com/WxpayAPI/example/notify.php");
支付类型: $input->SetTrade_type("NATIVE");
订单号:$input->SetProduct_id($order_sn);
如下所示
$input = new WxPayUnifiedOrder();
$input->SetBody("老码头旅游");
$input->SetAttach("test");
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee("$goods_amount");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://www.lmtly.com/WxpayAPI/example/notify.php");
$input->SetTrade_type("NATIVE");
$input->SetProduct_id($order_sn);
$result = $notify->GetPayUrl($input);
$url2 = $result["code_url"];
生成二维码
<div class="img"><img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data=<?php echo urlencode($url2);?>" style="width:150px;height:150px;"/>
//扫码后不断发送ajxf去查询支付状态
<script src="http://www.jq22.com/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
var order_sn = '<?php echo $order_sn; ?>';
my=setInterval(function(){
loda()
},1000);
function loda(){
$.ajax({
type:'POST',
url:"orderquery.php",
data:{out_trade_no:order_sn},
datatype:"String",
success:function(data){
if(data==1){
clearInterval(my);
$("#msg").html('支付成功');
}
}
});
}
</script>
======================================================================
notify.php
然后在notify.php里接收支付后的回调信息
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
define('IN_ECS', true);
require( '../../includes/init.php');
require_once "../lib/WxPay.Api.php";
require_once '../lib/WxPay.Notify.php';
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
class PayNotifyCallBack extends WxPayNotify
{
//查询订单
public function Queryorder($transaction_id)
{
$input = new WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$result = WxPayApi::orderQuery($input);
Log::DEBUG("query:" . json_encode($result));
if(array_key_exists("return_code", $result)
&& array_key_exists("result_code", $result)
&& $result["return_code"] == "SUCCESS"
&& $result["result_code"] == "SUCCESS")
{
return true;
}
return false;
}
//重写回调处理函数
public function NotifyProcess($data, &$msg)
{
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
if(!array_key_exists("transaction_id", $data)){
$msg = "输入参数不正确";
return false;
}
//查询订单,判断订单真实性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
return true;
}
}
Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);
function xmlToArray($xml) //将XML转为array
{
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
$xml = $GLOBALS['HTTP_RAW_POST_DATA']; //获取支付后返回的数据
$postdata=xmlToArray($xml);
$data = $postdata['transaction_id'];
$data_sn = $postdata['out_trade_no'];
$r = $notify->Queryorder($data);
if($r){ //验证确认支付后修改订单状态
$sql="update ecs_order_info set pay_status = 2 where order_sn = {$data_sn}";
$GLOBALS['db']->query($sql);
}