WxPay.
開(kāi)頭的文件復(fù)制到lib文件夾中require_once中的路徑去掉,改為當(dāng)前文件夾下調(diào)用
WxPay.Config.php
中的配置信息
<?php
/**
*
* example目錄下為簡(jiǎn)單的支付樣例,僅能用于搭建快速體驗(yàn)微信支付使用
* 樣例的作用僅限于指導(dǎo)如何使用sdk,在安全上面僅做了簡(jiǎn)單處理, 復(fù)制使用樣例代碼時(shí)請(qǐng)慎重
* 請(qǐng)勿直接直接使用樣例對(duì)外提供服務(wù)
*
**/
require_once "WxPay.Config.Interface.php";
/**
*
* 該類需要業(yè)務(wù)自己繼承, 該類只是作為deamon使用
* 實(shí)際部署時(shí),請(qǐng)務(wù)必保管自己的商戶密鑰,證書(shū)等
*
*/
class WxPayConfig extends WxPayConfigInterface
{
//=======【基本信息設(shè)置】=====================================
/**
* TODO: 修改這里配置為您自己申請(qǐng)的商戶信息
* 微信公眾號(hào)信息配置
*
* APPID:綁定支付的APPID(必須配置,開(kāi)戶郵件中可查看)
*
* MCHID:商戶號(hào)(必須配置,開(kāi)戶郵件中可查看)
*
*/
public function GetAppId()
{
return 'AppId';
}
public function GetMerchantId()
{
return '微信支付商戶號(hào)';
}
//=======【支付相關(guān)配置:支付成功回調(diào)地址/簽名方式】===================================
/**
* TODO:支付回調(diào)url
* 簽名和驗(yàn)證簽名方式, 支持md5和sha256方式
**/
public function GetNotifyUrl()
{
return "";
}
public function GetSignType()
{
return "HMAC-SHA256";
}
//=======【curl代理設(shè)置】===================================
/**
* TODO:這里設(shè)置代理機(jī)器,只有需要代理的時(shí)候才設(shè)置,不需要代理,請(qǐng)?jiān)O(shè)置為0.0.0.0和0
* 本例程通過(guò)curl使用HTTP POST方法,此處可修改代理服務(wù)器,
* 默認(rèn)CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此時(shí)不開(kāi)啟代理(如有需要才設(shè)置)
* @var unknown_type
*/
public function GetProxy(&$proxyHost, &$proxyPort)
{
$proxyHost = "0.0.0.0";
$proxyPort = 0;
}
//=======【上報(bào)信息配置】===================================
/**
* TODO:接口調(diào)用上報(bào)等級(jí),默認(rèn)緊錯(cuò)誤上報(bào)(注意:上報(bào)超時(shí)間為【1s】,上報(bào)無(wú)論成敗【永不拋出異?!?,
* 不會(huì)影響接口調(diào)用流程),開(kāi)啟上報(bào)之后,方便微信監(jiān)控請(qǐng)求調(diào)用的質(zhì)量,建議至少
* 開(kāi)啟錯(cuò)誤上報(bào)。
* 上報(bào)等級(jí),0.關(guān)閉上報(bào); 1.僅錯(cuò)誤出錯(cuò)上報(bào); 2.全量上報(bào)
* @var int
*/
public function GetReportLevenl()
{
return 1;
}
//=======【商戶密鑰信息-需要業(yè)務(wù)方繼承】===================================
/*
* KEY:商戶支付密鑰,參考開(kāi)戶郵件設(shè)置(必須配置,登錄商戶平臺(tái)自行設(shè)置), 請(qǐng)妥善保管, 避免密鑰泄露
* 設(shè)置地址:https://pay.weixin.qq.com/index.php/account/api_cert
*
* APPSECRET:公眾帳號(hào)secert(僅JSAPI支付的時(shí)候需要配置, 登錄公眾平臺(tái),進(jìn)入開(kāi)發(fā)者中心可設(shè)置), 請(qǐng)妥善保管, 避免密鑰泄露
* 獲取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
* @var string
*/
public function GetKey()
{
return '商戶支付密鑰';
}
public function GetAppSecret()
{
return 'AppSecert';
}
//=======【證書(shū)路徑設(shè)置-需要業(yè)務(wù)方繼承】=====================================
/**
* TODO:設(shè)置商戶證書(shū)路徑
* 證書(shū)路徑,注意應(yīng)該填寫(xiě)絕對(duì)路徑(僅退款、撤銷訂單時(shí)需要,可登錄商戶平臺(tái)下載,
* API證書(shū)下載地址:https://pay.weixin.qq.com/index.php/account/api_cert,下載之前需要安裝商戶操作證書(shū))
* 注意:
* 1.證書(shū)文件不能放在web服務(wù)器虛擬目錄,應(yīng)放在有訪問(wèn)權(quán)限控制的目錄中,防止被他人下載;
* 2.建議將證書(shū)文件名改為復(fù)雜且不容易猜測(cè)的文件名;
* 3.商戶服務(wù)器要做好病毒和木馬防護(hù)工作,不被非法侵入者竊取證書(shū)文件。
* @var path
*/
public function GetSSLCertPath(&$sslCertPath, &$sslKeyPath)
{
$sslCertPath = '../cert/apiclient_cert.pem';
$sslKeyPath = '../cert/apiclient_key.pem';
}
}
AppId
和AppSecert
是微信公眾號(hào)(服務(wù)號(hào))申請(qǐng)時(shí)的,申請(qǐng)的微信公眾號(hào)必須已經(jīng)通過(guò)微信認(rèn)證。微信支付商戶號(hào)
和商戶密鑰
是在微信支付的官方申請(qǐng)的商戶號(hào),申請(qǐng)后會(huì)有一個(gè)商戶密鑰Pay.php
\
代表當(dāng)前文件下的類。
<?php
namespace App\Http\Controllers\homes;
use App\Http\Controllers\Controller;
use Exception;
//手動(dòng)驗(yàn)證用戶名和密碼
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
//引入數(shù)據(jù)庫(kù)查詢構(gòu)造器,鏈?zhǔn)秸{(diào)用
use Illuminate\Support\Facades\DB;
//引入redis
use Illuminate\Support\Facades\Redis;
//用戶支付控制器
class Pay extends Controller
{
public function wxPay()
{
$dir = __DIR__.'\../../../../vendor/wxPay_sdk_v3.0.10/lib/';
require_once $dir."WxPay.Api.php";
require_once $dir."WxPay.JsApiPay.php";
require_once $dir."WxPay.Config.php";
//①、獲取用戶openid
try{
//計(jì)算總金額
//通過(guò)Auth的自定義guard(門衛(wèi))獲取當(dāng)前用戶登錄id
$memberID = Auth::guard('member')->id();
//然后讀取redis中該用戶的所有點(diǎn)菜數(shù)據(jù)
$orderData =json_decode(Redis::get('order'.$memberID),true);
$res = [];
$res['priceTotal'] = 0;
if(is_array($orderData))
{
foreach($orderData as $key => $ordData)
{
$num =(int)$ordData['num'];
$prices =json_decode($ordData['dish']['price'],true);
$orderData[$key]['price'] = $prices[$ordData['priceKey']];
$units = json_decode($ordData['dish']['unit'],true);
$orderData[$key]['unit'] = $units[$ordData['priceKey']];
$res['priceTotal']+=(float)$prices[$ordData['priceKey']]*$num;
}
}
//調(diào)起微信支付jsApi接口
$tools = new \JsApiPay();
$openId = $tools->GetOpenid();
//②、統(tǒng)一下單
$input = new \WxPayUnifiedOrder();
$input->SetBody("test");
$input->SetAttach("test");
$input->SetOut_trade_no("sdkphp".date("YmdHis"));
$input->SetTotal_fee((string)($res['priceTotal']*100));
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("支付成功回調(diào)地址");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$config = new \WxPayConfig();
$order = \WxPayApi::unifiedOrder($config, $input);
echo '<font color="#f00"><b>統(tǒng)一下單支付單信息</b></font><br/>';
// $this->printf_info($order);
$res['jsApiParameters'] = $tools->GetJsApiParameters($order);
//獲取共享收貨地址js函數(shù)參數(shù)
$res['editAddress'] = $tools->GetEditAddressParameters();
return view('/homes/pay/wxPay',$res);
}
catch(Exception $e)
{
echo $e;
exit;
// Log::ERROR(json_encode($e));
}
}
//打印輸出數(shù)組信息
public function printf_info($data)
{
foreach($data as $key=>$value){
echo "<font color='#00ff55;'>$key</font> : ".htmlspecialchars($value, ENT_QUOTES)." <br/>";
}
}
public function wxPayCallBack()
{
echo 'wxsuccsss';
}
}
wxPay.blade.php
jsApiParameters
和editAddress
是json數(shù)據(jù),必須解析后使用,否則無(wú)法調(diào)起支付
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<title>微信支付</title>
<script type="text/javascript">
layui.use(['layer'],function(){
$ = layui.jquery;
});
//調(diào)用微信JS api 支付
function jsApiCall()
{
var jsApiPar =JSON.parse($('input[name="jsApiPar"]').val());
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
jsApiPar,
function(res){
WeixinJSBridge.log(res.err_msg);
alert(res.err_code+res.err_desc+res.err_msg);
}
);
}
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>
<script type="text/javascript">
//獲取共享地址
function editAddress()
{
var editAdd =JSON.parse($('input[name="editAdd"]').val());
WeixinJSBridge.invoke(
'editAddress',
editAdd,
function(res){
$('div[name="test"]').text(JSON.parse(res));
var value1 = res.proviceFirstStageName;
var value2 = res.addressCitySecondStageName;
var value3 = res.addressCountiesThirdStageName;
var value4 = res.addressDetailInfo;
var tel = res.telNumber;
alert(value1 + value2 + value3 + value4 + ":" + tel);
}
);
}
window.onload = function(){
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', editAddress, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', editAddress);
document.attachEvent('onWeixinJSBridgeReady', editAddress);
}
}else{
editAddress();
}
};
</script>
</head>
<body>
<br/>
<font color="#9ACD32"><b>該筆訂單支付金額為<span style="color:#f00;font-size:50px">{{$priceTotal}}</span>元</b></font><br/><br/>
<div align="center">
<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >立即支付</button>
<input type="hidden" name="jsApiPar" value="{{$jsApiParameters}}">
<input type="hidden" name="editAdd" value="{{$editAddress}}">
</div>
</body>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)