++notify_jsapi.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 status='0' and type='2'");
  35. Log::DEBUG("select * from cmf_user_charge where orderno='{$out_trade_no}' and status='0' and type='2'");
  36. $row = $result->fetch_assoc();
  37. if($row){
  38. $coin=$row['coin']+$row['coin_give'];
  39. $link->query("update cmf_user set coin=coin+{$coin} where id='$row[touid]'");
  40. $link->query("update cmf_user_charge set status='1',trade_no='{$transaction_id}' where id={$row['id']}");
  41. Log::DEBUG("支付成功");
  42. }else{
  43. Log::DEBUG($out_trade_no.' 订单信息不存在');
  44. }
  45. }else{
  46. Log::DEBUG("数据库链接失败");
  47. }
  48. }else{
  49. $link = mysql_connect("127.0.0.1","","");
  50. if($link){
  51. mysql_select_db("",$link);
  52. mysql_query("set names utf8");
  53. $result = mysql_query("select * from cmf_user_charge where orderno='{$out_trade_no}' and status='0' and type='2'");
  54. Log::DEBUG("select * from cmf_user_charge where orderno='{$out_trade_no}' and status='0' and type='2'");
  55. $row = mysql_fetch_assoc($result);
  56. $str = json_encode($row);
  57. if($row){
  58. $coin=$row['coin']+$row['coin_give'];
  59. mysql_query("update cmf_user set coin=coin+{$coin} where id='$row[touid]'");
  60. mysql_query("update cmf_user_charge set status='1',trade_no='{$transaction_id}' where id={$row['id']}");
  61. Log::DEBUG("支付成功");
  62. }else{
  63. Log::DEBUG($out_trade_no.' 订单信息不存在');
  64. }
  65. }else{
  66. Log::DEBUG("数据库链接失败");
  67. }
  68. }
  69. return true;
  70. }
  71. return false;
  72. }
  73. //重写回调处理函数
  74. public function NotifyProcess($data, &$msg)
  75. {
  76. Log::DEBUG("call back:" . json_encode($data));
  77. $notfiyOutput = array();
  78. if(!array_key_exists("transaction_id", $data)){
  79. $msg = "输入参数不正确";
  80. return false;
  81. }
  82. //查询订单,判断订单真实性
  83. if(!$this->Queryorder($data["transaction_id"])){
  84. $msg = "订单查询失败";
  85. return false;
  86. }
  87. return true;
  88. }
  89. }
  90. Log::DEBUG("begin notify");
  91. $notify = new PayNotifyCallBack();
  92. $notify->Handle(false);