在一星期以前吧,我開始接觸微信,最微信支付,最開始也是一臉懵逼,還好吧,我請教大神,完美解決支付問題,下面直接上乾貨。javascript
首先要準備微信支付模塊,就是微信支付寶,官方是一個zip壓縮文件php
WxpayAPI_php_v3.zipcss
而後解壓放在項目根路徑也能夠。html
若是說你是隻須要綁定一個商戶java
const APPID = 'xxxxxxxx';
const MCHID = 'xxxxxxxx';
const KEY = 'xxxxxxxxx';
const APPSECRET = 'xxxxxxxxx';
你的微信配置文件能夠寫死 文件在微信支付模塊lib文件夾下 WxPay.Config.phpmysql
主要的配置實在微信支付模塊example下 jsapi.php 這是最重要的文件 咱們的主要操做都是在這裏面jquery
ini_set('date.timezone','Asia/Shanghai'); ini_set('display_errors','Off'); error_reporting(E_ERROR); require_once '../../../tp5/mydatabase.php'; require_once "../lib/WxPay.Api.php"; require_once "WxPay.JsApiPay.php"; require_once 'log.php';
最開始咱們要引入配置支付文件 樓主給支付配置文件起的名字叫作mydatabases.php 樓主的微信支付文件夾是兩層因此是../../../sql
const USERNAME=USERNAME; const PASSWORD=PASSWORD; const DATABASE=DATABASE;
先設定幾個變量 樓主是查數據庫去取APPID APPSECRET KEY MCHID數據庫
//數據庫鏈接 $conn = mysqli_connect('localhost',USERNAME,PASSWORD); if ($conn) { $db = mysqli_select_db($conn,DATABASE); if ($db) { mysqli_set_charset($conn,'utf8'); $sql = 'select * from activity where activity_id = '.$_COOKIE["activity_id"].''; $result = mysqli_query($conn,$sql); if ($result) { while ($row = mysqli_fetch_array($result)) { $fee = $row["activity_paymoney"]; $body = $row["activity_title"]; //var_dump($row); } } else { echo '數據庫查詢失敗'; } } else { echo '連接數據庫'; } } else { echo '登錄數據庫失敗'; }
因此接下來查詢數據庫json
$tools = new JsApiPay(); $openId = $tools->GetOpenid();
①、獲取用戶openid
$input = new WxPayUnifiedOrder(); $input->SetBody($body); $input->SetAttach($_COOKIE["activity_id"]); $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis")); $input->SetTotal_fee($fee*100); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis",time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url(MYMYURL."/wxpay/wxpay/example/notify.php"); $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input); $jsApiParameters = $tools->GetJsApiParameters($order);
②、統一下單 必定要配置SetNotify_url SetTime_expire-》600是指即時到帳 若是但願時間晚點到帳 ,能夠調整數字大小
mysqli_close($conn);
數據庫關閉
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <title>微信支付</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0,maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="/home/css/pay.css"> <link rel="stylesheet" href="/home/css/reset.css"> <script src="/home/js/jquery-1.8.3.min.js"></script> <script src="/home/js/jquery.cookie.js"></script> <script type="text/javascript"> var html = document.querySelector('html'); html.style.fontSize = html.offsetWidth / 7.2 + 'px'; addEventListener('resize', function() { html.style.fontSize = html.offsetWidth / 7.2 + 'px'; }, false); </script> <script type="text/javascript"> //調用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $jsApiParameters; ?>, function(res){ WeixinJSBridge.log(res.err_msg); // alert(res.err_code+res.err_desc+res.err_msg); if (res.err_msg == "get_brand_wcpay_request:ok") { window.location.href = "/tp5/public/users/index/index?id="+$.cookie('activity_id')+"&olduid="+$.cookie('openid'); } else { alert('取消支付'); window.location.href = "/tp5/public/users/index/index?id="+$.cookie('activity_id')+"&olduid="+$.cookie('openid'); } } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } function cancel(){ // window.history.go(-1); window.location.href = "/tp5/public/users/index/index?id="+$.cookie('activity_id')+"&olduid="+$.cookie('openid'); } </script> </head> <body> <div id="pay"> <div class="wrap"> <div class="order"> <div class="order-in"><p style="border-bottom: 0.05rem solid #fd908d;margin: 1.1rem auto;color:#fff;font-size: 0.4rem;width:50vw;line-height: 1rem;">訂單金額</p></div> <div class="order-in"><p style="color: #eefe00;line-height: 1rem;"><span class="fee"><?php echo $fee; ?></span>元</p></div> </div> <div class="pay" onclick="callpay()">微信支付</div> <div class="cancelpay" onclick="cancel()">取消支付</div> <div class="blank"> <p class="zf"><a href="http://maidian.winyx.net/about.aspx" style="font-weight:900;font-family:'Microsoft YaHei';">技術支持:</a></p> </div> </div> </div> </body> </html>
支付的自定義html
接下來是notify.php文件 這個文件主要是修改數據庫狀態
require_once '../../../tp5/mydatabase.php'; require_once "../lib/WxPay.Api.php"; require_once '../lib/WxPay.Notify.php'; require_once 'log.php'; require_once 'Tixian.php';
引入數據庫文件配置
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; }
//從這裏開始連接數據庫 $_COOKIE['openid1']=$data['openid']; $_COOKIE['activity_id1']=$data['attach']; //修改數據的微信訂單和付款時間 //鏈接數據庫 $conn=mysqli_connect('localhost',USERNAME,PASSWORD); mysqli_select_db($conn,DATABASE); mysqli_set_charset($conn,'utf8');//設置字符集 //準備sql語句 $paymoney="select * from activity where activity_id='{$data['attach']}'";//查詢活動金額 $paymoneys=mysqli_query($conn,$paymoney); while ($row = mysqli_fetch_array($paymoneys)) { $fee = $row["activity_paymoney"]; } $sql="update users set pay_wx_order='{$data['transaction_id']}',pay_time='{$data['time_end']}',money_self={$fee},state=1 where openid='{$data['openid']}' and activity_id='{$data['attach']}'"; $result=mysqli_query($conn,$sql); //======把支付數據寫入數據庫開始==============================================- $chaxun="select * from tixian where openid='{$data['openid']}' and activity_id='{$data['attach']}'"; $chaxuns=mysqli_query($conn,$chaxun); $chaxunss=mysqli_fetch_array($chaxuns); //執行插入數據 if($chaxunss){ }else{ $time=date('Y-m-d H:i:s',time()); $tixian="insert into tixian value(null,'{$data['openid']}',3,{$data['attach']},'{$time}',null,null)"; $tixians=mysqli_query($conn,$tixian); } $rechaxun="select * from tixian where openid='{$data['openid']}' and activity_id='{$data['attach']}'"; $rechaxuns=mysqli_query($conn,$rechaxun); while($rechaxunss=mysqli_fetch_array($rechaxuns)){ $tixian_state=$rechaxunss['state']; } if($tixian_state==3){ //執行更新提現狀態 $gengxin="update tixian set state=2 where openid='{$data['openid']}' and activity_id='{$data['attach']}'"; $gengxins=mysqli_query($conn,$gengxin); //執行提現代碼 //提現類外邊,就是最下邊寫 $tt=new Tixian(); $tt->actionAct_tixian(); } //========================================================= mysqli_close($conn); return SUCCESS;//通知微信收到數據 } }
重寫回調處理函數裏邊處理狀態
接下來 就是配置服務器的什麼回調地址 什麼的 作這個就不說了 微信解釋的挺清楚地 好了 今天教程將就到這裏了 有問題 你們評論留言 樓主盡力解答 原創不易 受教了