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

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

服务器之家 - 编程语言 - PHP教程 - 微信支付PHP SDK之微信公众号支付代码详解

微信支付PHP SDK之微信公众号支付代码详解

2020-12-06 17:13PHP教程网 PHP教程

这篇文章主要介绍了微信支付PHP SDK之微信公众号支付代码详解的相关资料,需要的朋友可以参考下

这里假设你已经申请完微信支付

1. 微信后台配置  如图

微信支付PHP SDK之微信公众号支付代码详解

我们先进行测试,所以先把测试授权目录和 测试白名单添加上。测试授权目录是你要发起微信请求的哪个文件所在的目录。

例如jsapi 发起请求一般是jsapi.php所在目录 为测试目录,测试白名单即开发人员的微信号。

正式的支付授权目录不能和测试的一样否则会报错。不填写或者填错授权目录以及测试白名单都会报错。

报错样例:

nansystem:access_denied

微信支付PHP SDK之微信公众号支付代码详解

不在测试白名单

微信支付PHP SDK之微信公众号支付代码详解

2. 配置 lib/wxpay.config.php文件

最主要配置一下四项:

const appid = '';
const mchid = '';
const key = '';
const appsecret = '';
appid 和 appsecret都可以在微信后台中找到。
mchid 在申请微信支付后发来的邮件中可以找到,key 则根据邮件提示

微信支付PHP SDK之微信公众号支付代码详解

去商户平台配置即可。

3. 访问起始 index.php

首先访问 index.php 你可以看到界面

微信支付PHP SDK之微信公众号支付代码详解

我们首先需要的是 jsapi支付。但是看代码 index.php 最下面的链接。他默认是个demo的链接,改为我们自定义的即可

 
?
1
 
2
3
4
5
6
7
8
9
<ul>
  <li style="background-color:#ff7f24"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/jsapi.php';?>">jsapi支付</a></li>
  <li style="background-color:#698b22"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/micropay.php';?>">刷卡支付</a></li>
  <li style="background-color:#8b6914"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/native.php';?>">扫码支付</a></li>
  <li style="background-color:#cdcd00"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/orderquery.php';?>">订单查询</a></li>
  <li style="background-color:#cd3278"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/refund.php';?>">订单退款</a></li>
  <li style="background-color:#848484"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/refundquery.php';?>">退款查询</a></li>
  <li style="background-color:#8ee5ee"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/download.php';?>">下载订单</a></li>
</ul>

当然你也可以直接写死为自己的访问链接。

4. jsapi 支付

必要代码解析:

 
?
1
 
2
$loghandler= new clogfilehandler("../logs/".date('y-m-d').'.log');
$log = log::init($loghandler, 15);

调用日志类 可以通过 $log->debug(‘test‘); 打印调试信息。其实也可以直接使用 $log::debug(‘test‘); 来调试

 
?
1
 
2
$tools = new jsapipay();
$openid = $tools->getopenid();

主要是为了获取 openid 其中getopenid() 函数定义在 文件 wxpay.jsapipay.php 文件中

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public function getopenid()
 {
 //通过code获得openid
 if (!isset($_get['code'])){
  //触发微信返回code码
  $baseurl = urlencode('http://'.$_server['http_host'].$_server['php_self'].$_server['query_string']);
  $url = $this->__createoauthurlforcode($baseurl);
  header("location: $url");
  exit();
 } else {
  //获取code码,以获取openid
   $code = $_get['code'];
  $openid = $this->getopenidfrommp($code);
  return $openid;
 }
 }

$baseurl 其实就是为了在跳转回来这个页面。  可以继续跟踪函数__createoauthurlforcode()  其实就是通过微信的auth2.0 来获取openid

参考链接:

这就需要你把微信的 网页授权接口也设置好。

获取到 openid 就可以调用微信支付的统一下单接口了。回到 文件 jsapi.php 如下代码

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$input = new wxpayunifiedorder();
$input->setbody("test");
$input->setattach("test");
$input->setout_trade_no(wxpayconfig::mchid.date("ymdhis"));
$input->settotal_fee("1");
$input->settime_start(date("ymdhis"));
$input->settime_expire(date("ymdhis", time() + 600));
$input->setgoods_tag("test");
$input->setnotify_url("http://paysdk.weixin.qq.com/example/notify.php");
$input->settrade_type("jsapi");
$input->setopenid($openid);
$order = wxpayapi::unifiedorder($input);
echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
printf_info($order);
$jsapiparameters = $tools->getjsapiparameters($order);

这里面的代码:

 
?
1
 
$input->setattach("test");

如果 把值改为 $input->setattach("test this is attach");就会存在bug 后面再说,其实这个参数不是必须的干脆可以去掉。

代码:

 
?
1
 
$input->setnotify_url(http://paysdk.weixin.qq.com/example/notify.php);

是设置接收支付结果通知的url 这里是默认的demo 链接我们可以设置成我们的:

 
?
1
 
$input->setnotify_url(dirname('http://'.$_server['http_host'].$_server['request_uri']).'/notify.php');

当然你也可以选择直接写死。
其中的函数 unifiedorder($input) 可以到wxpay.api.php 中文件跟踪,其实就是调用统一下单接口。

在 wxpay.api.php 中需要更改的一处代码是:

 
?
1
 
2
3
4
//异步通知url未设置,则使用配置文件中的url
    if(!$inputobj->isnotify_urlset()){
      $inputobj->setnotify_url(wxpayconfig::notify_url);//异步通知url
    }

就是当没设置 notifyurl 的时候回去配置文件中找,但是配置文件中根本没有设置。

所以你可以选择在 配置文件wxpay.config.php 中加上这个配置,也可以直接写一个默认的notify链接。

函数 getjsapiparameters() 是获取jsapi支付的参数给变量 $jsapiparameters 方便在下面的js中调用

jsapi.php 中js的代码:

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function jsapicall()
 {
 weixinjsbridge.invoke(
  'getbrandwcpayrequest',
  <?php echo $jsapiparameters; ?>,
  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();
 }
 }

其中点击立即支付按钮调用的就是 callpay() 函数,他有会调用jsapicall() 函数打开支付程序。
此后输入密码完成支付。

在完成支付页面点击完成会回到这个支付页面,并弹出 支付成功的提示框

微信支付PHP SDK之微信公众号支付代码详解

这个其实就是 js函数 jsapicall 里面的alter 弹出的对话框

其中 res.err_msg 为get_brand_wcpay_request:ok 表明前端判断的支付成功,我们可以根据这个将支付跳转到成功页面。

但是这个并不可信。确认是否支付成功还是应当 通过notify.php 处理业务逻辑。

5. 支付结果通知 notify.php

其实这个页面最主要的代码就两行

 
?
1
 
2
$notify = new paynotifycallback();
$notify->handle(false);

其中大部分逻辑在 handle 函数中处理 文件 wxpay.notify.php

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
final public function handle($needsign = true)
 {
 $msg = "ok";
 //当返回false的时候,表示notify中调用notifycallback回调失败获取签名校验失败,此时直接回复失败
 $result = wxpayapi::notify(array($this, 'notifycallback'), $msg);
 if($result == false){
  $this->setreturn_code("fail");
  $this->setreturn_msg($msg);
  $this->replynotify(false);
  return;
 } else {
  //该分支在成功回调到notifycallback方法,处理完成之后流程
  $this->setreturn_code("success");
  $this->setreturn_msg("ok");
 }
 $this->replynotify($needsign);
 }

主要代码:

 
?
1
 
$result = wxpayapi::notify(array($this, 'notifycallback'), $msg);

跟踪函数 notify 文件wxpay.api.php

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
public static function notify($callback, &$msg)
 {
 //获取通知的数据
 $xml = $globals['http_raw_post_data'];
 //如果返回成功则验证签名
 try {
  $result = wxpayresults::init($xml);
 } catch (wxpayexception $e){
  $msg = $e->errormessage();
  return false;
 }
 
 return call_user_func($callback, $result);
 }

通过 $globals[‘http_raw_post_data‘]; 获取同志数据 然后 init 函数验证签名等。验签成功运行代码

 
?
1
 
return call_user_func($callback, $result);

即调用了一个回调函数,notifycallback() 函数并传递参数 $result 在notifycallback函数中会调用我们重写的notifyprocess()函数(此函数在notify.php 中被重写)

notifyprocess() 判断也没有问题就会 设置返回 success的xml信息

 
?
1
 
2
$this->setreturn_code("success");
$this->setreturn_msg("ok");

并最终调用函数 $this->replynotify($needsign);  echo success的结果

函数replynotify 需要修改一处代码:

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
final private function replynotify($needsign = true)
 {
 //如果需要签名
 if($needsign == true &&
  $this->getreturn_code($return_code) == "success")
 {
  $this->setsign();
 }
 wxpayapi::replynotify($this->toxml());
 }
 
$this->getreturn_code($return_code) == "success")

改为

 
?
1
 
$this->getreturn_code() == "success")

即可。

这样整个流程就结束了。上面提到了 传递订单参数

 
?
1
 
$input->setattach("test");

如果我设置 值为 test this is attach (其实只要有空格就会存在bug)
如图 传递的订单信息

微信支付PHP SDK之微信公众号支付代码详解

可以看到 attach 信息正常,当然支付也是正常的没有任何问题。

但是发现总是会收到notify 通知,即意味着没有返回给微信服务器正确的结果通知。

打印服务器发来的通知数据

微信支付PHP SDK之微信公众号支付代码详解

可以看到 attach 是 test+this+is+attach 即空格被转化为加号

打印接收到的签名和程序算出来的签名发现 签名不同,即认为接收结果异常。

所以我们要是想使用attach 这个值就不能有空格,要么干脆不使用这个参数

(等待微信修复这个bug, 也可能是我这边有哪个地方不会? - -#)

这样 微信支付的 jsapi支付就大致分析完成了。

延伸 · 阅读

精彩推荐