PHP微信支付開發實例

這篇文章主要爲大家詳細介紹了PHP微信支付開發過程,具有一定的參考價值,感興趣的小夥伴們可以參考一下

PHP微信支付開發過程,分享給大家,供大家參考,具體內容如下

1.開發環境
Thinkphp 3.2.3
微信:服務號,已認證
開發域名:http://test.paywechat.com (自定義的域名,外網不可訪問)

2.需要相關文件和權限
微信支付需申請開通
微信公衆平臺開發者文檔:http://mp.weixin.qq.com/wiki/home/index.html
微信支付開發者文檔:https://pay.weixin.qq.com/wiki/doc/api/index.html
微信支付SDK下載地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

3.開發
下載好微信支付PHP版本的SDK,文件目錄爲下圖:

這裏寫圖片描述


這裏寫圖片描述

把微信支付SDK的Cert和Lib目錄放入Thinkphp,目錄爲

這裏寫圖片描述



現在介紹微信支付授權目錄問題,首先是微信支付開發配置裏面的支付授權目錄填寫,

這裏寫圖片描述


然後填寫JS接口安全域。

這裏寫圖片描述


最後設置網頁授權

這裏寫圖片描述

這裏寫圖片描述

這些設置完,基本完成一半,注意設置的目錄和我thinkphp裏面的目錄。

這裏寫圖片描述

4.微信支付配置

這裏寫圖片描述

把相關配置填寫正確。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 配置賬號信息
*/
class WxPayConfig
{
//=======【基本信息設置】=====================================
//
/**
* TODO: 修改這裏配置爲您自己申請的商戶信息
* 微信公衆號信息配置
*
* APPID:綁定支付的APPID(必須配置,開戶郵件中可查看)
*
* MCHID:商戶號(必須配置,開戶郵件中可查看)
*
* KEY:商戶支付密鑰,參考開戶郵件設置(必須配置,登錄商戶平臺自行設置)
*
* APPSECRET:公衆帳號secert(僅JSAPI支付的時候需要配置, 登錄公衆平臺,進入開發者中心可設置),
* @var string
*/
const APPID = '';
const MCHID = '';
const KEY = '';
const APPSECRET = '';
//=======【證書路徑設置】=====================================
/**
* TODO:設置商戶證書路徑
* 證書路徑,注意應該填寫絕對路徑(僅退款、撤銷訂單時需要,可登錄商戶平臺下載,
* API證書下載地址:https://pay.weixin.qq.com/index.php/account/api_cert,下載之前需要安裝商戶操作證書)
* @var path
*/
const SSLCERT_PATH = '../cert/apiclient_cert.pem';
const SSLKEY_PATH = '../cert/apiclient_key.pem';
//=======【curl代理設置】===================================
/**
* TODO:這裏設置代理機器,只有需要代理的時候才設置,不需要代理,請設置爲0.0.0.0和0
* 本例程通過curl使用HTTP POST方法,此處可修改代理服務器,
* 默認CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此時不開啓代理(如有需要才設置)
* @var unknown_type
*/
const CURL_PROXY_HOST = "0.0.0.0";//"10.152.18.220";
const CURL_PROXY_PORT = 0;//8080;
//=======【上報信息配置】===================================
/**
* TODO:接口調用上報等級,默認緊錯誤上報(注意:上報超時間爲【1s】,上報無論成敗【永不拋出異常】,
* 不會影響接口調用流程),開啓上報之後,方便微信監控請求調用的質量,建議至少
* 開啓錯誤上報。
* 上報等級,0.關閉上報; 1.僅錯誤出錯上報; 2.全量上報
* @var int
*/
const REPORT_LEVENL = 1;
}

