++notify_jsapi_wx.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. ini_set('date.timezone','Asia/Shanghai');
  3. error_reporting(E_ERROR);
  4. require_once "../lib/WxPay.Api.php";
  5. require_once '../lib/WxPay.Notify.php';
  6. require_once '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. //查询订单
  13. public function Queryorder($transaction_id)
  14. {
  15. $input = new WxPayOrderQuery();
  16. $input->SetTransaction_id($transaction_id);
  17. $result = WxPayApi::orderQuery($input);
  18. Log::DEBUG("query:" . json_encode($result));
  19. if(array_key_exists("return_code", $result)
  20. && array_key_exists("result_code", $result)
  21. && $result["return_code"] == "SUCCESS"
  22. && $result["result_code"] == "SUCCESS")
  23. {
  24. Log::DEBUG("已进入");
  25. $attach=$result['attach'];
  26. $out_trade_no=$result['out_trade_no'];
  27. $fee=$result['total_fee'];
  28. $transaction_id=$result['transaction_id'];
  29. $total_fee=$fee*0.01;
  30. if(function_exists('mysqli_close')){
  31. $link = new mysqli("127.0.0.1","","","");
  32. if($link){
  33. $link->query("set names utf8");
  34. $result = $link->query("select * from cmf_user_charge where orderno='{$out_trade_no}' and money='{$total_fee}' and status='0' and type='2'");
  35. Log::DEBUG("select * from cmf_user_charge where orderno='{$out_trade_no}' and money='{$total_fee}' and status='0' and type='2'");
  36. $row = $result->fetch_assoc();
  37. if($row){
  38. $link->query("update cmf_user set coin=coin+{$row['coin']} where id='$row[touid]'");
  39. $link->query("update cmf_user_charge set status='1',trade_no='{$transaction_id}' where id={$row['id']}");
  40. Log::DEBUG("支付成功");
  41. }else{
  42. Log::DEBUG($out_trade_no.' 订单信息不存在');
  43. }
  44. }else{
  45. Log::DEBUG("数据库链接失败");
  46. }
  47. }else{
  48. $link = mysql_connect("127.0.0.1","","");
  49. if($link){
  50. mysql_select_db("",$link);
  51. mysql_query("set names utf8");
  52. $result = mysql_query("select * from cmf_user_charge where orderno='{$out_trade_no}' and money='{$total_fee}' and status='0' and type='2'");
  53. Log::DEBUG("select * from cmf_user_charge where orderno='{$out_trade_no}' and money='{$total_fee}' and status='0' and type='2'");
  54. $row = mysql_fetch_assoc($result);
  55. $str = json_encode($row);
  56. if($row){
  57. mysql_query("update cmf_user set coin=coin+{$row['coin']} where id='$row[touid]'");
  58. mysql_query("update cmf_user_charge set status='1',trade_no='{$transaction_id}' where id={$row['id']}");
  59. Log::DEBUG("支付成功");
  60. }else{
  61. Log::DEBUG($out_trade_no.' 订单信息不存在');
  62. }
  63. }else{
  64. Log::DEBUG("数据库链接失败");
  65. }
  66. }
  67. return true;
  68. }
  69. return false;
  70. }
  71. //重写回调处理函数
  72. public function NotifyProcess($data, &$msg)
  73. {
  74. Log::DEBUG("call back:" . json_encode($data));
  75. $notfiyOutput = array();
  76. if(!array_key_exists("transaction_id", $data)){
  77. $msg = "输入参数不正确";
  78. return false;
  79. }
  80. //查询订单,判断订单真实性
  81. if(!$this->Queryorder($data["transaction_id"])){
  82. $msg = "订单查询失败";
  83. return false;
  84. }
  85. return true;
  86. }
  87. }
  88. Log::DEBUG("begin notify");
  89. $notify = new PayNotifyCallBack();
  90. $notify->Handle(false);