首頁  >  文章  >  微信小程式  >  Thinkphp整合微信支付功能

Thinkphp整合微信支付功能

高洛峰
高洛峰原創
2017-02-21 15:01:331929瀏覽

這篇文章主要介紹了Thinkphp整合微信支付功能的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

先上效果圖:我要告訴你我這一篇文章寫的是微信支付之中的(一般商家而非服務商戶的統一下單JSPI)微信支付:

Thinkphp整合微信支付功能Thinkphp整合微信支付功能Thinkphp整合微信支付功能Thinkphp整合微信支付功能Thinkphp整合微信支付功能

其實自己整合SDK失敗了,用了一個部落格部落客整合的程式碼,在這裡寫一下筆記:

## 前面準備:

1、微信公眾號:

    獨特的appid、appscrect、介面權限之中設定可以取得使用者ID資訊權限的網域名稱(每個使用者對於不同公眾都會有一個特有ID,透過這個ID取得使用者微信帳號基本資料、詳情看微信開發者文件)、在微信支付按鈕出設定微信支付授權目錄(寫到發起請求的控制器那一層)、設定開發者微信帳號測試白名單(用微信開發者工具的時候需要)

2、微信支付平台:

  商家平台登陸帳號、支付金鑰(隨時可以自行設置,只能有一個)、

3、整合進去thinkphp之中邏輯:

  前端微信支付按鈕設定點擊呼叫支付發起控制器方法、

  控制器運行,引用微信支付類、獲取用戶openid、獲取訂單數據、拼接出所有普通商戶預付jsp需要的數據,display出那個自定義的支付頁面、

