NotifyService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace addons\YunfastPay\common\services;
  3. use addons\YunfastPay\common\utils\Des3Utils;
  4. use Exception;
  5. use Yii;
  6. use yii\helpers\Json;
  7. /**
  8. * 回调通知处理
  9. * @package addons\YunfastPay\common\services
  10. */
  11. class NotifyService extends BaseService
  12. {
  13. /**
  14. * 通知信息
  15. *
  16. * @param string $msg
  17. * @return boolean
  18. */
  19. public function check(string $msg, string $type)
  20. {
  21. $config = (new SettingService([
  22. 'mall_id' => $this->mall_id,
  23. ]))->getSetting();
  24. try {
  25. $reqStr = Des3Utils::decrypt($msg, $config['secret']);
  26. Yii::error('$reqStr' . $reqStr);
  27. $respData = Json::decode($reqStr);
  28. Yii::error('$respData' . $respData);
  29. // 添加日志
  30. $this->addFileLog($msg, $config['secret'], $respData);
  31. // 处理支付回调
  32. if ($type == 'pay') {
  33. $service = new PayService(['mall_id' => $this->mall_id]);
  34. return $service->notify($respData);
  35. }
  36. // 处理代付回调
  37. if ($type == 'agent') {
  38. $service = new AgentService(['mall_id' => $this->mall_id]);
  39. return $service->notify($respData);
  40. }
  41. if ($type == 'refund') {
  42. $service = new RefundService(['mall_id' => $this->mall_id]);
  43. return $service->notify($respData);
  44. }
  45. } catch (Exception $e) {
  46. Yii::error('银典支付回调异常: ' . $e->getMessage());
  47. return $this->error($e->getMessage());
  48. }
  49. }
  50. /**
  51. * 添加文件日志
  52. *
  53. * @param string $msg
  54. * @param string $secret
  55. * @param array $data
  56. * @return void
  57. */
  58. public function addFileLog(string $msg, string $secret, array $data)
  59. {
  60. file_put_contents('./yunfast.log', Json::encode([
  61. 'mall_id' => $this->mall_id,
  62. 'msg' => $msg,
  63. 'secret' => $secret,
  64. 'data' => $data,
  65. ]) . "\r", FILE_APPEND);
  66. }
  67. }