NotifyService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. NotifyService::rebackOk();
  64. return false;
  65. }
  66. // 订单用户
  67. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','coupon','status'])->first();
  68. if(!$memberInfo){
  69. NotifyService::rebackOk();
  70. return false;
  71. }
  72. // 验证订单状态是否可处理
  73. if ($orderStatus != 1) {
  74. NotifyService::rebackOk();
  75. return false;
  76. }
  77. // 验证订单金额是否正确
  78. $payDebug = config('weixin.payDebug');
  79. $payMoney = isset($postData['total_fee']) ? moneyFormat($postData['total_fee']) : 0;
  80. $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
  81. $credit = isset($orderInfo['credit']) ? moneyFormat($orderInfo['credit']) : 0.00;
  82. $orderAmount = moneyFormat($orderMoney + $credit);
  83. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  84. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  85. ], 3600);
  86. return false;
  87. }
  88. // 更新订单数据
  89. DB::beginTransaction();
  90. if(!$this->model::where(['order_sn'=> $outTradeNo])->update(['status'=> 2,'pay_money'=> $payMoney,'remark'=> '已支付'])){
  91. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  92. ], 3600);
  93. DB::rollBack();
  94. return false;
  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,
  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 false;
  114. }
  115. // 开灯处理
  116. // 消息处理
  117. }
  118. /**
  119. * 返回成功
  120. */
  121. public static function rebackOk(){
  122. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  123. exit;
  124. }
  125. }