PHP 接入微信扫码支付总结(总结篇)


Posted in PHP onNovember 03, 2016

PHP 接入微信扫码支付总结(总结篇)

PHP 接入微信扫码支付总结(总结篇)

PHP 接入微信扫码支付总结(总结篇)

PHP 接入微信扫码支付总结(总结篇)

PHP 接入微信扫码支付总结(总结篇)

微信扫码支付分为两种模式,

模式一比较复杂,需要公众号配置回调地址。

模式二比较简单,只需要在代码中配置回调地址就可以了。

我这次使用的是模式二。

需要配置参数,

const APPID = 'xxx';
const MCHID = 'xxx';
const KEY = 'xxx';
const APPSECRET = 'xxx';

配置公众号的appid,appsecret。以及微信支付的mchid与key。

生成二维码,这个页面需要自己去美化,不像支付宝那样自带效果。

require_once "./phpcms/plugin/weixinpay/lib/WxPay.Api.php";
require_once "./phpcms/plugin/weixinpay/example/WxPay.NativePay.php";
require_once './phpcms/plugin/weixinpay/example/log.php';
$input = new WxPayUnifiedOrder();
$input->SetBody('预订'.$product_info['name'].'订单');
$input->SetAttach('预订'.$product_info['name'].'订单');
$input->SetOut_trade_no($order_info['orderno']);
$input->SetTotal_fee($order_info['payprice'] * 100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("");
$input->SetNotify_url("http://www.ayuanduanzu.com/wxpay/notify.php"); // 地址是外网能访问的,且不能包含参数
$input->SetTrade_type("NATIVE");
$input->SetProduct_id($product_info['id']);
$notify = new NativePay();
$result = $notify->GetPayUrl($input);
$code_url = $result["code_url"];
<img alt="扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data={urlencode($code_url)}" style="width:150px;height:150px;"/>

这里的回调地址很有讲究,扫码支付成功后,微信会自动调用这个地址。这个地址不能包含任何参数,否则调用失败。啥都看不到!

微信调用的时候,会传递xml类型的参数。

include_once "../phpcms/base.php";
// 处理回调数据
error_reporting(E_ERROR);
require_once "../phpcms/plugin/weixinpay/lib/WxPay.Api.php";
require_once '../phpcms/plugin/weixinpay/lib/WxPay.Notify.php';
require_once '../phpcms/plugin/weixinpay/example/log.php';
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
class PayNotifyCallBack extends WxPayNotify
//查询订单
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;
}
return false;
}
//重写回调处理函数
public function NotifyProcess($data, &$msg)
{
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
if(!array_key_exists("transaction_id", $data)){
$msg = "输入参数不正确";
return false;
}
//查询订单,判断订单真实性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
return true;
}
Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$ilog_db = pc_base::load_model('ilog_model');
$order_db = pc_base::load_model('order_model');
$postXml = $GLOBALS["HTTP_RAW_POST_DATA"];
$postArr = xmlToArray($postXml);
// 查询是否支付成功
$r = $notify->Queryorder($postArr['transaction_id']);
if ($r) {
// 获取订单信息
$order_info = $order_db->get_one(array('orderno'=>$postArr['out_trade_no']));
if ($order_info['pay_status'] != '1') {
$data['pay_status'] = '1';
$data['pay_type'] = 'weixinpay';
$data['pay_code'] = $postArr['transaction_id'];
$data['paytime'] = time();
$data['order_status']= 3; // 已支付
$order_db->update($data,array('orderno'=>$postArr['out_trade_no']));
}
?>

通过

$GLOBALS["HTTP_RAW_POST_DATA"];

可以获取微信端传入的参数

{
"appid": "wxed7996e9ad58345d",
"attach": "u9884u8ba2u5bbfu8fc1u00b7u592au53e4u91ccu7f8eu5f0fu5957u623fu8ba2u5355",
"bank_type": "CFT",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "1283301801",
"nonce_str": "20xn5e0lbk2u1u6pes2uonape2sdyfs4",
"openid": "oR8C7wsknwVELIRrzTlZX2eONWeY",
"out_trade_no": "2016091455521024608",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "95C2C532D095E7BF7588522C579758C4",
"time_end": "20160914135518",
"total_fee": "1",
"trade_type": "NATIVE",
"transaction_id": "4009602001201609143926590576"
}

查询是否已支付,支付完成的话,进行订单数据处理。

这里的一切都是异步的,二维码页面啥都看不到。

只能通过异步获取订单状态来判断是否操作成功。

// 检测是否支付成功
$(document).ready(function () {
setInterval("ajaxstatus()", 3000); 

function ajaxstatus() {
var orderno = $("#out_trade_no").val();
if (orderno != 0) {
$.ajax({
url: "?m=home&c=order&a=ajax",
type: "GET",
dataType:"json",
data: {
todo: 'ajaxCheckWxPay',
orderno: orderno,
},
success: function (json) {
if (json.status == 1) { //订单状态为1表示支付成功
layer.msg('支付成功',{icon:1,time: 2000},function(){
window.location.href = "?m=home&c=order&a=payDone&orderno="+json.info['orderno'];
});
// window.location.href = "wxScanSuccessUrl.action"; //页面跳转
}
}
});
}
}

三秒执行一次,如果成功,进行跳转处理。

赠送函数

* 作用:array转xml
*/
function arrayToXml($arr)
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">"; 
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; 
}
$xml.="</xml>";
return $xml; 

* 作用:将xml转为array
*/
function xmlToArray($xml)
{ 
//将XML转为array 
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); 
return $array_data;
}

赠送小窍门

对于异步的调用,如果看不到效果。可以建一个日志表,把操作的数据记录在表中。便于测试。支付回调都是异步的,可以通过日志表中的数据来判断是否支付成功,是否调用了回调,调用了几次。

小结:

微信扫码支付不如支付宝扫码支付便捷。需要自己做很多处理。

以上所述是小编给大家介绍的PHP 微信扫码支付接入总结(总结篇),数据库显示空白的完美解决方案(图文教程),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
实现分十页分向前十页向后十页的处理
Oct 09 PHP
用PHP连接Oracle for NT 远程数据库
Oct 09 PHP
繁体中文转换为简体中文的PHP函数
Oct 09 PHP
PHP伪静态写法附代码
Jun 20 PHP
分享一下贝贝成长进度的php代码
Sep 14 PHP
PHP连接MySQL的2种方法小结以及防止乱码
Mar 11 PHP
PHP中模拟处理HTTP PUT请求的例子
Jul 22 PHP
PHP中header函数的用法及其注意事项详解
Jun 13 PHP
PHP面向对象中new self()与 new static()的区别浅析
Aug 17 PHP
php文件包含的几种方式总结
Sep 19 PHP
基于Laravel-admin 后台的自定义页面用法详解
Sep 30 PHP
PHP实现Snowflake生成分布式唯一ID的方法示例
Aug 30 PHP
php正则去除网页中所有的html,js,css,注释的实现方法
Nov 03 #PHP
PHP 微信扫码支付源代码(推荐)
Nov 03 #PHP
php使用正则表达式去掉html中的注释方法
Nov 03 #PHP
使用正则去除php代码中的注释方法
Nov 03 #PHP
php cookie 详解使用实例
Nov 03 #PHP
PHP使用curl制作简易百度搜索
Nov 03 #PHP
php 防止表单重复提交两种实现方法
Nov 03 #PHP
You might like
PHP简单实现二维数组的矩阵转置操作示例
2017/11/24 PHP
在JavaScript中实现命名空间
2006/11/23 Javascript
javascript 混合的构造函数和原型方式,动态原型方式
2009/12/07 Javascript
js调用webservice构造SOAP进行身份验证
2016/04/27 Javascript
js中遍历Map对象的方法
2016/07/27 Javascript
10分钟掌握XML、JSON及其解析
2020/12/06 Javascript
jQuery图片加载显示loading效果
2016/11/04 Javascript
JavaScript设计模式之模板方法模式原理与用法示例
2018/08/07 Javascript
微信小程序环境下将文件上传到OSS的方法步骤
2019/05/31 Javascript
文章或博客自动生成章节目录索引(支持三级)的实现代码
2020/05/10 Javascript
viewer.js实现图片预览功能
2020/06/24 Javascript
JS实现点击掉落特效
2021/01/29 Javascript
深入理解javascript中的this
2021/02/08 Javascript
python创建线程示例
2014/05/06 Python
python生成式的send()方法(详解)
2017/05/08 Python
django进阶之cookie和session的使用示例
2018/08/17 Python
Python通用循环的构造方法实例分析
2018/12/19 Python
Python动态语言与鸭子类型详解
2019/07/01 Python
py-charm延长试用期限实例
2019/12/22 Python
Python安装whl文件过程图解
2020/02/18 Python
关于多元线性回归分析——Python&amp;SPSS
2020/02/24 Python
Django数据库操作之save与update的使用
2020/04/01 Python
python读取配置文件方式(ini、yaml、xml)
2020/04/09 Python
Python环境使用OpenCV检测人脸实现教程
2020/10/19 Python
澳大利亚潮流尖端的快时尚品牌:Cotton On
2016/09/26 全球购物
广告设计专业自荐信范文
2013/11/14 职场文书
医生爱岗敬业演讲稿
2014/08/26 职场文书
民政局办理协议离婚(范本)
2014/10/25 职场文书
2014年导购员工作总结
2014/11/18 职场文书
家长评语怎么写
2014/12/30 职场文书
省级三好学生主要事迹材料
2015/11/03 职场文书
如何使用Python提取Chrome浏览器保存的密码
2021/06/09 Python
MongoDB数据库常用的10条操作命令
2021/06/18 MongoDB
linux中nohup和后台运行进程查看及终止
2021/06/24 Python
详解Go语言Slice作为函数参数的使用
2021/07/02 Golang
JavaWeb 入门:Hello Servlet
2021/07/16 Java/Android