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

2025-05-29 0 26

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=newwxpayunifiedorder();
  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=newnativepay();
  16. $result=$notify->getpayurl($input);
  17. $code_url=$result["code_url"];
  18. <imgalt="扫码支付"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=newclogfilehandler("../logs/".date('y-m-d').'.log');
  9. $log=log::init($loghandler,15);
  10. classpaynotifycallbackextendswxpaynotify
  11. //查询订单
  12. publicfunctionqueryorder($transaction_id)
  13. {
  14. $input=newwxpayorderquery();
  15. $input->settransaction_id($transaction_id);
  16. $result=wxpayapi::orderquery($input);
  17. log::debug("query:".json_encode($result));
  18. if(array_key_exists("return_code",$result)
  19. &&array_key_exists("result_code",$result)
  20. &&$result["return_code"]=="success"
  21. &&$result["result_code"]=="success")
  22. {
  23. returntrue;
  24. }
  25. returnfalse;
  26. }
  27. //重写回调处理函数
  28. publicfunctionnotifyprocess($data,&$msg)
  29. {
  30. log::debug("callback:".json_encode($data));
  31. $notfiyoutput=array();
  32. if(!array_key_exists("transaction_id",$data)){
  33. $msg="输入参数不正确";
  34. returnfalse;
  35. }
  36. //查询订单,判断订单真实性
  37. if(!$this->queryorder($data["transaction_id"])){
  38. $msg="订单查询失败";
  39. returnfalse;
  40. }
  41. returntrue;
  42. }
  43. log::debug("beginnotify");
  44. $notify=newpaynotifycallback();
  45. $ilog_db=pc_base::load_model('ilog_model');
  46. $order_db=pc_base::load_model('order_model');
  47. $postxml=$globals["http_raw_post_data"];
  48. $postarr=xmltoarray($postxml);
  49. //查询是否支付成功
  50. $r=$notify->queryorder($postarr['transaction_id']);
  51. if($r){
  52. //获取订单信息
  53. $order_info=$order_db->get_one(array('orderno'=>$postarr['out_trade_no']));
  54. if($order_info['pay_status']!='1'){
  55. $data['pay_status']='1';
  56. $data['pay_type']='weixinpay';
  57. $data['pay_code']=$postarr['transaction_id'];
  58. $data['paytime']=time();
  59. $data['order_status']=3;//已支付
  60. $order_db->update($data,array('orderno'=>$postarr['out_trade_no']));
  61. }
  62. ?>

通过

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP 接入微信扫码支付总结(总结篇) https://www.kuaiidc.com/96173.html

相关文章

发表评论
暂无评论