PHP微信H5支付開發(fā)實(shí)例
最近由于業(yè)務(wù)所需,對接了微信H5支付,然而微信支付對這塊并沒有現(xiàn)成的demo可用,所以就必須自己老老實(shí)實(shí)對照開發(fā)文檔去寫咯!但這對于剛接觸的童鞋來說,坑多多少少還是有的,所以尋思著把自己的經(jīng)驗(yàn)分享出來,畢竟現(xiàn)成的用的還是多巴適的嘛!
好了,官方文檔的那一套就不多說了,詳情見官方文檔。
在這里,我主要分成了三個(gè)文件:WxPay.Config.php(支付配置文件)、Weixin.class.php(支付類)以及PayMentController.class.php(支付文件)。
首先,WxPay.Config.php配置文件主要包含了商戶appId、商戶號、商家key、異步回調(diào)URL、支付場景信息,如下:
class WxPayConfig
{
public static $appid = '微信支付的公眾號appid';
public static $mchid = '微信支付分配的商戶號';
public static $key = '微信商戶自己設(shè)置的安全key';
public static $notify_url = '商戶側(cè)接收微信支付異步通知的URL';
public static $scene_info = '{"h5_info":{"type":"Wap","wap_url":" 發(fā)起微信H5支付H5的URL","wap_name":"支付"}}';
}
然后,封裝Weixin.class.php支付類,主要調(diào)用統(tǒng)一下單Api,這里不多說了,直接上代碼:
<?php
require_once "lib/WxPay.Config.php";
class Weixin
{
/**
* 微信H5下單付款
* @order 付款信息
* @bodys 付款內(nèi)容
* */
function getCode($order,$bodys){
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信傳參地址
//1.獲取調(diào)用統(tǒng)一下單接口所需必備參數(shù)
$appid =WxPayConfig::$appid;//微信公眾號appid
$mch_id = WxPayConfig::$mchid;//微信支付商戶號
$key = WxPayConfig::$key;//自己設(shè)置的微信商家key
$out_trade_no = $order['order_sn'];//平臺(tái)內(nèi)部訂單號
$nonce_str=MD5($out_trade_no);//隨機(jī)字符串
$body = $bodys;//付款內(nèi)容
$total_fee = $order['order_amount']*100;//付款金額,單位為分
$spbill_create_ip = getIP(); //獲得用戶設(shè)備IP
$attach = 'weixinh5';//附加數(shù)據(jù)(自定義,在支付通知中原樣返回)
$notify_url = WxPayConfig::$notify_url; //異步回調(diào)地址,需外網(wǎng)可以直接訪問
$trade_type = 'MWEB';//交易類型,微信H5支付時(shí)固定為MWEB
$scene_info =WxPayConfig::$scene_info;//場景信息
//2.將參數(shù)按照key=value的格式,并按照參數(shù)名ASCII字典序排序生成字符串
$signA ="appid=$appid&attach=$attach&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
//3.拼接字符串
$strSignTmp = $signA."&key=$key";
//4.MD5加密后轉(zhuǎn)換成大寫
$sign = strtoupper(MD5($strSignTmp));
//5.拼接成所需XML格式
$post_data = "<xml>
<appid>$appid</appid>
<attach>$attach</attach>
<body>$body</body>
<mch_id>$mch_id</mch_id>
<nonce_str>$nonce_str</nonce_str>
<notify_url>$notify_url</notify_url>
<out_trade_no>$out_trade_no</out_trade_no>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<total_fee>$total_fee</total_fee>
<trade_type>$trade_type</trade_type>
<scene_info>$scene_info</scene_info>
<sign>$sign</sign>
</xml>";
//6.以POST方式向微信傳參,并取得微信返回的支付參數(shù)
$dataxml = httpRequest($url,'POST',$post_data);
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //將微信返回的XML轉(zhuǎn)換成數(shù)組
return $objectxml;
}
}
最后,PayMentController.class.php支付文件,支付文件接收前端發(fā)起支付的請求并處理后,調(diào)用Weixin.class.php支付類并接受結(jié)果后返回給前端(此處分享已經(jīng)去掉接口驗(yàn)證等系列代碼邏輯):
public function getPay(){
//1.引入支付類文件
include_once "plugins/Payment/weixin/Weixin.class.php";
$payment = new \Weixin();
$order_id = I('order_id');
//2.判斷參數(shù)是否為空
if (!empty($order_id)){
//3.根據(jù)訂單id查詢訂單是否存在
$order = M('Order')->where(array('id'=>$order_id))->find();
if ($order){//訂單存在
//4.判斷該筆訂單是否已經(jīng)支付,如已支付則返回支付失敗并給出相應(yīng)提示
if ($order['pay_status'] == '1'){
exit(json_encode(array('status'=>'205','msg'=>'該訂單已支付,請勿重復(fù)提交!')));
}
$bodys = '訂單:'.$order['order_sn'] . '支付';
//5.調(diào)用支付類中封裝的支付方法并對應(yīng)傳參
$result = $payment->getCode($order,$bodys);
//6.當(dāng)return_code和result_code均為SUCCESS,代表下單成功,將支付參數(shù)返回
if($result['return_code'] == 'SUCCESS'){
if($result['result_code'] == 'SUCCESS'){
exit(json_encode(array('status'=>'0','msg'=>'下單成功,請支付!','result'=>$result['mweb_url'])));
}elseif($result['result_code'] == 'FAIL'){
exit(json_encode(array('status'=>'-201','msg'=>$result['err_code_des'])));
}
}else{
exit(json_encode(array('status'=>'-1','msg'=>'未知錯(cuò)誤,請稍后重試!')));
}
}else{
//報(bào)錯(cuò):數(shù)據(jù)不存在
exit(json_encode(array('status'=>'-200','msg'=>'訂單不存在,請核實(shí)后再提交!')));
}
}else{
//報(bào)錯(cuò):缺少參數(shù)
exit(json_encode(array('status'=>'-204','msg'=>'參數(shù)缺失,請核實(shí)!')));
}
}
前端在接收到支付URL后執(zhí)行即可喚醒微信支付。
附一:獲取用戶終端設(shè)備ip方法
function getIP(){
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else $ip = "Unknow";
return $ip;
}
######附二:CURL請求方法
/**
* CURL請求
* @param $url 請求url地址
* @param $method 請求方法 get post
* @param null $postfields post數(shù)據(jù)數(shù)組
* @param array $headers 請求header信息
* @param bool|false $debug 調(diào)試開啟 默認(rèn)false
* @return mixed
*/
function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) {
$method = strtoupper($method);
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在發(fā)起連接前等待的時(shí)間,如果設(shè)置為0,則無限等待 */
curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 設(shè)置cURL允許執(zhí)行的最長秒數(shù) */
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case "POST":
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
$tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
}
break;
default:
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //設(shè)置請求方式 */
break;
}
$ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
curl_setopt($ci, CURLOPT_URL, $url);
if($ssl){
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https請求 不驗(yàn)證證書和hosts
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不從證書中檢查SSL加密算法是否存在
}
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的數(shù)量,這個(gè)選項(xiàng)是和CURLOPT_FOLLOWLOCATION一起使用的*/
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ci);
$requestinfo = curl_getinfo($ci);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo "=====info===== \r\n";
print_r($requestinfo);
echo "=====response=====\r\n";
print_r($response);
}
curl_close($ci);
return $response;
}
好了,一點(diǎn)點(diǎn)菜鳥心得,有不當(dāng)之處歡迎留言指證交流,一起成長!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php 論壇采集程序 模擬登陸,抓取頁面 實(shí)現(xiàn)代碼
php 論壇采集程序 模擬登陸,抓取頁面 實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-07-07
Zend Framework入門之環(huán)境配置及第一個(gè)Hello World示例(附demo源碼下載)
這篇文章主要介紹了Zend Framework入門之環(huán)境配置及第一個(gè)Hello World示例,詳細(xì)講述了Zend Framework環(huán)境搭建與配置,以及實(shí)現(xiàn)第一個(gè)Hello World程序的方法,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03
Yii框架學(xué)習(xí)筆記之session與cookie簡單操作示例
這篇文章主要介紹了Yii框架學(xué)習(xí)筆記之session與cookie簡單操作,結(jié)合實(shí)例形式分析了Yii框架session與cookie的定義、設(shè)置、讀寫、刪除等簡單操作技巧,需要的朋友可以參考下2019-04-04
PHP連接SQL Server的方法分析【基于thinkPHP5.1框架】
這篇文章主要介紹了PHP連接SQL Server的方法,結(jié)合實(shí)例形式分析了基于thinkPHP5.1框架Db類以及使用PDO進(jìn)行SQL Server數(shù)據(jù)庫連接的相關(guān)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05
使用swoole 定時(shí)器變更超時(shí)未支付訂單狀態(tài)的解決方案
本文主要是借助 swoole 定時(shí)器和 redis 的 zset 來實(shí)現(xiàn)的定時(shí)檢查并過期未支付訂單,感興趣的朋友跟隨小編一起看看吧2019-07-07
Laravel框架環(huán)境與配置操作實(shí)例分析
這篇文章主要介紹了Laravel框架環(huán)境與配置操作,結(jié)合實(shí)例形式分析了laravel框架基本環(huán)境配置方法及維護(hù)模式相關(guān)操作技巧,需要的朋友可以參考下2019-12-12

