NotifyService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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','merits_num','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. // 项目完成
  119. $giveGd = ConfigService::make()->getConfigByCode('gongdeng_give_gd');
  120. $giveGd = $giveGd? $giveGd : 0;
  121. if($giveGd>0){
  122. if(!MemberModel::where(['id'=> $userId])->increment('merits_num', $giveGd)){
  123. DB::rollBack();
  124. return NotifyService::rebackMsg('奖励功德失败', 'error');
  125. }
  126. $data = [
  127. 'user_id'=> $userId,
  128. 'type'=> 3,
  129. 'coin_type'=> 4,
  130. 'pay_type'=> 4,
  131. 'money'=> $giveGd,
  132. 'change_type'=> 1,
  133. 'balance'=> $memberInfo->merits_num? $memberInfo->merits_num : 0,
  134. 'create_time'=> time(),
  135. 'remark'=> '供灯奖励',
  136. 'status'=> 1
  137. ];
  138. if(!TradeModel::insertGetId($data)){
  139. RedisService::set($errorKey.':error_gd',['notify'=> $notifyData, 'error'=> '功德奖励交易明细失败','data'=> $data
  140. ], 3600);
  141. DB::rollBack();
  142. return NotifyService::rebackMsg('功德奖励处理失败', 'error');
  143. }
  144. // 完成供灯项目
  145. PracticesService::make()->saveLog($userId, 4,'供灯祈福');
  146. }
  147. DB::commit();
  148. // 记录
  149. $data = [
  150. 'user_id'=> $userId,
  151. 'remark'=> "供灯祈福,福报绵延。",
  152. 'create_time'=> time(),
  153. 'update_time'=> time(),
  154. 'status'=> 1,
  155. ];
  156. RedisService::set("caches:dynamic:notice_{$userId}", $data, 300);
  157. DynamicNoticeModel::insertGetId($data);
  158. // 开灯处理,续费不处理
  159. $buyType = isset($orderInfo['buy_type'])? $orderInfo['buy_type'] : 0;
  160. if($buyType == 1){
  161. DevicesService::make()->catchLamp($outTradeNo);
  162. }
  163. // 消息处理
  164. return NotifyService::rebackMsg('支付处理成功','success');
  165. }
  166. /**
  167. * 供灯订单回调处理
  168. * @param $notifyData
  169. * @param $outTradeNo
  170. * @return false
  171. */
  172. public function notifyRecharge($notifyData, $outTradeNo){
  173. $errorKey = "caches:orders:recharge:{$outTradeNo}";
  174. // 验证订单是否存在
  175. $orderInfo = $this->rechargeModel::where(['order_sn'=> $outTradeNo])
  176. ->select(['id','type','user_id','num','money','status'])
  177. ->first();
  178. // 验证参数
  179. RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
  180. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  181. $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
  182. if (empty($orderInfo) || $userId<=0) {
  183. return NotifyService::rebackMsg('订单数据不存在', 'success');
  184. }
  185. // 订单用户
  186. $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
  187. if(!$memberInfo){
  188. return NotifyService::rebackMsg('订单用户不存在', 'success');
  189. }
  190. // 验证订单状态是否可处理
  191. if ($orderStatus != 2) {
  192. return NotifyService::rebackMsg('订单已处理', 'success');
  193. }
  194. // 验证订单金额是否正确
  195. $payDebug = config('weixin.payDebug');
  196. $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
  197. $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
  198. $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
  199. $orderAmount = moneyFormat($orderMoney);
  200. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  201. RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
  202. ], 3600);
  203. return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
  204. }
  205. // 更新订单数据
  206. DB::beginTransaction();
  207. $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
  208. 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'=> '已支付'])){
  209. RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
  210. ], 3600);
  211. DB::rollBack();
  212. return NotifyService::rebackMsg('更新订单数据失败', 'error');
  213. }
  214. $num = isset($orderInfo['num'])? intval($orderInfo['num']) : 0;
  215. if($num && !MemberModel::where(['id'=> $userId])->increment('coupon',$num)){
  216. RedisService::set($errorKey.':error_coupon',['notify'=> $notifyData, 'error'=> '更新账户数据失败','order'=> $orderInfo
  217. ], 3600);
  218. DB::rollBack();
  219. return NotifyService::rebackMsg('更新账户数据失败', 'error');
  220. }
  221. // 处理支付明细
  222. $data = [
  223. 'user_id'=> $userId,
  224. 'type'=> 2,
  225. 'coin_type'=> 1,
  226. 'pay_type'=> 2,
  227. 'money'=> moneyFormat($payMoney/100),
  228. 'change_type'=> 1,
  229. 'balance'=> $memberInfo->coupon? $memberInfo->coupon : 0,
  230. 'create_time'=> time(),
  231. 'remark'=> '充值订单支付',
  232. 'status'=> 1
  233. ];
  234. if(!TradeModel::insertGetId($data)){
  235. RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
  236. ], 3600);
  237. DB::rollBack();
  238. return NotifyService::rebackMsg('处理交易明细失败', 'error');
  239. }
  240. DB::commit();
  241. return NotifyService::rebackMsg('支付处理成功','success');
  242. }
  243. /**
  244. * 回去回调报文内容
  245. * @param $message
  246. * @return string
  247. */
  248. public static function getBody($message){
  249. $body = '';
  250. $bodyStream = $message->getBody();
  251. if ($bodyStream->isSeekable()) {
  252. $body = (string)$bodyStream;
  253. $bodyStream->rewind();
  254. }
  255. return $body;
  256. }
  257. /**
  258. * 返回成功
  259. */
  260. public static function rebackOk(){
  261. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  262. exit;
  263. }
  264. /**
  265. * V3支付回调应答
  266. * @param $code
  267. * @param string $msg
  268. * @return false|string
  269. */
  270. public static function rebackMsg($msg='失败', $code='error'){
  271. return ['code'=> $code, 'message'=> $msg];
  272. }
  273. }