現在開始貼出代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
namespace Wechat\Controller;
use Think\Controller;
/**
* 父類控制器,需要繼承
* @file ParentController.class.php
* @author Gary <[email protected]>
* @date 2015年8月4日
* @todu
*/
class ParentController extends Controller {
protected $options = array (
'token' => '', // 填寫你設定的key
'encodingaeskey' => '', // 填寫加密用的EncodingAESKey
'appid' => '', // 填寫高級調用功能的app id
'appsecret' => '', // 填寫高級調用功能的密鑰
'debug' => false,
'logcallback' => ''
);
public $errCode = 40001;
public $errMsg = "no access";
/**
* 獲取access_token
* @return mixed|boolean|unknown
*/
public function getToken(){
$cache_token = S('exp_wechat_pay_token');
if(!empty($cache_token)){
return $cache_token;
}
$url = sprintf($url,$this->options['appid'],$this->options['appsecret']);
$result = $this->http_get($url);
$result = json_decode($result,true);
if(empty($result)){
return false;
}
S('exp_wechat_pay_token',$result['access_token'],array('type'=>'file','expire'=>3600));
return $result['access_token'];
}
/**
* 發送客服消息
* @param array $data 消息結構{"touser":"OPENID","msgtype":"news","news":{...}}
*/
public function sendCustomMessage($data){
$token = $this->getToken();
if (empty($token)) return false;
$url = sprintf($url,$token);
$result = $this->http_post($url,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 發送模板消息
* @param unknown $data
* @return boolean|unknown
*/
public function sendTemplateMessage($data){
$token = $this->getToken();
if (empty($token)) return false;
$url = sprintf($url,$token);
$result = $this->http_post($url,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
public function getFileCache($name){
return S($name);
}
/**
* 微信api不支持中文轉義的json結構
* @param array $arr
*/
static function json_encode($arr) {
$parts = array ();
$is_list = false;
//Find out if the given array is a numerical array
$keys = array_keys ( $arr );
$max_length = count ( $arr ) - 1;
if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
$is_list = true;
for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
if ($i != $keys [$i]) { //A key fails at position check.
$is_list = false; //It is an associative array.
break;
}
}
}
foreach ( $arr as $key => $value ) {
if (is_array ( $value )) { //Custom handling for arrays
if ($is_list)
$parts [] = self::json_encode ( $value ); /* :RECURSION: */
else
$parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
} else {
$str = '';
if (! $is_list)
$str = '"' . $key . '":';
//Custom handling for multiple data types
if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
$str .= $value; //Numbers
elseif ($value === false)
$str .= 'false'; //The booleans
elseif ($value === true)
$str .= 'true';
else
$str .= '"' . addslashes ( $value ) . '"'; //All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
$parts [] = $str;
}
}
$json = implode ( ',', $parts );
if ($is_list)
return '[' . $json . ']'; //Return numerical JSON
return '{' . $json . '}'; //Return associative JSON
}
/**
+----------------------------------------------------------
* 生成隨機字符串
+----------------------------------------------------------
* @param int $length 要生成的隨機字符串長度
* @param string $type 隨機碼類型:0,數字+大小寫字母;1,數字;2,小寫字母;3,大寫字母;4,特殊字符;-1,數字+大小寫字母+特殊字符
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
static public function randCode($length = 5, $type = 2){
$arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
if ($type == 0) {
array_pop($arr);
$string = implode("", $arr);
} elseif ($type == "-1") {
$string = implode("", $arr);
} else {
$string = $arr[$type];
}
$count = strlen($string) - 1;
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $string[rand(0, $count)];
}
return $code;
}
/**
* GET 請求
* @param string $url
*/
private function http_get($url){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
/**
* POST 請求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上傳
* @return string content
*/
private function http_post($url,$param,$post_file=false){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach($param as $key=>$val){
$aPOST[] = $key."=".urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST,true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
namespace Wechat\Controller;
use Wechat\Controller\ParentController;
/**
* 微信支付測試控制器
* @file TestController.class.php
* @author Gary <[email protected]>
* @date 2015年8月4日
* @todu
*/
class TestController extends ParentController {
private $_order_body = 'xxx';
private $_order_goods_tag = 'xxx';
public function __construct(){
parent::__construct();
require_once ROOT_PATH."Api/lib/WxPay.Api.php";
require_once ROOT_PATH."Api/lib/WxPay.JsApiPay.php";
}
public function index(){
//①、獲取用戶openid
$tools = new \JsApiPay();
$openId = $tools->GetOpenid();
//②、統一下單
$input = new \WxPayUnifiedOrder();
//商品描述
$input->SetBody($this->_order_body);
//附加數據,可以添加自己需要的數據,微信回異步回調時會附加這個數據
$input->SetAttach('xxx');
//商戶訂單號
$out_trade_no = \WxPayConfig::MCHID.date("YmdHis");
$input->SetOut_trade_no($out_trade_no);
//總金額,訂單總金額,只能爲整數,單位爲分
$input->SetTotal_fee(1);
//交易起始時間
$input->SetTime_start(date("YmdHis"));
//交易結束時間
$input->SetTime_expire(date("YmdHis", time() + 600));
//商品標記
$input->SetGoods_tag($this->_order_goods_tag);
//通知地址,接收微信支付異步通知回調地址 SITE_URL=http://test.paywechat.com/Charge
$notify_url = SITE_URL.'/index.php/Test/notify.html';
$input->SetNotify_url($notify_url);
//交易類型
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = \WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
//獲取共享收貨地址js函數參數
$editAddress = $tools->GetEditAddressParameters();
$this->assign('openId',$openId);
$this->assign('jsApiParameters',$jsApiParameters);
$this->assign('editAddress',$editAddress);
$this->display();
}
/**
* 異步通知回調方法
*/
public function notify(){
require_once ROOT_PATH."Api/lib/notify.php";
$notify = new \PayNotifyCallBack();
$notify->Handle(false);
//這裏的IsSuccess是我自定義的一個方法,後面我會貼出這個文件的代碼,供參考。
$is_success = $notify->IsSuccess();
$bdata = $is_success['data'];
//支付成功
if($is_success['code'] == 1){
$news = array(
'touser' => $bdata['openid'],
'msgtype' => 'news',
'news' => array (
'articles'=> array (
array(
'title' => '訂單支付成功',
'description' => "支付金額:{$bdata['total_fee']}\n".
"微信訂單號:{$bdata['transaction_id']}\n"
'picurl' => '',
'url' => ''
)
)
)
);
//發送微信支付通知
$this->sendCustomMessage($news);
}else{//支付失敗
}
}
/**
* 支付成功頁面
* 不可靠的回調
*/
public function ajax_PaySuccess(){
//訂單號
$out_trade_no = I('post.out_trade_no');
//支付金額
$total_fee = I('post.total_fee');
/*相關邏輯處理*/
}

貼上模板HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>微信支付樣例-支付</title>
<script type="text/javascript">
//調用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsApiParameters},
function(res){
WeixinJSBridge.log(res.err_msg);
//取消支付
if(res.err_msg == 'get_brand_wcpay_request:cancel'){
//處理取消支付的事件邏輯
}else if(res.err_msg == "get_brand_wcpay_request:ok"){
/*使用以上方式判斷前端返回,微信團隊鄭重提示:
res.err_msg將在用戶支付成功後返回 ok,但並不保證它絕對可靠。
這裏可以使用Ajax提交到後臺,處理一些日誌,如Test控制器裏面的ajax_PaySuccess方法。
*/
}
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();
}
}
//獲取共享地址
function editAddress()
{
WeixinJSBridge.invoke(
'editAddress',
{$editAddress},
function(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">1分</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>
</div>
</body>
</html>

notify.php文件代碼,這裏有在官方文件裏新添加的一個自定義方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require_once ROOT_PATH."Api/lib/WxPay.Api.php";
require_once ROOT_PATH.'Api/lib/WxPay.Notify.php';
require_once ROOT_PATH.'Api/lib/log.php';
//初始化日誌
$logHandler= new \CLogFileHandler(ROOT_PATH."/logs/".date('Y-m-d').'.log');
$log = \Log::Init($logHandler, 15);
class PayNotifyCallBack extends WxPayNotify
{
protected $para = array('code'=>0,'data'=>'');
//查詢訂單
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;
}
$this->para['code'] = 0;
$this->para['data'] = '';
return false;
}
//重寫回調處理函數
public function NotifyProcess($data, &$msg)
{
\Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
if(!array_key_exists("transaction_id", $data)){
$msg = "輸入參數不正確";
$this->para['code'] = 0;
$this->para['data'] = '';
return false;
}
//查詢訂單,判斷訂單真實性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "訂單查詢失敗";
$this->para['code'] = 0;
$this->para['data'] = '';
return false;
}
$this->para['code'] = 1;
$this->para['data'] = $data;
return true;
}
/**
* 自定義方法 檢測微信端是否回調成功方法
* @return multitype:number string
*/
public function IsSuccess(){
return $this->para;
}
}

到這裏基本上完成,可以在微信端打開http://test.paywechat.com/Charge/index.php/Test/index/
我的環境,HTTP服務器沒有重寫url,微信支付繼續探索中,有些地方可能寫的有問題或不足,望大家諒解,互相學習。

以上就是PHP微信支付開發的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

ps:轉自腳本之家
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章