notify.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // 微信订单通知
  3. ini_set('date.timezone', 'Asia/Shanghai');
  4. error_reporting(E_ERROR);
  5. require_once __DIR__ . '/../../../../main.php'; # 导入game utils
  6. include_once __DIR__ . '/../../Mo/payResp.php'; # 导入通用返回值结构
  7. include_once __DIR__ . '/../../Mo/payRequest.php'; # 导入通用返回值结构
  8. require_once __DIR__ . '/../../Mo/OrderNotice.php'; # 导入通知数据接收模型
  9. require_once __DIR__ . '/wx.php'; # 导入微信支付lib
  10. use loyalsoft\CLog;
  11. use loyalsoft\HttpUtil;
  12. // 添加 wx using
  13. use WeChatPay\Builder;
  14. use WeChatPay\Crypto\Rsa;
  15. use WeChatPay\Util\PemUtil;
  16. class PayNotifyCallBack extends WxPayNotify {
  17. //查询订单
  18. public function Queryorder($transaction_id) {
  19. $input = new WxPayOrderQuery();
  20. $input->SetTransaction_id($transaction_id);
  21. $result = WxPayApi::orderQuery($input);
  22. CLog::pay("[notify.wx]query:" . json_encode($result));
  23. if (array_key_exists("return_code", $result) && array_key_exists("result_code", $result)#
  24. && $result["return_code"] == "SUCCESS" #
  25. && $result["result_code"] == "SUCCESS") {
  26. return true;
  27. }
  28. return false;
  29. }
  30. //重写回调处理函数
  31. public function NotifyProcess($data, &$msg) {
  32. CLog::pay("[notify.wx]call back:" . json_encode($data));
  33. // $notfiyOutput = array();
  34. if (!array_key_exists("transaction_id", $data)) {
  35. $msg = "输入参数不正确";
  36. CLog::pay("[notify.wx] $msg"); # 日志
  37. return false;
  38. }
  39. //查询订单,判断订单真实性
  40. if (!$this->Queryorder($data["transaction_id"])) {
  41. $msg = "订单查询失败";
  42. CLog::pay("[notify.wx] $msg"); # 日志
  43. return false;
  44. }
  45. $order = loyalsoft\pay\OrderNotice::Parse_weixinpayOrder($data); # 提取参数
  46. CLog::pay($order);
  47. if ($order == null) { #
  48. $msg = "提取通知参数失败";
  49. CLog::pay("[notify.wx] $msg"); # 日志
  50. return false;
  51. }
  52. if (!$order->Check()) { # 订单校验,
  53. $msg = "订单校验失败";
  54. CLog::pay("[notify.wx] $msg"); # 日志
  55. return false;
  56. }
  57. if ($order->status == 2) { # status为2(failed)的情况
  58. $order->UpdateOrderStatus(); # [数据库操作]直接更新订单状态,->支付失败
  59. $msg = "订单状态异常";
  60. CLog::pay("[notify.wx] 异常的订单状态, 此处微信验证已通过,为啥自己的验证不通过!!!" . $order->cpOrderId);
  61. return false;
  62. }
  63. if ($order->status == 1) { # 订单状态是成功 1代表支付成功
  64. $order->UpdateOrderStatus(); # [数据库操作]更新订单状态,->已付款
  65. loyalsoft\TapDBUtil::PushPayEvent($order, 'alipay'); # 向tapdb推送充值记录
  66. CLog::pay("[notify.wx] [发货] " . $order->cpOrderId . " >>> " . $order->amount);
  67. $msg = "success";
  68. return true;
  69. }
  70. $msg = "未知的支付状态";
  71. CLog::pay("[notify.wx][处理结果]: FAILURE ($msg)"); # 日志
  72. return false;
  73. }
  74. }
  75. HttpUtil::PostOnly();
  76. CLog::pay("[notify.wx] [收到通知]: " . HttpUtil::getQueryString()); # 日志
  77. $notify = new PayNotifyCallBack();
  78. $notify->Handle(false); # 处理