溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用thinkphp5怎么實(shí)現(xiàn)一個微信掃碼支付功能

發(fā)布時(shí)間:2021-05-14 17:17:40 來源:億速云 閱讀:198 作者:Leah 欄目:開發(fā)技術(shù)

使用thinkphp5怎么實(shí)現(xiàn)一個微信掃碼支付功能?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

配置WxPay.Config.php

使用thinkphp5怎么實(shí)現(xiàn)一個微信掃碼支付功能

控制器

//微信支付
//參數(shù) 訂單 價(jià)格
 public function wxPay($order_number,$money)
 {
  header("Content-type:text/html;charset=utf-8");
   //require_once VENDOR_PATH.'/alipaymobile/config.php';
  require_once VENDOR_PATH.'/wxpay/WxPay.Api.php';//引入微信支付
  require_once VENDOR_PATH.'/wxpay/WxPay.Notify.php';
  require_once VENDOR_PATH.'/wxpay/phpqrcode/phpqrcode.php';

  $input = new \WxPayUnifiedOrder();//統(tǒng)一下單
  $config = new \WxPayConfig();//配置參數(shù)
  $notify = new \QRcode();
  //$paymoney = input('post.paymoney'); //支付金額
  $paymoney = $money; //測試寫死
  //$paymoney = 0.01; //測試寫死
  //$str = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);//生成訂單號 
  $out_trade_no = $order_number; //商戶訂單號(自定義)
  $goods_name = '掃碼支付'.$paymoney.'元'; //商品名稱(自定義)
  $input->SetBody($goods_name);
  $input->SetAttach($goods_name);
  $input->SetOut_trade_no($out_trade_no);
  $input->SetTotal_fee($paymoney*100);//金額乘以100
  $input->SetTime_start(date("YmdHis"));
  $input->SetTime_expire(date("YmdHis", time() + 600));
  $input->SetGoods_tag("test");
  //回調(diào)地址 注意能訪問
  $input->SetNotify_url("http://www.域名.com/index.php/index/index/wxpaynotifyurl"); //回調(diào)地址
  $input->SetTrade_type("NATIVE");
  $input->SetProduct_id('123456789');//商品id
  $result = \WxPayApi::unifiedOrder($config, $input);
  if($result['result_code']=='SUCCESS' && $result['return_code']=='SUCCESS') {
   $url = $result["code_url"];
   $this->assign('money',$paymoney);
   $this->assign('url',$url);
   $this->assign('num',$out_trade_no);
   //映射視圖 微信二維碼需要自己生成 
   return view("../../../template/wxpay");
  }else{
   $this->error('參數(shù)錯誤'); 
  }
  // return view();

 }

