本文為大家分享了PHP微信支付實(shí)例,包括PHP微信支付源碼,PHP微信退款源碼,php微信支付接口,供大家參考,具體內(nèi)容如下
1.JSapi支付demo(在微信客戶端中點(diǎn)擊)
<?php
/**
* JS_API支付demo
* ====================================================
* 在微信瀏覽器里面打開H5網(wǎng)頁(yè)中執(zhí)行JS調(diào)起支付。接口輸入輸出數(shù)據(jù)格式為JSON。
* 成功調(diào)起支付需要三個(gè)步驟:
* 步驟1:網(wǎng)頁(yè)授權(quán)獲取用戶openid
* 步驟2:使用統(tǒng)一支付接口,獲取prepay_id
* 步驟3:使用jsapi調(diào)起支付
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//使用jsapi接口
$jsApi = new JsApi_pub();
//=========步驟1:網(wǎng)頁(yè)授權(quán)獲取用戶openid============
//通過(guò)code獲得openid
if (!isset($_GET['code']))
{
//觸發(fā)微信返回code碼
$url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
Header("Location: $url");
}else
{
//獲取code碼,以獲取openid
$code = $_GET['code'];
$jsApi->setCode($code);
$openid = $jsApi->getOpenId();
}
//=========步驟2:使用統(tǒng)一支付接口,獲取prepay_id============
//使用統(tǒng)一支付接口
$unifiedOrder = new UnifiedOrder_pub();
//設(shè)置統(tǒng)一支付接口參數(shù)
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//spbill_create_ip已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$unifiedOrder->setParameter("openid","$openid");//商品描述
$unifiedOrder->setParameter("body","貢獻(xiàn)一分錢");//商品描述
//自定義訂單號(hào),此處僅作舉例
$timeStamp = time();
$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商戶訂單號(hào)
$unifiedOrder->setParameter("total_fee","1");//總金額
$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易類型
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號(hào)
//$unifiedOrder->setParameter("device_info","XXXX");//設(shè)備號(hào)
//$unifiedOrder->setParameter("attach","XXXX");//附加數(shù)據(jù)
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始時(shí)間
//$unifiedOrder->setParameter("time_expire","XXXX");//交易結(jié)束時(shí)間
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品標(biāo)記
//$unifiedOrder->setParameter("openid","XXXX");//用戶標(biāo)識(shí)
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
$prepay_id = $unifiedOrder->getPrepayId();
//=========步驟3:使用jsapi調(diào)起支付============
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();
//echo $jsApiParameters;
?>
2.native支付模式一demo(用微信掃的靜態(tài)鏈接二維碼)
<?php
/**
* Native(原生)支付模式一demo
* ====================================================
* 模式一:商戶按固定格式生成鏈接二維碼,用戶掃碼后調(diào)微信
* 會(huì)將productid和用戶openid發(fā)送到商戶設(shè)置的鏈接上,商戶收到
* 請(qǐng)求生成訂單,調(diào)用統(tǒng)一支付接口下單提交到微信,微信會(huì)返回
* 給商戶prepayid。
* 本例程對(duì)應(yīng)的二維碼由native_call_qrcode.php生成;
* 本例程對(duì)應(yīng)的響應(yīng)服務(wù)為native_call.php;
* 需要兩者配合使用。
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//設(shè)置靜態(tài)鏈接
$nativeLink = new NativeLink_pub();
//設(shè)置靜態(tài)鏈接參數(shù)
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//time_stamp已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$product_id = WxPayConf_pub::APPID."static";//自定義商品id
$nativeLink->setParameter("product_id","$product_id");//商品id
//獲取鏈接
$product_url = $nativeLink->getUrl();
//使用短鏈接轉(zhuǎn)換接口
$shortUrl = new ShortUrl_pub();
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$shortUrl->setParameter("long_url","$product_url");//URL鏈接
//獲取短鏈接
$codeUrl = $shortUrl->getShortUrl();
?>
3.native支付模式二demo(用微信掃的動(dòng)態(tài)鏈接二維碼)
<?php
/**
* Native(原生)支付-模式二-demo
* ====================================================
* 商戶生成訂單,先調(diào)用統(tǒng)一支付接口獲取到code_url,
* 此URL直接生成二維碼,用戶掃碼后調(diào)起支付。
*
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//使用統(tǒng)一支付接口
$unifiedOrder = new UnifiedOrder_pub();
//設(shè)置統(tǒng)一支付接口參數(shù)
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//spbill_create_ip已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$unifiedOrder->setParameter("body","貢獻(xiàn)一分錢");//商品描述
//自定義訂單號(hào),此處僅作舉例
$timeStamp = time();
$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商戶訂單號(hào)
$unifiedOrder->setParameter("total_fee","1");//總金額
$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","NATIVE");//交易類型
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號(hào)
//$unifiedOrder->setParameter("device_info","XXXX");//設(shè)備號(hào)
//$unifiedOrder->setParameter("attach","XXXX");//附加數(shù)據(jù)
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始時(shí)間
//$unifiedOrder->setParameter("time_expire","XXXX");//交易結(jié)束時(shí)間
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品標(biāo)記
//$unifiedOrder->setParameter("openid","XXXX");//用戶標(biāo)識(shí)
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
//獲取統(tǒng)一支付接口結(jié)果
$unifiedOrderResult = $unifiedOrder->getResult();
//商戶根據(jù)實(shí)際情況設(shè)置相應(yīng)的處理流程
if ($unifiedOrderResult["return_code"] == "FAIL")
{
//商戶自行增加處理流程
echo "通信出錯(cuò):".$unifiedOrderResult['return_msg']."<br>";
}
elseif($unifiedOrderResult["result_code"] == "FAIL")
{
//商戶自行增加處理流程
echo "錯(cuò)誤代碼:".$unifiedOrderResult['err_code']."<br>";
echo "錯(cuò)誤代碼描述:".$unifiedOrderResult['err_code_des']."<br>";
}
elseif($unifiedOrderResult["code_url"] != NULL)
{
//從統(tǒng)一支付接口獲取到code_url
$code_url = $unifiedOrderResult["code_url"];
//商戶自行增加處理流程
//......
}
?>
4.支付查詢接口demo
<?php
/**
* 訂單查詢-demo
* ====================================================
* 該接口提供所有微信支付訂單的查詢。
* 當(dāng)支付通知處理異?;騺G失的情況,商戶可以通過(guò)該接口查詢訂單支付狀態(tài)。
*
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//退款的訂單號(hào)
if (!isset($_POST["out_trade_no"]))
{
$out_trade_no = " ";
}else{
$out_trade_no = $_POST["out_trade_no"];
//使用訂單查詢接口
$orderQuery = new OrderQuery_pub();
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$orderQuery->setParameter("out_trade_no","$out_trade_no");//商戶訂單號(hào)
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$orderQuery->setParameter("sub_mch_id","XXXX");//子商戶號(hào)
//$orderQuery->setParameter("transaction_id","XXXX");//微信訂單號(hào)
//獲取訂單查詢結(jié)果
$orderQueryResult = $orderQuery->getResult();
//商戶根據(jù)實(shí)際情況設(shè)置相應(yīng)的處理流程,此處僅作舉例
if ($orderQueryResult["return_code"] == "FAIL") {
echo "通信出錯(cuò):".$orderQueryResult['return_msg']."<br>";
}
elseif($orderQueryResult["result_code"] == "FAIL"){
echo "錯(cuò)誤代碼:".$orderQueryResult['err_code']."<br>";
echo "錯(cuò)誤代碼描述:".$orderQueryResult['err_code_des']."<br>";
}
else{
echo "交易狀態(tài):".$orderQueryResult['trade_state']."<br>";
echo "設(shè)備號(hào):".$orderQueryResult['device_info']."<br>";
echo "用戶標(biāo)識(shí):".$orderQueryResult['openid']."<br>";
echo "是否關(guān)注公眾賬號(hào):".$orderQueryResult['is_subscribe']."<br>";
echo "交易類型:".$orderQueryResult['trade_type']."<br>";
echo "付款銀行:".$orderQueryResult['bank_type']."<br>";
echo "總金額:".$orderQueryResult['total_fee']."<br>";
echo "現(xiàn)金券金額:".$orderQueryResult['coupon_fee']."<br>";
echo "貨幣種類:".$orderQueryResult['fee_type']."<br>";
echo "微信支付訂單號(hào):".$orderQueryResult['transaction_id']."<br>";
echo "商戶訂單號(hào):".$orderQueryResult['out_trade_no']."<br>";
echo "商家數(shù)據(jù)包:".$orderQueryResult['attach']."<br>";
echo "支付完成時(shí)間:".$orderQueryResult['time_end']."<br>";
}
}
//商戶自行增加處理流程
//......
?>
5.對(duì)賬單接口demo
<?php
/**
* 對(duì)賬單接口demo
* ====================================================
* 商戶可以通過(guò)該接口下載歷史交易清單。
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//對(duì)賬單日期
if (!isset($_POST["bill_date"])){
$bill_date = "20140814";
}
else{
$bill_date = $_POST["bill_date"];
//使用對(duì)賬單接口
$downloadBill = new DownloadBill_pub();
//設(shè)置對(duì)賬單接口參數(shù)
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$downloadBill->setParameter("bill_date","$bill_date");//對(duì)賬單日期
$downloadBill->setParameter("bill_type","ALL");//賬單類型
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$downloadBill->setParameter("device_info","XXXX");//設(shè)備號(hào)
//對(duì)賬單接口結(jié)果
$downloadBillResult = $downloadBill->getResult();
echo $downloadBillResult['return_code'];
if ($downloadBillResult['return_code'] == "FAIL") {
echo "通信出錯(cuò):".$downloadBillResult['return_msg'];
}else{
print_r('<pre>');
echo "【對(duì)賬單詳情】"."</br>";
print_r($downloadBill->response);
print_r('</pre>');
}
}
?>
6.退款接口demo
<?php
/**
* 退款申請(qǐng)接口-demo
* ====================================================
* 注意:同一筆單的部分退款需要設(shè)置相同的訂單號(hào)和不同的
* out_refund_no。一筆退款失敗后重新提交,要采用原來(lái)的
* out_refund_no??偼丝罱痤~不能超過(guò)用戶實(shí)際支付金額(現(xiàn)
* 金券金額不能退款)。
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//輸入需退款的訂單號(hào)
if (!isset($_POST["out_trade_no"]) || !isset($_POST["refund_fee"]))
{
$out_trade_no = " ";
$refund_fee = "1";
}else{
$out_trade_no = $_POST["out_trade_no"];
$refund_fee = $_POST["refund_fee"];
//商戶退款單號(hào),商戶自定義,此處僅作舉例
$out_refund_no = "$out_trade_no"."$time_stamp";
//總金額需與訂單號(hào)out_trade_no對(duì)應(yīng),demo中的所有訂單的總金額為1分
$total_fee = "1";
//使用退款接口
$refund = new Refund_pub();
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$refund->setParameter("out_trade_no","$out_trade_no");//商戶訂單號(hào)
$refund->setParameter("out_refund_no","$out_refund_no");//商戶退款單號(hào)
$refund->setParameter("total_fee","$total_fee");//總金額
$refund->setParameter("refund_fee","$refund_fee");//退款金額
$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作員
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$refund->setParameter("sub_mch_id","XXXX");//子商戶號(hào)
//$refund->setParameter("device_info","XXXX");//設(shè)備號(hào)
//$refund->setParameter("transaction_id","XXXX");//微信訂單號(hào)
//調(diào)用結(jié)果
$refundResult = $refund->getResult();
//商戶根據(jù)實(shí)際情況設(shè)置相應(yīng)的處理流程,此處僅作舉例
if ($refundResult["return_code"] == "FAIL") {
echo "通信出錯(cuò):".$refundResult['return_msg']."<br>";
}
else{
echo "業(yè)務(wù)結(jié)果:".$refundResult['result_code']."<br>";
echo "錯(cuò)誤代碼:".$refundResult['err_code']."<br>";
echo "錯(cuò)誤代碼描述:".$refundResult['err_code_des']."<br>";
echo "公眾賬號(hào)ID:".$refundResult['appid']."<br>";
echo "商戶號(hào):".$refundResult['mch_id']."<br>";
echo "子商戶號(hào):".$refundResult['sub_mch_id']."<br>";
echo "設(shè)備號(hào):".$refundResult['device_info']."<br>";
echo "簽名:".$refundResult['sign']."<br>";
echo "微信訂單號(hào):".$refundResult['transaction_id']."<br>";
echo "商戶訂單號(hào):".$refundResult['out_trade_no']."<br>";
echo "商戶退款單號(hào):".$refundResult['out_refund_no']."<br>";
echo "微信退款單號(hào):".$refundResult['refund_idrefund_id']."<br>";
echo "退款渠道:".$refundResult['refund_channel']."<br>";
echo "退款金額:".$refundResult['refund_fee']."<br>";
echo "現(xiàn)金券退款金額:".$refundResult['coupon_refund_fee']."<br>";
}
}
?>
7.退款查詢接口demo
<?php
/**
* 退款申請(qǐng)接口-demo
* ====================================================
*
*
*/
include_once("../WxPayPubHelper/WxPayPubHelper.php");
//要查詢的訂單號(hào)
if (!isset($_POST["out_trade_no"]))
{
$out_trade_no = " ";
}else{
$out_trade_no = $_POST["out_trade_no"];
//使用退款查詢接口
$refundQuery = new RefundQuery_pub();
//設(shè)置必填參數(shù)
//appid已填,商戶無(wú)需重復(fù)填寫
//mch_id已填,商戶無(wú)需重復(fù)填寫
//noncestr已填,商戶無(wú)需重復(fù)填寫
//sign已填,商戶無(wú)需重復(fù)填寫
$refundQuery->setParameter("out_trade_no","$out_trade_no");//商戶訂單號(hào)
// $refundQuery->setParameter("out_refund_no","XXXX");//商戶退款單號(hào)
// $refundQuery->setParameter("refund_id","XXXX");//微信退款單號(hào)
// $refundQuery->setParameter("transaction_id","XXXX");//微信退款單號(hào)
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$refundQuery->setParameter("sub_mch_id","XXXX");//子商戶號(hào)
//$refundQuery->setParameter("device_info","XXXX");//設(shè)備號(hào)
//退款查詢接口結(jié)果
$refundQueryResult = $refundQuery->getResult();
//商戶根據(jù)實(shí)際情況設(shè)置相應(yīng)的處理流程,此處僅作舉例
if ($refundQueryResult["return_code"] == "FAIL") {
echo "通信出錯(cuò):".$refundQueryResult['return_msg']."<br>";
}
else{
echo "業(yè)務(wù)結(jié)果:".$refundQueryResult['result_code']."<br>";
echo "錯(cuò)誤代碼:".$refundQueryResult['err_code']."<br>";
echo "錯(cuò)誤代碼描述:".$refundQueryResult['err_code_des']."<br>";
echo "公眾賬號(hào)ID:".$refundQueryResult['appid']."<br>";
echo "商戶號(hào):".$refundQueryResult['mch_id']."<br>";
echo "子商戶號(hào):".$refundQueryResult['sub_mch_id']."<br>";
echo "設(shè)備號(hào):".$refundQueryResult['device_info']."<br>";
echo "簽名:".$refundQueryResult['sign']."<br>";
echo "微信訂單號(hào):".$refundQueryResult['transaction_id']."<br>";
echo "商戶訂單號(hào):".$refundQueryResult['out_trade_no']."<br>";
echo "退款筆數(shù):".$refundQueryResult['refund_count']."<br>";
echo "商戶退款單號(hào):".$refundQueryResult['out_refund_no']."<br>";
echo "微信退款單號(hào):".$refundQueryResult['refund_idrefund_id']."<br>";
echo "退款渠道:".$refundQueryResult['refund_channel']."<br>";
echo "退款金額:".$refundQueryResult['refund_fee']."<br>";
echo "現(xiàn)金券退款金額:".$refundQueryResult['coupon_refund_fee']."<br>";
echo "退款狀態(tài):".$refundQueryResult['refund_status']."<br>";
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。