NotifyListener.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\listener\pay;
  12. use app\services\pay\PayNotifyServices;
  13. use app\services\wechat\WechatMessageServices;
  14. use crmeb\utils\Hook;
  15. /**
  16. * 支付异步回调
  17. * Class NotifyListener
  18. * @package app\listener\pay
  19. */
  20. class NotifyListener
  21. {
  22. /**
  23. * @param $event
  24. * @return bool
  25. * @throws \Psr\SimpleCache\InvalidArgumentException
  26. */
  27. public function handle($event)
  28. {
  29. [$notify, $payType] = $event;
  30. if (isset($notify['attach']) && $notify['attach']) {
  31. if (($count = strpos($notify['out_trade_no'], '_')) !== false) {
  32. $notify['out_trade_no'] = substr($notify['out_trade_no'], $count + 1);
  33. }
  34. return (new Hook(PayNotifyServices::class, 'wechat'))->listen($notify['attach'], $notify['out_trade_no'], $notify['transaction_id'], $payType);
  35. }
  36. if ($notify['attach'] === 'wechat' && isset($notify['out_trade_no'])) {
  37. /** @var WechatMessageServices $wechatMessageService */
  38. $wechatMessageService = app()->make(WechatMessageServices::class);
  39. $wechatMessageService->setOnceMessage($notify, $notify['openid'], 'payment_success', $notify['out_trade_no']);
  40. }
  41. return false;
  42. }
  43. }