NotifyService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\RechargeModel;
  15. use App\Models\SignsModel;
  16. use App\Models\TradeModel;
  17. use Illuminate\Support\Facades\DB;
  18. use Psr\Http\Message\MessageInterface;
  19. /**
  20. * 支付回调管理-服务类
  21. * @author wesmiler
  22. * @since 2020/11/11
  23. * Class NotifyService
  24. * @package App\Services
  25. */
  26. class NotifyService extends BaseService
  27. {
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author wesmiler
  32. * @since 2020/11/11
  33. * NotifyService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new GongdengOrderModel();
  38. $this->rechargeModel = new RechargeModel();
  39. }
  40. /**
  41. * 静态入口
  42. * @return NotifyService|null
  43. */
  44. public static function make(){
  45. if(!self::$instance){
  46. self::$instance = new NotifyService();
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 供灯订单回调处理
  52. * @param $notifyData
  53. * @param $outTradeNo
  54. * @return false
  55. */
  56. public function notifyGongdeng($notifyData, $outTradeNo){
  57. $errorKey = "caches:orders:gongdeng:{$outTradeNo}";
  58. // 验证订单是否存在
  59. $orderInfo = $this->model::where(['order_sn'=> $outTradeNo])
  60. ->select(['id','source_id','buy_type','user_id','num','total','status'])
  61. ->first();
  62. // 验证参数
  63. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  64. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  65. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  66. if (empty($orderInfo) || $userId<=0) {
  67. return NotifyService::rebackMsg('订单数据不存在', 'success');
  68. }
  69. // 订单用户
  70. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  71. if(!$memberInfo){
  72. return NotifyService::rebackMsg('订单用户不存在', 'success');
  73. }
  74. // 验证订单状态是否可处理
  75. if ($orderStatus != 1) {
  76. return NotifyService::rebackMsg('订单已处理', 'success');
  77. }
  78. // 验证订单金额是否正确
  79. $payDebug = config('weixin.payDebug');
  80. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  81. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  82. $orderMoney = isset($orderInfo['total']) ? moneyFormat($orderInfo['total']) : 0.00;
  83. $orderAmount = moneyFormat($orderMoney);
  84. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  85. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  86. ], 3600);
  87. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  88. }
  89. // 更新订单数据
  90. DB::beginTransaction();
  91. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  92. 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'=> '已支付'])){
  93. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  94. ], 3600);
  95. DB::rollBack();
  96. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  97. }
  98. // 处理支付明细
  99. $data = [
  100. 'user_id'=> $userId,
  101. 'type'=> 1,
  102. 'coin_type'=> 2,
  103. 'pay_type'=> 2,
  104. 'money'=> moneyFormat($payMoney/100),
  105. 'change_type'=> 2,
  106. 'balance'=> $memberInfo->balance? $memberInfo->balance : 0,
  107. 'create_time'=> time(),
  108. 'remark'=> '供灯订单支付',
  109. 'status'=> 1
  110. ];
  111. if(!TradeModel::insertGetId($data)){
  112. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  113. ], 3600);
  114. DB::rollBack();
  115. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  116. }
  117. DB::commit();
  118. // 开灯处理,续费不处理
  119. $buyType = isset($orderInfo['buy_type'])? $orderInfo['buy_type'] : 0;
  120. if($buyType == 1){
  121. DevicesService::make()->catchLamp($outTradeNo);
  122. }
  123. // 消息处理
  124. return NotifyService::rebackMsg('支付处理成功','success');
  125. }
  126. /**
  127. * 供灯订单回调处理
  128. * @param $notifyData
  129. * @param $outTradeNo
  130. * @return false
  131. */
  132. public function notifyRecharge($notifyData, $outTradeNo){
  133. $errorKey = "caches:orders:recharge:{$outTradeNo}";
  134. // 验证订单是否存在
  135. $orderInfo = $this->rechargeModel::where(['order_sn'=> $outTradeNo])
  136. ->select(['id','type','user_id','num','money','status'])
  137. ->first();
  138. // 验证参数
  139. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  140. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  141. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  142. if (empty($orderInfo) || $userId<=0) {
  143. return NotifyService::rebackMsg('订单数据不存在', 'success');
  144. }
  145. // 订单用户
  146. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  147. if(!$memberInfo){
  148. return NotifyService::rebackMsg('订单用户不存在', 'success');
  149. }
  150. // 验证订单状态是否可处理
  151. if ($orderStatus != 2) {
  152. return NotifyService::rebackMsg('订单已处理', 'success');
  153. }
  154. // 验证订单金额是否正确
  155. $payDebug = config('weixin.payDebug');
  156. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  157. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  158. $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
  159. $orderAmount = moneyFormat($orderMoney);
  160. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  161. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  162. ], 3600);
  163. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  164. }
  165. // 更新订单数据
  166. DB::beginTransaction();
  167. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  168. if(!$this->rechargeModel::where(['order_sn'=> $outTradeNo])->update(['status'=> 1,'trade_no'=> $tradeNo,'pay_at'=> date('Y-m-d H:i:s'),'pay_money'=> moneyFormat($payMoney/100),'remark'=> '已支付'])){
  169. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  170. ], 3600);
  171. DB::rollBack();
  172. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  173. }
  174. $num = isset($orderInfo['num'])? intval($orderInfo['num']) : 0;
  175. if($num && !MemberModel::where(['id'=> $userId])->increment('coupon',$num)){
  176. RedisService::set($errorKey.':error_coupon',['notify'=> $notifyData, 'error'=> '更新账户数据失败','order'=> $orderInfo
  177. ], 3600);
  178. DB::rollBack();
  179. return NotifyService::rebackMsg('更新账户数据失败', 'error');
  180. }
  181. // 处理支付明细
  182. $data = [
  183. 'user_id'=> $userId,
  184. 'type'=> 2,
  185. 'coin_type'=> 1,
  186. 'pay_type'=> 2,
  187. 'money'=> moneyFormat($payMoney/100),
  188. 'change_type'=> 1,
  189. 'balance'=> $memberInfo->coupon? $memberInfo->coupon : 0,
  190. 'create_time'=> time(),
  191. 'remark'=> '充值订单支付',
  192. 'status'=> 1
  193. ];
  194. if(!TradeModel::insertGetId($data)){
  195. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  196. ], 3600);
  197. DB::rollBack();
  198. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  199. }
  200. DB::commit();
  201. return NotifyService::rebackMsg('支付处理成功','success');
  202. }
  203. /**
  204. * 回去回调报文内容
  205. * @param $message
  206. * @return string
  207. */
  208. public static function getBody($message){
  209. $body = '';
  210. $bodyStream = $message->getBody();
  211. if ($bodyStream->isSeekable()) {
  212. $body = (string)$bodyStream;
  213. $bodyStream->rewind();
  214. }
  215. return $body;
  216. }
  217. /**
  218. * 返回成功
  219. */
  220. public static function rebackOk(){
  221. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  222. exit;
  223. }
  224. /**
  225. * V3支付回调应答
  226. * @param $code
  227. * @param string $msg
  228. * @return false|string
  229. */
  230. public static function rebackMsg($msg='失败', $code='error'){
  231. return ['code'=> $code, 'message'=> $msg];
  232. }
  233. }