NotifyService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\GongdengOrderModel;
  13. use App\Models\MemberModel;
  14. use App\Models\SignsModel;
  15. use App\Models\TradeModel;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 支付回调管理-服务类
  19. * @author wesmiler
  20. * @since 2020/11/11
  21. * Class NotifyService
  22. * @package App\Services
  23. */
  24. class NotifyService extends BaseService
  25. {
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author wesmiler
  30. * @since 2020/11/11
  31. * NotifyService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new GongdengOrderModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return NotifyService|null
  40. */
  41. public static function make(){
  42. if(!self::$instance){
  43. self::$instance = new NotifyService();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 供灯订单回调处理
  49. * @param $notifyData
  50. * @param $outTradeNo
  51. * @return false
  52. */
  53. public function notifyGongdeng($notifyData, $outTradeNo){
  54. $errorKey = "caches:orders:gongdeng:{$outTradeNo}";
  55. // 验证订单是否存在
  56. $orderInfo = $this->model::where(['order_sn'=> $outTradeNo])
  57. ->select(['id','source_id','buy_type','user_id','num','total','status'])
  58. ->first();
  59. // 验证参数
  60. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  61. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  62. if (empty($orderInfo) || $userId<=0) {
  63. return NotifyService::rebackMsg('订单数据不存在', 'success');
  64. }
  65. // 订单用户
  66. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','coupon','status'])->first();
  67. if(!$memberInfo){
  68. NotifyService::rebackOk();
  69. return NotifyService::rebackMsg('订单用户不存在', 'success');
  70. }
  71. // 验证订单状态是否可处理
  72. if ($orderStatus != 1) {
  73. NotifyService::rebackOk();
  74. return NotifyService::rebackMsg('订单已处理', 'success');
  75. }
  76. // 验证订单金额是否正确
  77. $payDebug = config('weixin.payDebug');
  78. $payMoney = isset($postData['total_fee']) ? moneyFormat($postData['total_fee']) : 0;
  79. $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
  80. $credit = isset($orderInfo['credit']) ? moneyFormat($orderInfo['credit']) : 0.00;
  81. $orderAmount = moneyFormat($orderMoney + $credit);
  82. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  83. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  84. ], 3600);
  85. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  86. }
  87. // 更新订单数据
  88. DB::beginTransaction();
  89. if(!$this->model::where(['order_sn'=> $outTradeNo])->update(['status'=> 2,'pay_money'=> $payMoney,'remark'=> '已支付'])){
  90. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  91. ], 3600);
  92. DB::rollBack();
  93. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  94. }
  95. // 处理支付明细
  96. $data = [
  97. 'user_id'=> $userId,
  98. 'type'=> 1,
  99. 'coin_type'=> 2,
  100. 'pay_type'=> 2,
  101. 'money'=> $payMoney,
  102. 'change_type'=> 2,
  103. 'balance'=> $memberInfo->balance,
  104. 'create_time'=> time(),
  105. 'remark'=> '供灯订单支付',
  106. 'status'=> 1
  107. ];
  108. if(!TradeModel::insertGetId($data)){
  109. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  110. ], 3600);
  111. DB::rollBack();
  112. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  113. }
  114. DB::commit();
  115. // 开灯处理,续费不处理
  116. $buyType = isset($orderInfo['buy_type'])? $orderInfo['buy_type'] : 0;
  117. if($buyType == 1){
  118. DevicesService::make()->catchLamp($outTradeNo);
  119. }
  120. // 消息处理
  121. return NotifyService::rebackMsg('支付处理成功','success');
  122. }
  123. /**
  124. * 返回成功
  125. */
  126. public static function rebackOk(){
  127. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  128. exit;
  129. }
  130. /**
  131. * V3支付回调应答
  132. * @param $code
  133. * @param string $msg
  134. * @return false|string
  135. */
  136. public static function rebackMsg($msg='失败', $code='error'){
  137. return json_encode(['code'=> $code, 'message'=> $msg]);
  138. }
  139. }