WxPayNotify.php 2.9 KB

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