NotifyService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. use Psr\Http\Message\MessageInterface;
  18. /**
  19. * 支付回调管理-服务类
  20. * @author wesmiler
  21. * @since 2020/11/11
  22. * Class NotifyService
  23. * @package App\Services
  24. */
  25. class NotifyService extends BaseService
  26. {
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author wesmiler
  31. * @since 2020/11/11
  32. * NotifyService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new GongdengOrderModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return NotifyService|null
  41. */
  42. public static function make(){
  43. if(!self::$instance){
  44. self::$instance = new NotifyService();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 供灯订单回调处理
  50. * @param $notifyData
  51. * @param $outTradeNo
  52. * @return false
  53. */
  54. public function notifyGongdeng($notifyData, $outTradeNo){
  55. $errorKey = "caches:orders:gongdeng:{$outTradeNo}";
  56. // 验证订单是否存在
  57. $orderInfo = $this->model::where(['order_sn'=> $outTradeNo])
  58. ->select(['id','source_id','buy_type','user_id','num','total','status'])
  59. ->first();
  60. // 验证参数
  61. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  62. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  63. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  64. if (empty($orderInfo) || $userId<=0) {
  65. return NotifyService::rebackMsg('订单数据不存在', 'success');
  66. }
  67. // 订单用户
  68. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  69. if(!$memberInfo){
  70. return NotifyService::rebackMsg('订单用户不存在', 'success');
  71. }
  72. // 验证订单状态是否可处理
  73. if ($orderStatus != 1) {
  74. return NotifyService::rebackMsg('订单已处理', 'success');
  75. }
  76. // 验证订单金额是否正确
  77. $payDebug = config('weixin.payDebug');
  78. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  79. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  80. $orderMoney = isset($orderInfo['total']) ? moneyFormat($orderInfo['total']) : 0.00;
  81. $orderAmount = moneyFormat($orderMoney);
  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. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  90. if(!$this->model::where(['order_sn'=> $outTradeNo])->update(['status'=> 2,'trade_no'=> $tradeNo,'pay_at'=> date('Y-m-d H:i:s'),'pay_money'=> moneyFormat($payMoney/100),'remark'=> '已支付'])){
  91. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  92. ], 3600);
  93. DB::rollBack();
  94. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  95. }
  96. // 处理支付明细
  97. $data = [
  98. 'user_id'=> $userId,
  99. 'type'=> 1,
  100. 'coin_type'=> 2,
  101. 'pay_type'=> 2,
  102. 'money'=> $payMoney,
  103. 'change_type'=> 2,
  104. 'balance'=> $memberInfo->balance? $memberInfo->balance : 0,
  105. 'create_time'=> time(),
  106. 'remark'=> '供灯订单支付',
  107. 'status'=> 1
  108. ];
  109. if(!TradeModel::insertGetId($data)){
  110. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  111. ], 3600);
  112. DB::rollBack();
  113. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  114. }
  115. DB::commit();
  116. // 开灯处理,续费不处理
  117. $buyType = isset($orderInfo['buy_type'])? $orderInfo['buy_type'] : 0;
  118. if($buyType == 1){
  119. DevicesService::make()->catchLamp($outTradeNo);
  120. }
  121. // 消息处理
  122. return NotifyService::rebackMsg('支付处理成功','success');
  123. }
  124. /**
  125. * 回去回调报文内容
  126. * @param $message
  127. * @return string
  128. */
  129. public static function getBody($message){
  130. $body = '';
  131. $bodyStream = $message->getBody();
  132. if ($bodyStream->isSeekable()) {
  133. $body = (string)$bodyStream;
  134. $bodyStream->rewind();
  135. }
  136. return $body;
  137. }
  138. /**
  139. * 返回成功
  140. */
  141. public static function rebackOk(){
  142. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  143. exit;
  144. }
  145. /**
  146. * V3支付回调应答
  147. * @param $code
  148. * @param string $msg
  149. * @return false|string
  150. */
  151. public static function rebackMsg($msg='失败', $code='error'){
  152. return ['code'=> $code, 'message'=> $msg];
  153. }
  154. }