模式一比较复杂,需要公众号配置回调地址。
模式二比较简单,只需要在代码中配置回调地址就可以了。
我这次使用的是模式二。
需要配置参数,
|
1
2
3
4
|
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=newwxpayunifiedorder();
- $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=newnativepay();
- $result=$notify->getpayurl($input);
- $code_url=$result["code_url"];
- <imgalt="扫码支付"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=newclogfilehandler("../logs/".date('y-m-d').'.log');
- $log=log::init($loghandler,15);
- classpaynotifycallbackextendswxpaynotify
- //查询订单
- publicfunctionqueryorder($transaction_id)
- {
- $input=newwxpayorderquery();
- $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")
- {
- returntrue;
- }
- returnfalse;
- }
- //重写回调处理函数
- publicfunctionnotifyprocess($data,&$msg)
- {
- log::debug("callback:".json_encode($data));
- $notfiyoutput=array();
- if(!array_key_exists("transaction_id",$data)){
- $msg="输入参数不正确";
- returnfalse;
- }
- //查询订单,判断订单真实性
- if(!$this->queryorder($data["transaction_id"])){
- $msg="订单查询失败";
- returnfalse;
- }
- returntrue;
- }
- log::debug("beginnotify");
- $notify=newpaynotifycallback();
- $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"];
可以获取微信端传入的参数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
{
"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"
}
|
查询是否已支付,支付完成的话,进行订单数据处理。
这里的一切都是异步的,二维码页面啥都看不到。
只能通过异步获取订单状态来判断是否操作成功。
|
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
|
// 检测是否支付成功
$(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"; //页面跳转
}
}
});
}
}
|
三秒执行一次,如果成功,进行跳转处理。
赠送函数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
* 作用: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 微信扫码支付接入总结(总结篇),数据库显示空白的完美解决方案(图文教程),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://www.cnblogs.com/jiqing9006/p/5872729.html
相关文章
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 79
-
2025-05-29 74
-
租用美国服务器有哪些要注意的事项(租用美国服务器有哪些要注意的事项)
2025-05-25 24 -
浅谈ASP.NET Core中间件实现分布式 Session
2025-05-29 107 -
2025-05-25 60






