WxPay.Notify.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. *
  4. * 回调基础类
  5. * @author widyhu
  6. *
  7. */
  8. class WxPayNotify extends WxPayNotifyReply {
  9. /**
  10. *
  11. * 回调入口
  12. * @param bool $needSign 是否需要签名输出
  13. */
  14. final public function Handle($needSign = true) {
  15. //当返回false的时候,表示notify中调用NotifyCallBack回调失败获取签名校验失败,此时直接回复失败
  16. $result = WxpayApi::notify(array($this, 'NotifyCallBack'), $msg);
  17. if ($result == false) {
  18. $this->SetReturn_code("FAIL");
  19. $this->SetReturn_msg($msg);
  20. $this->ReplyNotify(false);
  21. return;
  22. } else {
  23. //该分支在成功回调到NotifyCallBack方法,处理完成之后流程
  24. $this->SetReturn_code("SUCCESS");
  25. $this->SetReturn_msg("OK");
  26. }
  27. $this->ReplyNotify($needSign);
  28. }
  29. /**
  30. *
  31. * 回调方法入口,子类可重写该方法
  32. * 注意:
  33. * 1、微信回调超时时间为2s,建议用户使用异步处理流程,确认成功之后立刻回复微信服务器
  34. * 2、微信服务器在调用失败或者接到回包为非确认包的时候,会发起重试,需确保你的回调是可以重入
  35. * @param array $data 回调解释出的参数
  36. * @param string $msg 如果回调处理失败,可以将错误信息输出到该方法
  37. * @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
  38. */
  39. public function NotifyProcess($data, &$msg) {
  40. //TODO 用户基础该类之后需要重写该方法,成功的时候返回true,失败返回false
  41. return true;
  42. }
  43. /**
  44. *
  45. * notify回调方法,该方法中需要赋值需要输出的参数,不可重写
  46. * @param array $data
  47. * @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
  48. */
  49. final public function NotifyCallBack($data) {
  50. $msg = "OK";
  51. $result = $this->NotifyProcess($data, $msg);
  52. if ($result == true) {
  53. $this->SetReturn_code("SUCCESS");
  54. $this->SetReturn_msg("OK");
  55. } else {
  56. $this->SetReturn_code("FAIL");
  57. $this->SetReturn_msg($msg);
  58. }
  59. return $result;
  60. }
  61. /**
  62. *
  63. * 回复通知
  64. * @param bool $needSign 是否需要签名输出
  65. */
  66. private function ReplyNotify($needSign = true) {
  67. //如果需要签名
  68. if ($needSign == true &&
  69. $this->GetReturn_code($return_code) == "SUCCESS") {
  70. $this->SetSign();
  71. }
  72. WxpayApi::replyNotify($this->ToXml());
  73. }
  74. }