WxNotify.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\client\controller;
  3. require_once __DIR__ . "/../../../extend/wx/pay/WxPay.Config.php";
  4. require_once __DIR__ . "/../../../extend/wx/pay/lib/WxPay.Data.php";
  5. require_once __DIR__ . "/../../../extend/wx/pay/lib/WxPay.Api.php";
  6. use app\model\ActivityOrder;
  7. use WxPayApi;
  8. use WxPayConfig;
  9. use WxPayNotifyReply;
  10. use WxPayUnifiedOrder;
  11. class WxNotify
  12. {
  13. private $reply;
  14. private $config;
  15. public function __construct()
  16. {
  17. $this->reply = new WxPayNotifyReply();
  18. }
  19. public function catch($action)
  20. {
  21. // 根据action 获取用户配置的商户号
  22. $config = [];
  23. $this->config = new WxPayConfig([
  24. 'app_id' => $config['mini_id'],
  25. 'mch_id' => $config['mch_id'],
  26. 'key' => $config['mch_key']
  27. ]);
  28. $this->run();
  29. }
  30. private function run()
  31. {
  32. // 微信支付请求接口配置生成
  33. $msg = '';
  34. $result = WxPayApi::notify($this->config, array($this, 'dealLogic'), $msg);
  35. if ($result == false) {
  36. $this->reply->SetReturn_code("FAIL");
  37. $this->reply->SetReturn_msg($msg);
  38. $this->ReplyNotify(false);
  39. return;
  40. } else {
  41. //该分支在成功回调到NotifyCallBack方法,处理完成之后流程
  42. $this->reply->SetReturn_code("SUCCESS");
  43. $this->reply->SetReturn_msg("OK");
  44. }
  45. $this->ReplyNotify(true);
  46. }
  47. /**
  48. * 回掉处理逻辑
  49. * @return boolean
  50. */
  51. public function dealLogic($WxPayNotifyResults)
  52. {
  53. $data = $WxPayNotifyResults->GetValues();
  54. $method = $data['attach'];
  55. if (!method_exists($this, $method)) {
  56. return false;
  57. }
  58. return $this->$method($data);
  59. }
  60. /**
  61. *
  62. * 回复通知
  63. * @param bool $needSign 是否需要签名输出
  64. */
  65. final private function ReplyNotify($needSign)
  66. {
  67. //如果需要签名
  68. if (
  69. $needSign == true &&
  70. $this->reply->GetReturn_code() == "SUCCESS"
  71. ) {
  72. $this->reply->SetSign($this->config);
  73. }
  74. $xml = $this->reply->ToXml();
  75. WxPayApi::replyNotify($xml);
  76. }
  77. /**
  78. * 查询订单是否支付成功
  79. */
  80. private function orderQuery($orderNo, $appidType = 'mini_appid')
  81. {
  82. $input = new WxPayUnifiedOrder();
  83. $input->SetOut_trade_no($orderNo);
  84. $response = \WxPayApi::orderQuery($this->config, $input, 12);
  85. if ($response['return_code'] != 'SUCCESS' || $response['result_code'] != 'SUCCESS' || $response['trade_state'] != 'SUCCESS') {
  86. trace($response, 'error');
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * 活动订单支付
  93. */
  94. public function activityOrder($data)
  95. {
  96. if (!$this->orderQuery($data['out_trade_no'])) return false;
  97. // 订单查找
  98. $order = ActivityOrder::where('order_no', $data['out_trade_no'])->find();
  99. // 更改订单状态
  100. $order->state = 1;
  101. $order->save();
  102. // 更改线索或客户的状态
  103. // 发送客户交定通知
  104. return true;
  105. }
  106. }