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

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

服务器之家 - 编程语言 - PHP教程 - PHP 接入微信扫码支付总结(总结篇)

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

2021-03-18 16:31牛逼的霍啸林 PHP教程

微信扫码支付分为两种模式,本文以模式二给大家详细介绍PHP 微信扫码支付接入总结,感兴趣的朋友一起看看吧

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

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

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

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

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

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

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

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

我这次使用的是模式二。

需要配置参数,

?
1
2
3
4
const appid = 'xxx';
const mchid = 'xxx';
const key = 'xxx';
const appsecret = 'xxx';

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

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

  1. require_once "./phpcms/plugin/weixinpay/lib/wxpay.api.php"
  2. require_once "./phpcms/plugin/weixinpay/example/wxpay.nativepay.php"
  3. require_once './phpcms/plugin/weixinpay/example/log.php'
  4. $input = new wxpayunifiedorder(); 
  5. $input->setbody('预订'.$product_info['name'].'订单'); 
  6. $input->setattach('预订'.$product_info['name'].'订单'); 
  7. $input->setout_trade_no($order_info['orderno']); 
  8. $input->settotal_fee($order_info['payprice'] * 100); 
  9. $input->settime_start(date("ymdhis")); 
  10. $input->settime_expire(date("ymdhis", time() + 600)); 
  11. $input->setgoods_tag(""); 
  12. $input->setnotify_url("http://www.ayuanduanzu.com/wxpay/notify.php"); // 地址是外网能访问的,且不能包含参数 
  13. $input->settrade_type("native"); 
  14. $input->setproduct_id($product_info['id']); 
  15. $notify = new nativepay(); 
  16. $result = $notify->getpayurl($input); 
  17. $code_url = $result["code_url"]; 
  18. <img alt="扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data={urlencode($code_url)}" style="width:150px;height:150px;"/> 

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

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

  1. include_once "../phpcms/base.php"
  2. // 处理回调数据 
  3. error_reporting(e_error); 
  4. require_once "../phpcms/plugin/weixinpay/lib/wxpay.api.php"
  5. require_once '../phpcms/plugin/weixinpay/lib/wxpay.notify.php'
  6. require_once '../phpcms/plugin/weixinpay/example/log.php'
  7. //初始化日志 
  8. $loghandler= new clogfilehandler("../logs/".date('y-m-d').'.log'); 
  9. $log = log::init($loghandler, 15); 
  10. class paynotifycallback extends wxpaynotify 
  11. //查询订单 
  12. public function queryorder($transaction_id) 
  13. $input = new wxpayorderquery(); 
  14. $input->settransaction_id($transaction_id); 
  15. $result = wxpayapi::orderquery($input); 
  16. log::debug("query:" . json_encode($result)); 
  17. if(array_key_exists("return_code", $result) 
  18. && array_key_exists("result_code", $result) 
  19. && $result["return_code"] == "success" 
  20. && $result["result_code"] == "success"
  21. return true
  22. return false
  23. //重写回调处理函数 
  24. public function notifyprocess($data, &$msg) 
  25. log::debug("call back:" . json_encode($data)); 
  26. $notfiyoutput = array(); 
  27. if(!array_key_exists("transaction_id", $data)){ 
  28. $msg = "输入参数不正确"
  29. return false
  30. //查询订单,判断订单真实性 
  31. if(!$this->queryorder($data["transaction_id"])){ 
  32. $msg = "订单查询失败"
  33. return false
  34. return true
  35. log::debug("begin notify"); 
  36. $notify = new paynotifycallback(); 
  37. $ilog_db = pc_base::load_model('ilog_model'); 
  38. $order_db = pc_base::load_model('order_model'); 
  39. $postxml = $globals["http_raw_post_data"]; 
  40. $postarr = xmltoarray($postxml); 
  41. // 查询是否支付成功 
  42. $r = $notify->queryorder($postarr['transaction_id']); 
  43. if ($r) { 
  44. // 获取订单信息 
  45. $order_info = $order_db->get_one(array('orderno'=>$postarr['out_trade_no'])); 
  46. if ($order_info['pay_status'] != '1') { 
  47. $data['pay_status'] = '1'
  48. $data['pay_type'] = 'weixinpay'
  49. $data['pay_code'] = $postarr['transaction_id']; 
  50. $data['paytime'] = time(); 
  51. $data['order_status']= 3; // 已支付 
  52. $order_db->update($data,array('orderno'=>$postarr['out_trade_no'])); 
  53. ?> 

通过

  1. $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 微信扫码支付接入总结(总结篇),数据库显示空白的完美解决方案(图文教程),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/jiqing9006/p/5872729.html

延伸 · 阅读

精彩推荐