做微信支付,需要的條件是:1、開通微信服務號,2、開通微信商戶后台申請支付方式(公眾號支付、H5支付、APP支付、小程序支付)
(注意微信服務號里面的配置以及商戶后台 安全目錄 的設置,不然即使你寫的沒錯誤,也調用不成功)
公眾號h5頁面寫法: (購物車提交--我們上一步已經生成了訂單,訂單表里面已經有記錄了)
order.php?act=wxpay&order_sn=888888 ($user_id可以從session獲取到)
//查詢訂單詳情
$orderInfo = orderDetailInfo($order_sn,$user_id);
var_dump($orderInfo);
//測試支付金額
if($user_id=="24") $orderInfo['total_fee']=0.01;
//調用統一下單api的請求參數
$jsApiParameters = wxUnifiedOrder($orderInfo['order_sn'],$orderInfo['total_fee']*100);
$smarty->assign('jsapiparameters',$jsApiParameters);
$smarty->display('wxpay.html');
//調用微信統一下單api的js參數
function wxUnifiedOrder($order_sn,$total_fee)
{
require_once ROOT_PATH . "wxpay/lib/WxPay.Api.php";
require_once ROOT_PATH . "wxpay/example/WxPay.JsApiPay.php";
//require_once 'log.php';
//①、獲取用戶openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
//②、統一下單
$input = new WxPayUnifiedOrder();
$input->SetOpenid($openId);
$input->SetBody("XXX公眾號購買");
$input->SetAttach("XXX公眾號購買產品$order_sn");
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($total_fee);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("XXX公眾號購買");
$input->SetNotify_url("http://XXX.com/wxshop/wxnotify.php");
$input->SetTrade_type("JSAPI");
$order = WxPayApi::unifiedOrder($input);
//printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);
//③、在支持成功回調通知中處理成功之后的事宜,見 notify.php
/**
* 注意:
* 1、當你的回調地址不可訪問的時候,回調通知會失敗,可以通過查詢訂單來確認支付是否成功
* 2、jsapi支付時需要填入用戶openid,WxPay.JsApiPay.php中有獲取openid流程 (文檔可以參考微信公眾平台“網頁授權接口”,
* 參考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
*/
return $jsApiParameters;
}
html頁面(怎么調用jsapi,上一個頁面的$jsApiParameters參數賦值到那塊了)
<a class="next btn SEND_PAY" onclick="callpay()" >立即支付</a>
<script type="text/javascript">
//調用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsapiparameters},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ){
layer.open({
content: '支付成功'
,skin: 'msg'
,time: 2 //2秒后自動關閉
});
}
}
);
}
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();
}
}
</script>
付款后通知地址:
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/../../includes/init.php');
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";//sdk有這個文件
require_once '../lib/WxPay.Notify.php';//sdk有這個文件
require_once 'log.php';//sdk有這個文件
//初始化日志
$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("1");
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
Log::DEBUG("2");
if(!array_key_exists("transaction_id", $data)){
$msg = "輸入參數不正確";
return false;
}
//查詢訂單,判斷訂單真實性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "訂單查詢失敗";
return false;
}
//如果成功,那么就更改訂單狀態
$res = $this->updateOrder($data["out_trade_no"],$data["transaction_id"]);
if($res)
{
return true;
}else{
return false;
}
}
public function updateOrder($order_sn,$trade_no)
{//更改訂單狀態--設置已付款
/* 取得訂單信息 */
$sql = 'SELECT order_id, user_id, order_sn, trade_no, consignee, address, tel, shipping_id, extension_code, extension_id, goods_amount ' .
'FROM t_order_info ' .
" WHERE order_sn = '$order_sn'";
$order = $GLOBALS['db']->getRow($sql);
$sql = 'UPDATE t_order_info ' .
" SET order_status = '1', " .
" confirm_time = '" . gmtime() . "', " .
" pay_status = '2', " .
" pay_id = '7', " .
" pay_name = '微信支付', " .
" pay_time = '".gmtime()."', " .
" money_paid = order_amount," .
" group_buy_status = group_buy_status+1," .
" trade_no = '$trade_no' ".
"WHERE order_sn = '$order_sn'";
$res = $GLOBALS['db']->query($sql);
if($res)
{
return true;
}else{
return false;
}
}
}
$notify = new PayNotifyCallBack();//實例化
$notify->Handle(false);//調用這個方法,將來就可以執行上面那個回調方法,並且,獲取xml數據,驗證簽名,和最后的給微信輸出xml,都可以完成
//$resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml> ";
//echo $resXml;
//Log::DEBUG($resXml);

