NotifyService.php 10 KB

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