NotifyService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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\OrdersModel;
  16. use App\Models\RechargeModel;
  17. use App\Models\SignsModel;
  18. use App\Models\TradeModel;
  19. use Illuminate\Support\Facades\DB;
  20. use Psr\Http\Message\MessageInterface;
  21. /**
  22. * 支付回调管理-服务类
  23. * @author wesmiler
  24. * @since 2020/11/11
  25. * Class NotifyService
  26. * @package App\Services
  27. */
  28. class NotifyService extends BaseService
  29. {
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author wesmiler
  34. * @since 2020/11/11
  35. * NotifyService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new GongdengOrderModel();
  40. $this->rechargeModel = new RechargeModel();
  41. $this->orderModel = new OrdersModel();
  42. }
  43. /**
  44. * 静态入口
  45. * @return NotifyService|null
  46. */
  47. public static function make(){
  48. if(!self::$instance){
  49. self::$instance = new NotifyService();
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 供灯订单回调处理
  55. * @param $notifyData
  56. * @param $outTradeNo
  57. * @return false
  58. */
  59. public function notifyGongdeng($notifyData, $outTradeNo){
  60. $errorKey = "caches:orders:gongdeng:{$outTradeNo}";
  61. // 验证订单是否存在
  62. $orderInfo = $this->model::where(['order_sn'=> $outTradeNo])
  63. ->select(['id','source_id','buy_type','user_id','sf_name','qf_content','num','total','status'])
  64. ->first();
  65. // 验证参数
  66. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  67. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  68. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  69. if (empty($orderInfo) || $userId<=0) {
  70. return NotifyService::rebackMsg('订单数据不存在', 'success');
  71. }
  72. // 订单用户
  73. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','merits_num','status'])->first();
  74. if(!$memberInfo){
  75. return NotifyService::rebackMsg('订单用户不存在', 'success');
  76. }
  77. // 验证订单状态是否可处理
  78. if ($orderStatus != 1) {
  79. return NotifyService::rebackMsg('订单已处理', 'success');
  80. }
  81. // 验证订单金额是否正确
  82. $payDebug = config('weixin.payDebug');
  83. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  84. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  85. $orderMoney = isset($orderInfo['total']) ? moneyFormat($orderInfo['total']) : 0.00;
  86. $orderAmount = moneyFormat($orderMoney);
  87. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  88. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  89. ], 3600);
  90. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  91. }
  92. // 更新订单数据
  93. DB::beginTransaction();
  94. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  95. 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'=> '已支付'])){
  96. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  97. ], 3600);
  98. DB::rollBack();
  99. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  100. }
  101. // 处理支付明细
  102. $data = [
  103. 'user_id'=> $userId,
  104. 'type'=> 1,
  105. 'coin_type'=> 2,
  106. 'pay_type'=> 2,
  107. 'money'=> moneyFormat($payMoney/100),
  108. 'change_type'=> 2,
  109. 'balance'=> $memberInfo->balance? $memberInfo->balance : 0,
  110. 'create_time'=> time(),
  111. 'remark'=> '供灯订单支付',
  112. 'status'=> 1
  113. ];
  114. if(!TradeModel::insertGetId($data)){
  115. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  116. ], 3600);
  117. DB::rollBack();
  118. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  119. }
  120. // 项目完成
  121. $giveGd = ConfigService::make()->getConfigByCode('gongdeng_give_gd');
  122. $giveGd = $giveGd? $giveGd : 0;
  123. if($giveGd>0){
  124. if(!MemberModel::where(['id'=> $userId])->increment('merits_num', $giveGd)){
  125. DB::rollBack();
  126. return NotifyService::rebackMsg('奖励功德失败', 'error');
  127. }
  128. $data = [
  129. 'user_id'=> $userId,
  130. 'type'=> 3,
  131. 'coin_type'=> 4,
  132. 'pay_type'=> 4,
  133. 'money'=> $giveGd,
  134. 'change_type'=> 1,
  135. 'balance'=> $memberInfo->merits_num? $memberInfo->merits_num : 0,
  136. 'create_time'=> time(),
  137. 'remark'=> '供灯奖励',
  138. 'status'=> 1
  139. ];
  140. if(!TradeModel::insertGetId($data)){
  141. RedisService::set($errorKey.':error_gd',['notify'=> $notifyData, 'error'=> '功德奖励交易明细失败','data'=> $data
  142. ], 3600);
  143. DB::rollBack();
  144. return NotifyService::rebackMsg('功德奖励处理失败', 'error');
  145. }
  146. // 完成供灯项目
  147. PracticesService::make()->saveLog($userId, 4,'供灯祈福');
  148. }
  149. DB::commit();
  150. // 记录
  151. $data = [
  152. 'user_id'=> $userId,
  153. 'remark'=> "供灯祈福,福报绵延。",
  154. 'create_time'=> time(),
  155. 'update_time'=> time(),
  156. 'status'=> 1,
  157. ];
  158. RedisService::set("caches:dynamic:notice_{$userId}", $data, 300);
  159. DynamicNoticeModel::insertGetId($data);
  160. // 开灯处理,续费不处理
  161. $buyType = isset($orderInfo['buy_type'])? $orderInfo['buy_type'] : 0;
  162. if($buyType == 1){
  163. DevicesService::make()->catchLamp($outTradeNo, $orderInfo);
  164. }
  165. // 消息处理
  166. return NotifyService::rebackMsg('支付处理成功','success');
  167. }
  168. /**
  169. * 充值订单回调处理
  170. * @param $notifyData
  171. * @param $outTradeNo
  172. * @return false
  173. */
  174. public function notifyRecharge($notifyData, $outTradeNo){
  175. $errorKey = "caches:orders:recharge:{$outTradeNo}";
  176. // 验证订单是否存在
  177. $orderInfo = $this->rechargeModel::where(['order_sn'=> $outTradeNo])
  178. ->select(['id','type','user_id','num','money','status'])
  179. ->first();
  180. // 验证参数
  181. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  182. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  183. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  184. if (empty($orderInfo) || $userId<=0) {
  185. return NotifyService::rebackMsg('订单数据不存在', 'success');
  186. }
  187. // 订单用户
  188. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  189. if(!$memberInfo){
  190. return NotifyService::rebackMsg('订单用户不存在', 'success');
  191. }
  192. // 验证订单状态是否可处理
  193. if ($orderStatus != 2) {
  194. return NotifyService::rebackMsg('订单已处理', 'success');
  195. }
  196. // 验证订单金额是否正确
  197. $payDebug = config('weixin.payDebug');
  198. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  199. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  200. $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
  201. $orderAmount = moneyFormat($orderMoney);
  202. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  203. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  204. ], 3600);
  205. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  206. }
  207. // 更新订单数据
  208. DB::beginTransaction();
  209. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  210. 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'=> '已支付'])){
  211. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  212. ], 3600);
  213. DB::rollBack();
  214. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  215. }
  216. $num = isset($orderInfo['num'])? intval($orderInfo['num']) : 0;
  217. if($num && !MemberModel::where(['id'=> $userId])->increment('coupon',$num)){
  218. RedisService::set($errorKey.':error_coupon',['notify'=> $notifyData, 'error'=> '更新账户数据失败','order'=> $orderInfo
  219. ], 3600);
  220. DB::rollBack();
  221. return NotifyService::rebackMsg('更新账户数据失败', 'error');
  222. }
  223. // 处理支付明细
  224. $data = [
  225. 'user_id'=> $userId,
  226. 'type'=> 2,
  227. 'coin_type'=> 1,
  228. 'pay_type'=> 2,
  229. 'money'=> moneyFormat($payMoney/100),
  230. 'change_type'=> 1,
  231. 'balance'=> $memberInfo->coupon? $memberInfo->coupon : 0,
  232. 'create_time'=> time(),
  233. 'remark'=> '充值订单支付',
  234. 'status'=> 1
  235. ];
  236. if(!TradeModel::insertGetId($data)){
  237. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  238. ], 3600);
  239. DB::rollBack();
  240. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  241. }
  242. DB::commit();
  243. return NotifyService::rebackMsg('支付处理成功','success');
  244. }
  245. /**
  246. * 商城订单回调处理
  247. * @param $notifyData
  248. * @param $outTradeNo
  249. * @return false
  250. */
  251. public function notifyShop($notifyData, $outTradeNo){
  252. $errorKey = "caches:orders:shop:{$outTradeNo}";
  253. // 验证订单是否存在
  254. $orderInfo = $this->orderModel::where(['order_sn'=> $outTradeNo])
  255. ->select(['id','type','user_id','goods_id','order_sn','num','total','status'])
  256. ->first();
  257. // 验证参数
  258. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  259. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  260. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  261. if (empty($orderInfo) || $userId<=0) {
  262. return NotifyService::rebackMsg('订单数据不存在', 'success');
  263. }
  264. // 订单用户
  265. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  266. if(!$memberInfo){
  267. return NotifyService::rebackMsg('订单用户不存在', 'success');
  268. }
  269. // 验证订单状态是否可处理
  270. if ($orderStatus != 1) {
  271. return NotifyService::rebackMsg('订单已处理', 'success');
  272. }
  273. // 验证订单金额是否正确
  274. $payDebug = config('weixin.payDebug');
  275. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  276. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  277. $orderMoney = isset($orderInfo['total']) ? moneyFormat($orderInfo['total']) : 0.00;
  278. $orderAmount = moneyFormat($orderMoney);
  279. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  280. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  281. ], 3600);
  282. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  283. }
  284. // 更新订单数据
  285. DB::beginTransaction();
  286. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  287. if(!$this->orderModel::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'=> '已支付'])){
  288. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  289. ], 3600);
  290. DB::rollBack();
  291. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  292. }
  293. // 处理支付明细
  294. $data = [
  295. 'user_id'=> $userId,
  296. 'type'=> 1,
  297. 'coin_type'=> 2,
  298. 'pay_type'=> 2,
  299. 'money'=> moneyFormat($payMoney/100),
  300. 'change_type'=> 2,
  301. 'balance'=> 0,
  302. 'create_time'=> time(),
  303. 'remark'=> '商城订单支付',
  304. 'status'=> 1
  305. ];
  306. if(!TradeModel::insertGetId($data)){
  307. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  308. ], 3600);
  309. DB::rollBack();
  310. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  311. }
  312. DB::commit();
  313. return NotifyService::rebackMsg('支付处理成功','success');
  314. }
  315. /**
  316. * 回去回调报文内容
  317. * @param $message
  318. * @return string
  319. */
  320. public static function getBody($message){
  321. $body = '';
  322. $bodyStream = $message->getBody();
  323. if ($bodyStream->isSeekable()) {
  324. $body = (string)$bodyStream;
  325. $bodyStream->rewind();
  326. }
  327. return $body;
  328. }
  329. /**
  330. * 返回成功
  331. */
  332. public static function rebackOk(){
  333. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  334. exit;
  335. }
  336. /**
  337. * V3支付回调应答
  338. * @param $code
  339. * @param string $msg
  340. * @return false|string
  341. */
  342. public static function rebackMsg($msg='失败', $code='error'){
  343. return ['code'=> $code, 'message'=> $msg];
  344. }
  345. }