WxPay.Notify.php 2.3 KB

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