服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - PHP微信支付开发实例

PHP微信支付开发实例

2021-01-29 16:07xy2204 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,文件目录为下图:

PHP微信支付开发实例

PHP微信支付开发实例

把微信支付sdk的cert和lib目录放入thinkphp,目录为

PHP微信支付开发实例

现在介绍微信支付授权目录问题,首先是微信支付开发配置里面的支付授权目录填写,

PHP微信支付开发实例

然后填写js接口安全域。

PHP微信支付开发实例

最后设置网页授权

PHP微信支付开发实例

PHP微信支付开发实例

这些设置完,基本完成一半,注意设置的目录和我thinkphp里面的目录。

PHP微信支付开发实例

4.微信支付配置

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
/**
* 配置账号信息
*/
 
class wxpayconfig
{
 //=======【基本信息设置】=====================================
 //
 /**
 * todo: 修改这里配置为您自己申请的商户信息
 * 微信公众号信息配置
 *
 * appid:绑定支付的appid(必须配置,开户邮件中可查看)
 *
 * mchid:商户号(必须配置,开户邮件中可查看)
 *
 * key:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
 * 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
 *
 * appsecret:公众帐号secert(仅jsapi支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
 * 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_cn
 * @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 <lizhiyong2204@sina.com>
 * @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 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
 $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 = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s';
 $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 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
 $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 <lizhiyong2204@sina.com>
 * @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微信支付开发的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