視圖 生成二維碼 進(jìn)行掃描付款

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>微信支付頁面</title>
<style>
.wxsm{ width:1200px; margin:0 auto; overflow:hidden;}
.wxsmt{ width:100%; height:40px; border-bottom:1px solid #ccc;}
.wxsmt h3{ font-size:14px; line-height:40px; color:#232323; font-weight:400; text-align:center;}
.wxsmc{ width:100%; overflow:hidden;}
.smcr{ overflow:hidden; margin:0 auto; }
.smcr h3{ font-size:14px; font-weight:400; line-height:40px; text-align:center;}
.smcr h3 span{ font-size:20px; color:#f23535;}
.smcrb{ width:100%; overflow:hidden;;}
.smm{ width:218px; height:284px; border:1px solid #3cb035; background:#3cb035; margin:0 auto}
.smm img{ width:218px; height:218px; background:#fff;}
.smm span{ display:block; color:#fff; line-height:66px; text-align:center;}

</style>
<script src="__TEMP__/js/jquery-1.10.1.min.js"></script>

</head>

<body>

<div class="wxsm">
 <div class="wxsmt">
  <h3>訂單提交成功,請盡快付款</h3>
 </div>
 <div class="wxsmc">
  <div class="smcr">
   <h3>應(yīng)付金額:<span>{$money}</span>元</h3>
   <div class="smcrb">
    <div class="smm">
     <img src="/vendor/wxpay/qrcode.php?data=<?php echo urlencode($url);?>"/>
     <span>打開微信,掃描二維碼支付</span>
    </div>
   </div>
  </div>
 </div>
</div>

<script> 
 //設(shè)置每隔1000毫秒執(zhí)行一次load() 方法 
 var myIntval=setInterval(function(){loads()},1000); 
 function loads(){ 
  
  var xmlhttp; 
    // 輪詢的瀏覽器設(shè)置 
    if (window.XMLHttpRequest){ 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    }else{ 
     // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      trade_state=JSON.parse(xmlhttp.responseText); 
      console.log(trade_state);//打印查看輪詢的狀態(tài),也可以關(guān)閉。
      //判斷訂單支付狀態(tài) 并用document.getElementById方法賦值到myDiv中;
      if(trade_state.code=='SUCCESS'){ 
       
       //延遲3000毫秒執(zhí)行tz() 方法
       clearInterval(myIntval);
       //進(jìn)行跳轉(zhuǎn)。
        var url = "{:url('index/center')}";
        var http = window.location.protocol;
        var zhu = window.location.host;
        setTimeout("location.href='"+http+"//"+zhu+""+url+"'",1500); 
      }else if(trade_state.code=='REFUND'){ 
       
       clearInterval(myIntval); 
      }else if(trade_state.code=='NOTPAY'){ 
         
      }else if(trade_state.code=='CLOSED'){ 
       
       clearInterval(myIntval);
      }else if(trade_state.code=='REVOKED'){ 
       
       clearInterval(myIntval);
      }else if(trade_state.code=='USERPAYING'){ 
       
      }else if(trade_state.code=='PAYERROR'){ 
       
       clearInterval(myIntval); 
      } 
       
     } 
    } 
  //orderquery.php 文件返回訂單狀態(tài),通過訂單狀態(tài)確定支付狀態(tài) 
  xmlhttp.open("POST","/vendor/wxpay/orderquery.php",false); 
  //下面這句話必須有 
  //把標(biāo)簽/值對添加到要發(fā)送的頭文件。 
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
  xmlhttp.send("out_trade_no=<?php echo $num;?>"); 
   
 } 
</script>

</body>
</html>

支付回調(diào)

public function wxpaynotifyurl()
 {
  //$xml = $GLOBALS['HTTP_RAW_POST_DATA']; //返回的xml
 $xml = file_get_contents("php://input");
 //$results = db('fund') -> where('id',1) -> update(['a'=>$xml]);exit();
  $xmlArr = $this->Init($xml);
  file_put_contents(dirname(__FILE__).'/xml.txt',$xml); //記錄日志 支付成功后查看xml.txt文件是否有內(nèi)容 如果有xml格式文件說明回調(diào)成功
  
  $out_trade_no=$xmlArr['out_trade_no']; //訂單號
  $total_fee=$xmlArr['total_fee']/100; //回調(diào)回來的xml文件中金額是以分為單位的
  $result_code=$xmlArr['result_code']; //狀態(tài)
  //$result = db('order') -> where(['order' => $out_trade_no]) -> find();
  //if($result['price'] == $total_fee){
   if($result_code=='SUCCESS'){ //數(shù)據(jù)庫操作
    //處理數(shù)據(jù)庫操作 例如修改訂單狀態(tài) 給賬戶充值等等 
 
    echo 'SUCCESS'; //返回成功給微信端 一定要帶上不然微信會一直回調(diào)8次
    exit; 
   }else{ //失敗
    return false;
    exit;
   }
  
  
 }
 public function Init($xml)
 {
  $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  return $array_data;
 }

關(guān)于使用thinkphp5怎么實(shí)現(xiàn)一個微信掃碼支付功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI