NotifyService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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'=> 2,
  228. 'pay_type'=> 2,
  229. 'money'=> moneyFormat($payMoney/100),
  230. 'change_type'=> 2,
  231. 'balance'=> 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. if($num>0){
  243. $data = [
  244. 'user_id'=> $userId,
  245. 'type'=> 2,
  246. 'coin_type'=> 1,
  247. 'pay_type'=> 1,
  248. 'money'=> $num,
  249. 'change_type'=> 1,
  250. 'balance'=> $memberInfo->coupon? $memberInfo->coupon : 0,
  251. 'create_time'=> time(),
  252. 'remark'=> '充值花灯券到账',
  253. 'status'=> 1
  254. ];
  255. if(!TradeModel::insertGetId($data)){
  256. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  257. ], 3600);
  258. DB::rollBack();
  259. return NotifyService::rebackMsg('处理花灯券交易明细失败', 'error');
  260. }
  261. }
  262. DB::commit();
  263. return NotifyService::rebackMsg('支付处理成功','success');
  264. }
  265. /**
  266. * 商城订单回调处理
  267. * @param $notifyData
  268. * @param $outTradeNo
  269. * @return false
  270. */
  271. public function notifyShop($notifyData, $outTradeNo){
  272. $errorKey = "caches:orders:shop:{$outTradeNo}";
  273. // 验证订单是否存在
  274. $orderInfo = $this->orderModel::where(['order_sn'=> $outTradeNo])
  275. ->select(['id','type','user_id','goods_id','order_sn','num','total','status'])
  276. ->first();
  277. // 验证参数
  278. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  279. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  280. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  281. if (empty($orderInfo) || $userId<=0) {
  282. return NotifyService::rebackMsg('订单数据不存在', 'success');
  283. }
  284. // 订单用户
  285. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  286. if(!$memberInfo){
  287. return NotifyService::rebackMsg('订单用户不存在', 'success');
  288. }
  289. // 验证订单状态是否可处理
  290. if ($orderStatus != 1) {
  291. return NotifyService::rebackMsg('订单已处理', 'success');
  292. }
  293. // 验证订单金额是否正确
  294. $payDebug = config('weixin.payDebug');
  295. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  296. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  297. $orderMoney = isset($orderInfo['total']) ? moneyFormat($orderInfo['total']) : 0.00;
  298. $orderAmount = moneyFormat($orderMoney);
  299. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  300. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  301. ], 3600);
  302. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  303. }
  304. // 更新订单数据
  305. DB::beginTransaction();
  306. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  307. 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'=> '已支付'])){
  308. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  309. ], 3600);
  310. DB::rollBack();
  311. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  312. }
  313. // 处理支付明细
  314. $data = [
  315. 'user_id'=> $userId,
  316. 'type'=> 1,
  317. 'coin_type'=> 2,
  318. 'pay_type'=> 2,
  319. 'money'=> moneyFormat($payMoney/100),
  320. 'change_type'=> 2,
  321. 'balance'=> 0,
  322. 'create_time'=> time(),
  323. 'remark'=> '商城订单支付',
  324. 'status'=> 1
  325. ];
  326. if(!TradeModel::insertGetId($data)){
  327. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  328. ], 3600);
  329. DB::rollBack();
  330. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  331. }
  332. DB::commit();
  333. return NotifyService::rebackMsg('支付处理成功','success');
  334. }
  335. /**
  336. * 回去回调报文内容
  337. * @param $message
  338. * @return string
  339. */
  340. public static function getBody($message){
  341. $body = '';
  342. $bodyStream = $message->getBody();
  343. if ($bodyStream->isSeekable()) {
  344. $body = (string)$bodyStream;
  345. $bodyStream->rewind();
  346. }
  347. return $body;
  348. }
  349. /**
  350. * 返回成功
  351. */
  352. public static function rebackOk(){
  353. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  354. exit;
  355. }
  356. /**
  357. * V3支付回调应答
  358. * @param $code
  359. * @param string $msg
  360. * @return false|string
  361. */
  362. public static function rebackMsg($msg='失败', $code='error'){
  363. return ['code'=> $code, 'message'=> $msg];
  364. }
  365. }