WxPay.Notify.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *
  4. * 回调基础类
  5. *
  6. * @author widyhu
  7. *
  8. */
  9. class WxPayNotify extends WxPayNotifyReply {
  10. /**
  11. *
  12. * 回调入口
  13. *
  14. * @param bool $needSign 是否需要签名输出
  15. * @param array $config
  16. */
  17. final public function Handle($needSign = true, array $config = []) {
  18. $msg = "OK";
  19. //当返回false的时候,表示notify中调用NotifyCallBack回调失败获取签名校验失败,此时直接回复失败
  20. $result = WxpayApi::notify(array($this, 'NotifyCallBack'), $msg, $config);
  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. *
  40. * @param array $data 回调解释出的参数
  41. * @param string $msg 如果回调处理失败,可以将错误信息输出到该方法
  42. *
  43. * @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
  44. */
  45. public function NotifyProcess($data, &$msg) {
  46. //TODO 用户基础该类之后需要重写该方法,成功的时候返回true,失败返回false
  47. return true;
  48. }
  49. /**
  50. *
  51. * notify回调方法,该方法中需要赋值需要输出的参数,不可重写
  52. *
  53. * @param array $data
  54. *
  55. * @return bool|true 回调出来完成不需要继续回调
  56. */
  57. final public function NotifyCallBack($data) {
  58. $msg = "OK";
  59. $result = $this->NotifyProcess($data, $msg);
  60. if ($result == true) {
  61. $this->SetReturn_code("SUCCESS");
  62. $this->SetReturn_msg("OK");
  63. } else {
  64. $this->SetReturn_code("FAIL");
  65. $this->SetReturn_msg($msg);
  66. }
  67. return $result;
  68. }
  69. /**
  70. *
  71. * 回复通知
  72. *
  73. * @param bool $needSign 是否需要签名输出
  74. */
  75. final private function ReplyNotify($needSign = true) {
  76. //如果需要签名
  77. if ($needSign == true && $this->GetReturn_code() == "SUCCESS") {
  78. $this->SetSign();
  79. }
  80. WxpayApi::replyNotify($this->ToXml());
  81. }
  82. }