关于微信扫码支付demo

PHP 徐, 伟 413℃ 0评论

微信商户平台有demo 可以下载下来

1.支付代码

//支付
	public function pay(){
		
		$order_number =date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
		$array = array(
			   'order_number' =>$order_number,
			   'payment_amount' => 1,
               'type' => 0,
               'number' => 1,
               'time' =>time(),

			);
		
		$record = M('order_xin')->add($array);	
		$notifyurl='http://' . $_SERVER['HTTP_HOST'] .U('user/weixinnotify');
		//dump($notifyurl);
		$url='http://' . $_SERVER['HTTP_HOST'] .'/Wxpay/example/qrcode.php';
		$this->assign('url',$url);
                include_once("Wxpay/lib/WxPay.Api.php") ;
                include_once("Wxpay/example/WxPay.NativePay.php");
                include_once("Wxpay/example/log.php");
                $notify = new \NativePay();
                $input = new \WxPayUnifiedOrder();
		$input->SetBody("充值20元活动");
		$input->SetAttach("永利建材商城欢迎您");
		$input->SetOut_trade_no($order_number);
		$input->SetTotal_fee($array['payment_amount']);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("订单优惠标记");
		$input->SetNotify_url($notifyurl);
		$input->SetTrade_type("NATIVE");
		$input->SetProduct_id("123456789");
		$result = $notify->GetPayUrl($input);
		$url2 = $result["code_url"];

		
		//var_dump($GLOBALS["HTTP_RAW_POST_DATA"]);
		$this->assign('order_id',$order_number);

		$this->assign('url2',$url2);
                $this->display();
    }

2.支付回调接口

public function weixinnotify(){
		
        include_once("wxPay/lib/WxPay.Api.php");
        include_once("wxPay/lib/WxPay.Notify.php");
        $xml=$GLOBALS['HTTP_RAW_POST_DATA']; 
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);  
        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($data["transaction_id"]);
		
        $WxPayApi = new \WxPayApi();
        $result = $WxPayApi->orderQuery($input);
		
		
        $orderid=$result['out_trade_no'];
        $array =array('type'=>1);
        $ordstatus = M('order_xin')->where('order_number=' . $orderid)->save($array);
        $trade_state=$result['trade_state'];
        if ($trade_state == "SUCCESS") {
            $status = self::checkorderstatus($orderid);
            if (!$status) {
                $parameter = array(
                    "out_trade_no" => $orderid, //商户订单编号;
                    "trade_no" => $result['transaction_id'],
                    "total_fee" => $result['total_fee']/100, //交易金额;
                    "trade_status" => $trade_state, //交易状态
                    "notify_id" => $result['sign'], //通知校验ID。
                    "notify_time" => date("Y-m-d H:i:s", time()), //通知的发送时间。
                );
                self::orderhandle($parameter);
                echo "SUCCESS";
            }else{
                echo "SUCCESS";
            }
        } else {
            echo "FAIL";
        }
		$test['content'] = json_decode($result);
		
		M('test')->add($test);
		
    }

3.检验订单状态

public function checkorderstatus($orderid) {
        $ordstatus = M('order_xin')->where('order_number=' . $orderid)->getField('type');
        if ($ordstatus == 1) {
            return true;
        } else {
            return false;
        }
    }

4.js 判断当前页面支付是否成功,成功则调到一个页面

function pay_status(){
   var order_id = $("#order_id").val();
   //console.log(order_id);
   $.ajax({  
    url:"{:U('User/pay_status_check')}",
    dataType:'json', 
    type:'post',  
    data:{'order_id':order_id}, 
    success:function(data){  
      if(data == '0' ){
        window.clearInterval(int); //销毁定时器
        setTimeout(function(){
          //跳转到结果页面,并传递状态
          window.location.href="{:U('User/index')}";
        },1000)
         
      }else if(data =='1'){
        alert('成功');
        window.clearInterval(int); //销毁定时器
        setTimeout(function(){
          //跳转到结果页面,并传递状态
          window.location.href="http://jiancainet.huazhong104.ccjjj.net";
        },1000)
      }
    }, 
    error:function(){  
     
       
    },  
 
 });
}
//启动定时器
var int=self.setInterval(function(){pay_status()},20000);

5.ajax判断当前订单状态

public function pay_status_check(){
    $order_id = I("order_id");
   // dump($order_id);exit;
    $result = M('order_xin')->where("order_number = $order_id")->find();
   // dump($result);exit;
    echo $result['type'];
  }

转载请注明:cooljun小站 » 关于微信扫码支付demo

如果你觉得这篇文章对你有帮助,请支持我继续更新网站 !捐赠本站
喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. cooljun
    格式有待优化 比上次有进步 :lol:
    cooljun2017-09-26 16:10 回复
    • 徐, 伟
      谢谢夸奖
      徐, 伟2017-09-26 16:12 回复