#  在支付頁面點選支付、呼叫微信提供的jspi發起支付的scripet函數發起支付、

  支付完成以後頁面會重定向到(在自訂支付頁面的script函數裡設定的跳轉目錄{:U( 'controller/function)}),並且非同步(靜默)設定的非同步處理訂單邏輯(記錄支付時間啦、標記為已經支付啦、標記是微信支付啦)之類的、

代碼:

  我的訂單頁面的微信支付按鈕:

<a href="{:U(&#39;Wxpay/js_api_start&#39;,array(&#39;order_key_num&#39;=>$v[&#39;order_key_num]))}"> 微信支付</a>

發起付款控制器Wxpay:

#

<?php
namespace Home\Controller;
use Think\Controller;
//微信支付类
class WxpayController extends Controller {
 //获取access_token过程中的跳转uri,通过跳转将code传入jsapi支付页面
 public function js_api_start(){
  if(!empty($_GET[&#39;order_key_num&#39;])){
   // session(array(&#39;pay_now_id&#39;=>$_GET[&#39;order_key_num&#39;],&#39;expire&#39;=>3600));
   S(&#39;pay_now_id&#39;,$_GET[&#39;order_key_num&#39;],3600);
  }
  vendor(&#39;Weixinpay.WxPayPubHelper&#39;);
  //使用jsapi接口
  $jsApi = new \JsApi_pub();
  //=========步骤1:网页授权获取用户openid============
  //通过code获得openid
   if($_GET[&#39;code&#39;] == &#39;&#39;){
   //跳转
    $redirect_uri = &#39;https://当前域名+模块+控制器+方法&#39;;
    $url = &#39;https://open.weixin.qq.com/connect/oauth2/authorize
    ?appid=公众号特有IDredirect_uri=&#39;.$redirect_uri.&#39;&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect&#39;;
    header("Location: $url");
    exit();
   }else{
   //获取openid
   $url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token
   ?appid=公众号ID&secret=公众号scrept&code=&#39;.$_GET[&#39;code&#39;].&#39;&grant_type=authorization_code&#39;; 
   $openid_arr = json_decode(file_get_contents($url),true);
  }
  $openid=$openid_arr[&#39;openid&#39;];
  $pay_now_id = S(&#39;pay_now_id&#39;);
  if($pay_now_id){
   $id=$pay_now_id;
   $o = D(&#39;order_info&#39;);
   $order_info = $o->where(&#39;order_id = %d&#39;,$id)->find();
   if(empty($order_info[&#39;paycode&#39;])){
    $order_info[&#39;paycode&#39;] = &#39;weixin&#39;;
   }
   if($order_info[&#39;is_pay&#39;]){
    $this->error(&#39;当前订单已经支付&#39;);
   }
  }else{
   $this->error("不存在当前订单编号!");
  }
   $res = array(
   &#39;order_sn&#39; => $order_info[&#39;order_sn&#39;],
   &#39;order_amount&#39; => $order_info[&#39;pay_money&#39;]
   );
  //=========步骤2:使用统一支付接口,获取prepay_id============
  //使用统一支付接口
  $unifiedOrder = new \UnifiedOrder_pub();
  //设置统一支付接口参数
  //设置必填参数
  //appid已填,商户无需重复填写
  //mch_id已填,商户无需重复填写
  //noncestr已填,商户无需重复填写
  //spbill_create_ip已填,商户无需重复填写
  //sign已填,商户无需重复填写
  $total_fee = $order_info[&#39;pay_money&#39;]*100;
  // $total_fee = $res[&#39;order_amount&#39;];
  //$total_fee = 1;
  // var_dump($order_info[&#39;pay_money&#39;]);die;
  $body = "订单支付";
  $unifiedOrder->setParameter("openid", "$openid");//用户标识
  $unifiedOrder->setParameter("body", &#39;商品采购&#39;);//商品描述
  //自定义订单号,此处仅作举例
  $unifiedOrder->setParameter("out_trade_no", $order_info[&#39;order_sn&#39;]);//商户订单号 
  $unifiedOrder->setParameter("total_fee", $total_fee);//总金额
  //$unifiedOrder->setParameter("attach", "order_sn={$res[&#39;order_sn&#39;]}");//附加数据 
  $unifiedOrder->setParameter("notify_url", \WxPayConf_pub::NOTIFY_URL);//通知地址 
  $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型
  //非必填参数,商户可根据实际情况选填
  //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号 
  //$unifiedOrder->setParameter("device_info","XXXX");//设备号 
  //$unifiedOrder->setParameter("attach","XXXX");//附加数据 
  //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
  //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 
  //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 
  //$unifiedOrder->setParameter("openid","XXXX");//用户标识
  //$unifiedOrder->setParameter("product_id","XXXX");//商品ID
  $prepay_id = $unifiedOrder->getPrepayId();
  // var_dump($prepay_id);die;
  //=========步骤3:使用jsapi调起支付============
  $jsApi->setPrepayId($prepay_id);
  $jsApiParameters = $jsApi->getParameters();
  $wxconf = json_decode($jsApiParameters, true);
  if ($wxconf[&#39;package&#39;] == &#39;prepay_id=&#39;) {
   $this->error(&#39;当前订单存在异常!&#39;);
  }
  $this->assign(&#39;res&#39;, $res);
  $this->assign(&#39;jsApiParameters&#39;, $jsApiParameters);
  $this->display(&#39;jsapi&#39;);
 }
 //异步通知url,商户根据实际开发过程设定
 public function notify_url() {
  vendor(&#39;Weixinpay.WxPayPubHelper&#39;);
  //使用通用通知接口
  $notify = new \Notify_pub();
  //存储微信的回调
  $xml = $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]; 
  $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();
  //==商户根据实际情况设置相应的处理流程,此处仅作举例=======
  //以log文件形式记录回调信息
  //$log_name = "notify_url.log";//log文件路径
  //$this->log_result($log_name, "【接收到的notify通知】:\n".$xml."\n");
  $parameter = $notify->xmlToArray($xml);
  //$this->log_result($log_name, "【接收到的notify通知】:\n".$parameter."\n");
  if($notify->checkSign() == TRUE){
   if ($notify->data["return_code"] == "FAIL") {
    //此处应该更新一下订单状态,商户自行增删操作
    //$this->log_result($log_name, "【通信出错】:\n".$xml."\n");
    //更新订单数据【通信出错】设为无效订单
    echo &#39;error&#39;;
   }
   else if($notify->data["result_code"] == "FAIL"){
    //此处应该更新一下订单状态,商户自行增删操作
    //$this->log_result($log_name, "【业务出错】:\n".$xml."\n");
    //更新订单数据【通信出错】设为无效订单
    echo &#39;error&#39;;
   }
   else{
    //$this->log_result($log_name, "【支付成功】:\n".$xml."\n");
    //我这里用到一个process方法,成功返回数据后处理,返回地数据具体可以参考微信的文档
    if ($this->process($parameter)) {
     //处理成功后输出success,微信就不会再下发请求了
     echo &#39;success&#39;;
    }else {
     //没有处理成功,微信会间隔的发送请求
     echo &#39;error&#39;;
    }
   }
  }
 }
 //订单处理
 private function process($parameter) {
  //此处应该更新一下订单状态,商户自行增删操作
  /*
  * 返回的数据最少有以下几个
  * $parameter = array(
   &#39;out_trade_no&#39; => xxx,//商户订单号
   &#39;total_fee&#39; => XXXX,//支付金额
   &#39;openid&#39; => XXxxx,//付款的用户ID
  );
  */
  $data = array(
      &#39;order_sn&#39;=>$parameter[&#39;out_trade_no&#39;],
      &#39;des&#39;=>(&#39;订单交易:&#39;.$parameter[&#39;out_trade_no&#39;]),
      &#39;money&#39;=>$parameter[&#39;total_fee&#39;],
     );
  orderhandlestarysdgdss($data);//这是一个common方法,他会将该订单状态设置为已支付之类的
  return true;
 }
}
?>

發起付款後拼接預付資料參數(參數清單看微信一般商家開發者文件-微信付款-統一下單)display的頁面:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<meta name="format-detection" content="telephone=no"/> 
<title>下</title>
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
<meta name="keyword" content="">
<meta name="description" content="">
<script type="text/javascript">
var order_sn = "{$res[&#39;order_sn&#39;]}";
//调用微信JS api 支付
function jsApiCall(){
 WeixinJSBridge.invoke(
  &#39;getBrandWCPayRequest&#39;,
  <?php echo $jsApiParameters; ?>,
  function(res){
   //如果支付成功
   if (res.err_msg == &#39;get_brand_wcpay_request:ok&#39;) {
    //支付成功后跳转的地址
    location.href = "{:U(&#39;Home/User/my_order&#39;)}";
   }else if (res.err_msg == &#39;get_brand_wcpay_request:cancel&#39;) {
    alert(&#39;请尽快完成支付哦!&#39;);
   }else if (res.err_msg == &#39;get_brand_wcpay_request:fail&#39;) {
    alert(&#39;支付失败&#39;);
   }else {
    alert(&#39;意外错误&#39;);
   }
   //WeixinJSBridge.log(res.err_msg);
   //alert(res.err_code+res.err_desc+res.err_msg);
   /*if (res.err_msg == &#39;get_brand_wcpay_request:ok&#39;) {
    alert(&#39;支付成功&#39;);
   }else {
    alert(&#39;取消支付&#39;);
   }*/
  }
 );
}
function callpay(){
 if (typeof WeixinJSBridge == "undefined"){
  if( document.addEventListener ){
   document.addEventListener(&#39;WeixinJSBridgeReady&#39;, jsApiCall, false);
  }else if (document.attachEvent){
   document.attachEvent(&#39;WeixinJSBridgeReady&#39;, jsApiCall); 
   document.attachEvent(&#39;onWeixinJSBridgeReady&#39;, jsApiCall);
  }
 }else{
  jsApiCall();
 }
}
</script>
<style>
*{font-family:&#39;微软雅黑&#39;,&#39;Microsoft YaHei&#39;;}
body #head{position:relative;z-index:99999999999999;padding:0 10px;}
body .zh-head{padding:0 0 0 0;height:auto;}
.zh-head-conter{position:relative;height:40px;}
.zh-logo{position:absolute;left:50%;top:0;margin:0 0 0 -60px;float:none;width:auto;}
.zh-logo a{display:block;}
.zh-logo img{width:120px;height:40px;display:block;}
.heads_fix .zh-logo{}
#head{position:fixed!important;left:0;top:0;right:0;z-index:99999;background:#fff;border-bottom:1px solid #ddd;}
.zh-logo{height:40px;}
.flowpay{margin-top:25%;}
.flowpay dt{text-align:center;}
.flowpay strong.price{font-size:40px;}
.wxLogo{text-align:center;}
.wxLogo img{}
.flowpay dd{margin:0;padding:20px 0 10px 0;}
.flowpay dd input{margin:0 auto;padding:0;width:90%;height:45px;line-height:45px;border:0;border-radius:4px;background:#0CBC0A;color:#fff;font-size:17px;display:block;-webkit-appearance:none;-moz-appearance:none;appearance:none;outline:none;}
</style>
</head>
<body>
<!--头部开始-->
<p class="flowpay">
 <dl>
  <dt>
   <p class="wxLogo"><img src="__PUBLIC__/home/images/1479953699138120.png" alt=""></p>
   本次订单需支付:¥<strong class="price">{$res[&#39;order_amount&#39;]}</strong> 元
  </dt>
  <dd>
   <input type="button" id="hhhhhh" onclick="callpay()" value="立即支付" />
  </dd>
 </dl>
</p>
<!--尾结束-->
</body>
</html>

然後就是類別檔案啦:

Thinkphp整合微信支付功能

那個cacert是憑證存放目錄;憑證不一定需要的;

vendor資料夾在我的文件裡面找找就可以。

以上所述是小編給大家介紹的Thinkphp整合微信支付功能,希望對大家有幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對PHP中文網的支持!

更多Thinkphp整合微信支付功能相關文章請追蹤PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn