本文将和大家介绍app微信支付(2016.10.11)的php(7.0)后台支付和回调接口,框架是thinkphp5.0:希望能帮助到大家。
账户的各种参数
订单信息
请求prepay_id
*返回app数据处理
微信回调
修改订单状态
账户的各种参数账户的各种参数就是像微信申请app支付的时候会给你的账户邮箱发邮件,里面会有对应的微信支付分配的商户号(mchid),appid和appsecret是在申请app支付权限的时候返回的,还有key需要自己在用户微信的商家后台里面自己设置的,这个很重要!避免外泄!
订单信息**客户端会把购物车里面的商品数据传到后台,还包括用户的信息等,拿到数据后首先需要验证这些数据,这里的验证一般都会在项目初期就和客户端定好传输规则,比如传输方式,参数名,验证参数的方式,这里的重点就是验证,一般都会采用签名认证的方式:
//签名步骤一:按字典序排序参数;
这里给出(格式化参数格式化成url参数)代码实例:
/**
* 格式化参数格式化成url参数
*/
public function tourlparams()
{
$buff = "";
foreach ($this->values as $k => $v)
{ if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&"); return $buff;
}
//签名步骤二:在string后加入key(这个key与前端人员协商而定);
//签名步骤三:md5加密;
//签名步骤四:所有字符转为大写
这些最好在项目初期就封装好,后期会只要调用就好,例:
(后期只需要, $param = $this->request('参数名'));即可), 然后将订单信息预存储.
请求prepay_id这里我就直接上代码吧,(这里给出接口文档下载地址:),虽然网上也有很多自己写请求接口,但是既然微信已经封装好了,用就行了:
$input = new \app\wxpay\wxpayunifiedorder();//这里引用微信的统一下单接口
$input->setbody($data['gname']['g_name']);//商品或支付单简要描述
$input->setattach($data['gname']['g_name']);//置附加数据
$input->setout_trade_no($order_sn); // 商户订单号
$input->settotal_fee(intval($data['data']['order_price']*100));
$input->settime_start(date("ymdhis"));//订单生成时间
$input->settime_expire(date("ymdhis", time() + 600));//订单失效时间
$input->setgoods_tag($data['gname']['g_name']); //商品标记
$input->setnotify_url("http://www.weixin.qq.com/wxpay/notify.php"); // 支付成功后的回调地址,
$input->settrade_type("app");
$order = \app\wxpay\wxpayapi::unifiedorder($input);return $order['prepay_id'];
这里给出微信官方统一下单接口说明地址:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
返回客户端信息$info = array();
//账号的信息一般都放在配置文件里面,用到的地方也很多
$info['appid'] = config('app_appid');
$info['partnerid'] = config('app_mchid');
$info['package'] = config('app_package');
$info['noncestr'] = $this->random_number();//生成随机数,下面有生成实例,统一下单接口需要
$info['timestamp'] = time();
$info['prepayid'] = $prepay_id;
$info['sign'] = self::_makesign($info);//生成签名return $info;
$info就是客户端需要的信息啦
生成随机数实例
//生成随机数
public function random_number($len=21,$format='all' ){
$is_abc = $is_numer = 0;
$password = $tmp ='';
switch($format){
case 'all':
$chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; break;
case 'char':
$chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break;
case 'number':
$chars='0123456789'; break;
default :
$chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; break;
} // www.jb51.net
mt_srand((double)microtime()*1000000*getmypid()); while(strlen($password)<$len){
$tmp =substr($chars,(mt_rand()%strlen($chars)),1); if(($is_numer <> 1 && is_numeric($tmp) && $tmp >0 )|| $format == 'char'){
$is_numer = 1;
} if(($is_abc <> 1 && preg_match('/[a-za-z]/',$tmp)) || $format == 'number'){
$is_abc = 1;
}
$password.= $tmp;
} if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
$password = $this->random_number($len,$format);
} return $password;
}
微信回调支付结果通知 notify.php(这里的地址就是统一下单时填写的回调地址,微信已经封装好),文档下载地址
http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
其实这个页面最主要的代码就两行
[php] view plain copy
$notify = new paynotifycallback();
$notify->handle(false);
其中大部分逻辑在 handle 函数中处理 文件 wxpay.notify.php
[php] view plain copy
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);
}
主要代码:
[php] view plain copy
$result = wxpayapi::notify(array($this, 'notifycallback'), $msg);
跟踪函数 notify 文件wxpay.api.php
[php] view plain copy
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 函数验证签名等。验签成功运行代码.
这里需要说明一下,php7本身不支持$globals[‘http_raw_post_data’],需要下载一个插件,具体什么可自行百度,我想说的是可以用file_get_contents(‘php://input’),具体原因可参照下面的博客,写的很详细(https://my.oschina.net/jiec/blog/485359)
[php] view plain copy
return call_user_func($callback, $result);
即调用了一个回调函数,notifycallback() 函数并传递参数 $result 在notifycallback函数中会调用我们重写的notifyprocess()函数(此函数在notify.php 中被重写)
notifyprocess() 判断也没有问题就会 设置返回 success的xml信息
[php] view plain copy
$this->setreturn_code("success");
$this->setreturn_msg("ok");
并最终调用函数 this−>replynotify(this−>replynotify(needsign); echo success的结果
函数replynotify 需要修改一处代码:
[php] view plain copy
final private function replynotify($needsign = true)
{
//如果需要签名
if($needsign == true &&
$this->getreturn_code($return_code) == "success")
{
$this->setsign();
}
wxpayapi::replynotify($this->toxml());
}
[php] view plain copy
$this->getreturn_code($return_code) == "success")
改为
[php] view plain copy
$this->getreturn_code() == "success")
即可。
然后是根据返回信息修改订单状态,主要就是,在什么地方修改,我是在notify.php里面新建了一个方法
//修改订单状态
public function updatestate($data){ if($data){
$order_sn = $data['out_trade_no'];\
$data = array();
$data['order_id'] = $order_id;
//修改订单状态(用curlpost方法请求至thinkphp目录下的controller里面控制器里面的方法,修改状态)
$url = 'www.test.com';
header('content-type:text/html;charset=utf8');
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_returntransfer, true);
curl_setopt($curl, curlopt_post, true);
curl_setopt($curl, curlopt_postfields, $data);
$result = curl_exec($curl);
curl_close($curl); if($result == 'success'){ return true;
}else{ return false;
}
}
}
然后在notify.php 的
$notify = new paynotifycallback();
$notify->handle(false);
下面加上
//接受参数,修改状态
$xml = file_get_contents("php://input");
$data = json_decode(json_encode(simplexml_load_string($xml, 'simplexmlelement', libxml_nocdata)), true);
$notify->updatestate($data);
相关推荐:
php后台接口的一些问题
5 分钟实现微信支付接入教程
php初学者如何学习实现微信支付和支付宝支付的相关教程
以上就是app微信支付之php后台接口详解的详细内容。