HighlightController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/5/26
  8. * Time: 17:13
  9. */
  10. namespace app\payment\controllers;
  11. use app\common\events\withdraw\WithdrawSuccessEvent;
  12. use app\common\exceptions\ShopException;
  13. use app\payment\PaymentController;
  14. use Yunshop\HighLight\models\HighLightWithdrawModel;
  15. use Yunshop\HighLight\services\ApiService;
  16. use app\common\services\Pay;
  17. use Yunshop\HighLight\services\WithdrawService;
  18. class HighlightController extends PaymentController
  19. {
  20. protected $parameters;
  21. /**
  22. * @var HighLightWithdrawModel
  23. */
  24. protected $withdraw;
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->setParameter();
  29. //访问记录
  30. Pay::payAccessLog();
  31. //保存响应数据
  32. Pay::payResponseDataLog('', '高灯提现回调', json_encode($this->parameters));
  33. if (!app('plugins')->isEnabled('high-light')) {
  34. \Log::debug('高灯提现回调通知:高灯插件未开启',$this->parameters);
  35. exit('success');
  36. }
  37. }
  38. /**
  39. * @return ApiService
  40. */
  41. private function api()
  42. {
  43. return ApiService::current($this->parameters['appkey']);
  44. }
  45. private function setParameter()
  46. {
  47. $this->parameters = request()->input();
  48. }
  49. /**
  50. * @param $trade_number
  51. */
  52. private function setWithdraw($trade_number)
  53. {
  54. $this->withdraw = HighLightWithdrawModel::where('order_sn',$trade_number)->first();
  55. if (!$this->withdraw) {
  56. \Log::debug('高灯提现结算单回调通知:提现订单信息未找到',$trade_number);
  57. exit('success');
  58. }
  59. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->withdraw->uniacid;
  60. }
  61. //异步创建高灯结算单通知
  62. public function notifyUrl()
  63. {
  64. try {
  65. if ($this->parameters['code'] <> 0) {
  66. throw new \Exception($this->parameters['msg']);
  67. }
  68. $this->parameters['data'] = $this->api()->decrypt($this->parameters['data']);
  69. if (!$this->parameters['data']) {
  70. throw new \Exception('解密失败');
  71. }
  72. $this->parameters['data'] = json_decode($this->parameters['data'],true);
  73. foreach ($this->parameters['data'] as $item) {
  74. $this->setWithdraw($item['order_random_code']);
  75. $this->verifyStatus($item);
  76. }
  77. } catch (\Exception $e) {
  78. \Log::debug('高灯创建结算单回调失败:'.$e->getMessage(),$this->parameters);
  79. }
  80. exit('success'); //确保一次回调就返回,流转状态交由定时任务走
  81. }
  82. public function verifyStatus($data)
  83. {
  84. switch ($data['status']) {
  85. case 100:
  86. case 200:
  87. if ($data['status'] == 100 && !$data['is_verification']) {
  88. WithdrawService::setWithdraw($this->withdraw,-1,'结算单创建失败:'.($data['check_error_info'] ? : $data['fail_reason']),null,null,$data['status']);
  89. } elseif ($data['status'] == 100 && $data['is_verification']) {
  90. WithdrawService::setWithdraw($this->withdraw,1,'结算单校验中',null,null,$data['status']);
  91. } elseif ($data['status'] == 200) {
  92. WithdrawService::setWithdraw($this->withdraw,-1,'结算单创建失败已删除:'.$data['fail_reason'],null,null,$data['status']);
  93. }
  94. break;
  95. case 300:
  96. WithdrawService::setWithdraw($this->withdraw,1,'商户余额不足,充值后状态会自动流转!如需停止支付,需在商户结算后台【财务管理】找到对应结算单-【删除】',null,null,$data['status']);
  97. break;
  98. case 600:
  99. if ($data['hangup_flag']) {
  100. $failText = '该用户在多家商户累计本月结算超14900,次月可自动支付,也可在【结算后台-财务人员-挂起订单】中操作退款';
  101. } else {
  102. $failText = '高灯侧挂单中,可在【结算后台-财务人员-挂起订单】中操作退款';
  103. }
  104. WithdrawService::setWithdraw($this->withdraw,1,$failText,null,null,$data['status']);
  105. break;
  106. case 700:
  107. case 800:
  108. case 850:
  109. case 900:
  110. case 860:
  111. case 1006:
  112. case 910:
  113. case 920:
  114. case 930:
  115. WithdrawService::setWithdraw($this->withdraw,1,'高灯侧正在结算处理中',null,null,$data['status']);
  116. break;
  117. //----以下状态需进行退款处理----
  118. case 610:
  119. WithdrawService::setWithdraw($this->withdraw,1,'待用户确认结算单',null,null,$data['status']);
  120. break;
  121. case 1004:
  122. $failText = $data['status'] == 610?'打款失败:创建结算单时用户未签约或未认证':'打款失败';
  123. try {
  124. $this->api()->refundBalance(['order_random_code' => [$this->withdraw->order_sn]]);
  125. $refundStatus = 1;
  126. $refundFailText = null;
  127. } catch (\Exception $e) {
  128. $refundStatus = -1;
  129. $refundFailText = '退款失败:'.$e->getMessage().',请到高灯后台进行退款!';
  130. }
  131. WithdrawService::setWithdraw($this->withdraw,-1,$failText,$refundStatus,$refundFailText,$data['status']);
  132. break;
  133. //------------------------------
  134. case 1000://打款成功
  135. WithdrawService::setWithdraw($this->withdraw,2,'',null,null,$data['status']);
  136. WithdrawService::withdrawSuccess($this->withdraw->withdraw_sn);
  137. break;
  138. case 750:
  139. case 5000:
  140. //退款完成
  141. WithdrawService::setWithdraw($this->withdraw,-1,'',2,'',$data['status']);
  142. break;
  143. default:
  144. WithdrawService::setWithdraw($this->withdraw,-1,'未知状态码',null,null,$data['status']);
  145. }
  146. }
  147. //异步高灯退款通知
  148. public function refundNotifyUrl()
  149. {
  150. try {
  151. if ($this->parameters['code'] <> 0) {
  152. throw new \Exception($this->parameters['msg']);
  153. }
  154. $this->parameters['data'] = $this->api()->decrypt($this->parameters['data']);
  155. if (!$this->parameters['data']) {
  156. throw new \Exception('解密失败');
  157. }
  158. $this->parameters['data'] = json_decode($this->parameters['data'],true);
  159. $this->setWithdraw($this->parameters['data']['order_random_code']);
  160. WithdrawService::setWithdraw($this->withdraw,-1,null,2,'');
  161. } catch (\Exception $e) {
  162. \Log::debug('高灯结算单退款回调失败:'.$e->getMessage(),$this->parameters);
  163. }
  164. exit('success'); //确保一次回调就返回,流转状态交由定时任务走
  165. }
  166. }