RechargeController.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\service\coupon\CouponService;
  4. use app\admin\service\member\MemberService;
  5. use app\admin\service\order\OrderService;
  6. use app\admin\service\order\OrderSheetService;
  7. use app\admin\service\order\PayDetailService;
  8. use app\admin\validate\order\OrderValidate;
  9. use app\controller\Curd;
  10. use app\model\Appointment;
  11. use app\model\ClientConfig;
  12. use app\model\Coupon;
  13. use app\model\CouponDetail;
  14. use app\model\Goods;
  15. use app\model\GoodsComponent;
  16. use app\model\GoodsRunning;
  17. use app\model\GoodsSku;
  18. use app\model\Member;
  19. use app\model\MemberAccount;
  20. use app\model\MemberBenefit;
  21. use app\model\MemberRole;
  22. use app\model\Order;
  23. use app\model\OrderExpress;
  24. use app\model\OrderReturn;
  25. use app\model\OrderSheet;
  26. use app\model\PayDetail;
  27. use app\model\Supplier;
  28. use app\model\SysDept;
  29. use app\model\SysUser;
  30. use support\Db;
  31. use support\exception\BusinessException;
  32. use support\Redis;
  33. use support\Request;
  34. use support\Response;
  35. use Tinywan\Jwt\JwtToken;
  36. use Webman\Event\Event;
  37. use Yansongda\Pay\Pay;
  38. class RechargeController extends Curd
  39. {
  40. public function __construct()
  41. {
  42. $this->model = new Order();
  43. $this->validate = true;
  44. $this->validateClass = new OrderValidate();
  45. }
  46. /**
  47. * @Desc 列表
  48. * @Author Gorden
  49. * @Date 2024/3/28 15:01
  50. *
  51. * @param Request $request
  52. * @return Response
  53. * @throws \support\exception\BusinessException
  54. */
  55. public function select(Request $request): Response
  56. {
  57. $timeType = $request->get('time_type', 'add');
  58. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  59. $where['order_classify'] = 'RECHARGE';
  60. if (!empty($where['order_addtimes']) && $timeType == 'add') {
  61. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  62. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  63. }
  64. $order = $request->get('order', 'desc');
  65. $orderId = $request->get('orderId');
  66. $field = $field ?? 'order_addtimes';
  67. $orderIds = [];
  68. if (!empty($orderId)) {
  69. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  70. ->where('order_classify', 'RECHARGE')
  71. ->pluck('order_id')
  72. ->toArray();
  73. }
  74. $goodsName = trim($request->get('goods_name', ''));
  75. if (!empty($goodsName)) {
  76. $goodsIds = Goods::where('goods_classify', 'RECHARGE')
  77. ->where('goods_name', 'like', '%' . $goodsName . '%')
  78. ->pluck('goods_id')
  79. ->toArray();
  80. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  81. if (!empty($where['order_id'])) {
  82. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  83. } else {
  84. $orderIds = $goodsOrderIds;
  85. }
  86. }
  87. // 付款时间
  88. $payOrderIds = [];
  89. if ($timeType == 'pay' && !empty($where['order_addtimes'])) {
  90. $payTimeStart = strtotime($where['order_addtimes'][0]);
  91. $payTimeEnd = strtotime($where['order_addtimes'][1]);
  92. unset($where['order_addtimes']);
  93. $payOrderIds = PayDetailService::getPayOrderId($payTimeStart, $payTimeEnd, ['RECHARGE']);
  94. }
  95. if ((!empty($orderId) || !empty($goodsName)) && $timeType == 'pay') {
  96. $orderIds = array_intersect($orderIds, $payOrderIds);
  97. $orderIds[] = '00';
  98. $where['order_id'] = ['in', implode(',', $orderIds)];
  99. } elseif (!empty($orderId) || !empty($goodsName)) {
  100. $orderIds[] = '00';
  101. $where['order_id'] = ['in', implode(',', $orderIds)];
  102. } elseif ($timeType == 'pay') {
  103. $payOrderIds[] = '00';
  104. $where['order_id'] = ['in', implode(',', $payOrderIds)];
  105. }
  106. $query = $this->doSelect($where, $field, $order);
  107. return $this->doFormat($query, $format, $limit);
  108. }
  109. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  110. {
  111. $model = $this->model->with([
  112. 'sheets' => function ($query) {
  113. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  114. },
  115. 'member' => function ($query) {
  116. $query->select('member_id', 'member_mobile');
  117. },
  118. 'cert' => function ($query) {
  119. $query->select('join_cert_member_id', 'member_cert_name');
  120. },
  121. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
  122. ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
  123. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  124. foreach ($where as $column => $value) {
  125. if (is_array($value)) {
  126. if ($value[0] === 'like' || $value[0] === 'not like') {
  127. $model = $model->where($column, $value[0], "%$value[1]%");
  128. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  129. $model = $model->where($column, $value[0], $value[1]);
  130. } elseif ($value[0] == 'in' && !empty($value[1])) {
  131. $valArr = $value[1];
  132. if (is_string($value[1])) {
  133. $valArr = explode(",", trim($value[1]));
  134. }
  135. $model = $model->whereIn($column, $valArr);
  136. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  137. $valArr = $value[1];
  138. if (is_string($value[1])) {
  139. $valArr = explode(",", trim($value[1]));
  140. }
  141. $model = $model->whereNotIn($column, $valArr);
  142. } elseif ($value[0] == 'null') {
  143. $model = $model->whereNull($column);
  144. } elseif ($value[0] == 'not null') {
  145. $model = $model->whereNotNull($column);
  146. } elseif ($value[0] !== '' || $value[1] !== '') {
  147. $model = $model->whereBetween($column, $value);
  148. }
  149. } else {
  150. $model = $model->where($column, $value);
  151. }
  152. }
  153. if ($field) {
  154. $model = $model->orderBy($field, $order);
  155. }
  156. $model = $model->whereJsonContains('order_config_json->order_category', 'RECHARGE')
  157. ->select('order.*', 'order_express.join_express_order_id', 'order_express.order_express_type', 'order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status', 'order_return.order_return_apply_json', 'order_return.order_return_remark');
  158. return $model;
  159. }
  160. public function afterQuery($items)
  161. {
  162. foreach ($items as &$item) {
  163. $sheetDeng = '';
  164. $item['is_crown'] = 'N';
  165. $item['sheet'] = $item['sheets'][0] ?? [];
  166. if (!empty($item['sheet'])) {
  167. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  168. if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
  169. $sheetDeng = ' 等';
  170. }
  171. if ($goods && $goods->goods_name && strpos($goods->goods_name, '皇冠') !== false) {
  172. $item['is_crown'] = 'Y';
  173. }
  174. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '自定义充值';
  175. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  176. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  177. }
  178. unset($item['sheets']);
  179. if (isset($item['orders_return_id'])) {
  180. $item['return'] = [
  181. 'orders_return_id' => $item['orders_return_id'],
  182. 'join_return_order_id' => $item['join_return_order_id'],
  183. 'order_return_status' => $item['order_return_status'],
  184. 'order_return_apply_json' => $item['order_return_apply_json'],
  185. 'order_return_remark' => $item['order_return_remark']
  186. ];
  187. }
  188. if (isset($item['join_express_order_id'])) {
  189. $item['express'] = [
  190. 'join_express_order_id' => $item['join_express_order_id'],
  191. 'order_express_type' => $item['order_express_type']
  192. ];
  193. unset($item['join_express_order_id'], $item['order_express_type']);
  194. }
  195. $item['payDetail'] = PayDetailService::getPayWay($item['order_groupby'], $item['order_id']);
  196. }
  197. return $items;
  198. }
  199. /**
  200. * @Desc 下单+支付
  201. * @Author Gorden
  202. * @Date 2024/9/5 13:13
  203. *
  204. * @param Request $request
  205. * @return Response
  206. */
  207. public function insert(Request $request): Response
  208. {
  209. $params = $request->post();
  210. // 判断餐品是否连带着服务或实体
  211. $goodsClassifys = array_unique(array_column($params['goodsContentList'], 'goods_classify'));
  212. $params['goods_classify'] = $goodsClassifys[0];
  213. $orderExtendJson = [];
  214. Db::beginTransaction();
  215. try {
  216. // 验证线下付款密码
  217. if ($params['settlement_now'] == 'Y' && $params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  218. $password = $params['offline_password'];
  219. if ($password != '666888') {
  220. throw new BusinessException('密码错误,请重新输入');
  221. }
  222. }
  223. if (!empty($params['dept_id'])) {
  224. $dept = SysDept::where('dept_id', $params['dept_id'])->first();
  225. if (!$dept) {
  226. throw new BusinessException("充值场所不存在");
  227. }
  228. $params['dept_name'] = $dept->dept_name;
  229. $orderExtendJson = ['referee_dept_id' => $params['dept_id'], 'referee_premises' => $params['dept_name']];
  230. }
  231. $orderExtendJson['referee'] = $params['referee'] ?? '';
  232. // 下单账户
  233. if (empty($params['join_order_member_id']) && !empty($params['mobile'])) {
  234. if (Member::where('member_mobile', $params['mobile'])->exists()) {
  235. throw new BusinessException('会员已存在');
  236. }
  237. $params['join_order_member_id'] = $params['member_id'] = 'MR' . date('ymdHi') . random_string(4, 'up');
  238. // 创建会员
  239. MemberService::createMember($params);
  240. } else if (empty($params['join_order_member_id']) && empty($params['mobile'])) {
  241. $params['join_order_member_id'] = Member::where('member_mobile', '0000')->value('member_id');
  242. }
  243. if (empty($params['join_order_member_id'])) {
  244. throw new BusinessException('检查下单账户');
  245. }
  246. $params['orderId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  247. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  248. $systemStatus = 'PAYING';
  249. // 立即结算
  250. if ($params['settlement_now'] == 'Y') {
  251. if ($params['goods_classify'] == 'RECHARGE') {
  252. $params['order_is_complete'] = 'Y';
  253. $systemStatus = 'DONE';
  254. }
  255. }
  256. if ($params['settlement_now'] == 'Y' && ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY')) {
  257. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  258. $params['pay_category'] = $params['pay_category_sub'];
  259. }
  260. $params['order_status_system'] = $systemStatus;
  261. $params['order_status_payment'] = 'SUCCESS';
  262. }
  263. if ($params['pay_category'] == 'QRCODE' && $params['settlement_now'] == 'Y' && !empty($params['qrcode_nbr'])) { // 付款码
  264. $result = OrderService::qrcodePay($params);
  265. $result = json_encode($result);
  266. $params['pay_json_response'] = $result;
  267. $result = json_decode($result, true);
  268. $prefix = substr($params['qrcode_nbr'], 0, 2);
  269. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  270. $params['pay_category'] = 'WXPAY';
  271. if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) {
  272. $params['order_status_system'] = 'PAYING';
  273. $params['order_status_payment'] = 'PENDING';
  274. $params['order_is_complete'] = 'N';
  275. } else {
  276. $params['order_status_system'] = $systemStatus;
  277. $params['order_status_payment'] = 'SUCCESS';
  278. }
  279. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  280. $params['pay_category'] = 'ALIPAY';
  281. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  282. $params['order_status_system'] = 'PAYING';
  283. $params['order_status_payment'] = 'PENDING';
  284. $params['order_is_complete'] = 'N';
  285. } else {
  286. $params['order_status_system'] = $systemStatus;
  287. $params['order_status_payment'] = 'SUCCESS';
  288. }
  289. } else {
  290. throw new BusinessException('付款码无效');
  291. }
  292. }
  293. $params['order_extend_json'] = $orderExtendJson;
  294. // 写入主订单
  295. $this->insertMain($params);
  296. // 订单详情
  297. $this->insertSheet($params);
  298. // 支付记录
  299. $payId = $this->insertPayDetail($params);
  300. Db::commit();
  301. if ($params['order_status_payment'] == 'SUCCESS') {
  302. // 入收支明细表
  303. // $params['inout_category'] = '会员充值';
  304. // Event::dispatch('statistics.inout.in', $params);
  305. // 充值回调
  306. (new \app\admin\controller\notify\RechargeController)->disposePaySuccess($payId);
  307. }
  308. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
  309. _syslog("订单", "支付异常,检查是否有轮询");
  310. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  311. }
  312. _syslog("订单", "创建订单成功");
  313. return json_success('创建订单成功');
  314. } catch (BusinessException $e) {
  315. Db::rollBack();
  316. dump($e->getMessage());
  317. dump($e->getTrace());
  318. _syslog("订单", $e->getMessage());
  319. return json_fail($e->getMessage());
  320. } catch (\Exception $e) {
  321. Db::rollBack();
  322. dump($e->getMessage());
  323. dump($e->getTrace());
  324. _syslog("订单", "创建订单失败");
  325. return json_fail('创建订单失败');
  326. }
  327. }
  328. /**
  329. * @Desc 支付
  330. * @Author Gorden
  331. * @Date 2024/9/5 13:12
  332. *
  333. * @param Request $request
  334. * @return Response
  335. */
  336. public function pay(Request $request)
  337. {
  338. $params = $request->post();
  339. // 验证线下付款密码
  340. if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) {
  341. $password = $params['offline_password'];
  342. if ($password != '666888') {
  343. return json_fail("密码错误,请重新输入");
  344. }
  345. }
  346. $order = Order::where('order_id', $params['order_id'])->first();
  347. if (!$order) {
  348. return json_fail('订单异常');
  349. }
  350. if ($order->order_status_system != 'PAYING') {
  351. return json_fail('订单不是可支付状态');
  352. }
  353. $params['orderId'] = $params['order_id'];
  354. $params['orderGroupId'] = 'OD' . date('ymdHi') . random_string(4, 'up');
  355. $order->order_groupby = $params['orderGroupId'];
  356. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
  357. ->select('goods_id', 'goods_name', 'goods_classify')
  358. ->first();
  359. if (!$goods) {
  360. return json_fail('产品数据异常');
  361. }
  362. $goods = $goods->toArray();
  363. $params['goods_classify'] = $goods['goods_classify'] ?? '';
  364. // 入伙场所
  365. if (!empty($params['dept_id'])) {
  366. $orderExtendJson = [];
  367. if (!empty($order->order_extend_json)) {
  368. $orderExtendJson = json_decode($order->order_extend_json, true);
  369. }
  370. $dept = SysDept::where('dept_id', $params['dept_id'])->first();
  371. if (!$dept) {
  372. throw new BusinessException("入伙场所不存在");
  373. }
  374. $params['dept_name'] = $dept->dept_name;
  375. $orderExtendJson['dept_id'] = $params['dept_id'];
  376. $orderExtendJson['dept_name'] = $params['dept_name'];
  377. $order->order_extend_json = json_encode($orderExtendJson);
  378. }
  379. // 立即结算
  380. if ($params['goods_classify'] == 'RECHARGE') {
  381. $order->order_is_complete = 'Y';
  382. $systemStatus = 'DONE';
  383. }
  384. $paymentStatus = 'PENDING';
  385. Db::beginTransaction();
  386. try {
  387. // 组合支付时,付款码应收金额
  388. $qrcodePayAmount = 0;
  389. if ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY') {
  390. $order->order_status_system = $systemStatus;
  391. $order->order_status_payment = 'SUCCESS';
  392. $paymentStatus = 'SUCCESS';
  393. if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
  394. $params['pay_category'] = $params['pay_category_sub'];
  395. }
  396. }
  397. if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && !empty($params['qrcode_nbr'])) { // 付款码
  398. // 提交过来的支付分类
  399. $submitPayCategory = $params['pay_category'];
  400. // 账户支付的金额
  401. $accountAmount = $params['order_amount_pay'];
  402. if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
  403. // 组合支付,改成需要付款码需要支付的金额
  404. $params['order_amount_pay'] = $qrcodePayAmount;
  405. }
  406. // 去支付
  407. $result = OrderService::qrcodePay($params);
  408. $result = json_encode($result);
  409. $params['pay_json_response'] = $result;
  410. $result = json_decode($result, true);
  411. $prefix = substr($params['qrcode_nbr'], 0, 2);
  412. if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
  413. $params['pay_category'] = 'WXPAY';
  414. if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) {
  415. $order->order_status_system = 'PAYING';
  416. $order->order_status_payment = 'PENDING';
  417. $order->order_is_complete = 'N';
  418. // Db::rollBack();
  419. // return json_fail('支付失败');
  420. } else {
  421. $order->order_status_system = $systemStatus;
  422. $order->order_status_payment = 'SUCCESS';
  423. $paymentStatus = 'SUCCESS';
  424. }
  425. } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
  426. $params['pay_category'] = 'ALIPAY';
  427. if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
  428. $order->order_status_system = 'PAYING';
  429. $order->order_status_payment = 'PENDING';
  430. $order->order_is_complete = 'N';
  431. // Db::rollBack();
  432. // return json_fail('支付失败');
  433. } else {
  434. $order->order_status_system = $systemStatus;
  435. $order->order_status_payment = 'SUCCESS';
  436. $paymentStatus = 'SUCCESS';
  437. }
  438. } else {
  439. throw new BusinessException('付款码无效');
  440. }
  441. // 账户支付的金额
  442. $params['order_amount_pay'] = $accountAmount;
  443. }
  444. if ($order->order_status_payment == 'SUCCESS') {
  445. $order->order_is_complete = 'Y';
  446. }
  447. // 主订单
  448. $order->save();
  449. // sheet
  450. if ($order->order_status_payment == 'SUCCESS') {
  451. $data = [
  452. 'order_sheet_status' => $systemStatus
  453. ];
  454. if (floatval($order->order_amount_paid) < $order->order_amount_total) {
  455. $data['order_sheet_status'] = 'BEING';
  456. }
  457. OrderSheet::where('join_sheet_order_id', $params['order_id'])->update($data);
  458. }
  459. // 清除此订单的未支付记录
  460. PayDetail::whereJsonContains('join_pay_object_json->order_id', $order->order_id)->where('pay_status', 'WAITING')->delete();
  461. // 重新创建
  462. $payId = $this->insertPayDetail($params);
  463. Db::commit();
  464. if ($order->order_status_payment == 'SUCCESS') {
  465. // 入收支明细表
  466. $params['inout_category'] = '会员充值';
  467. Event::dispatch('statistics.inout.in', $params);
  468. // 加销售量
  469. OrderSheetService::addGoodsSales($params['orderId']);
  470. // 充值回调
  471. (new \app\admin\controller\notify\RechargeController)->disposePaySuccess($payId);
  472. }
  473. if ($order->order_status_payment != 'SUCCESS' && $paymentStatus != 'SUCCESS') {
  474. _syslog("订单", "支付异常,检查是否有轮询");
  475. return json_throw(2001, '支付异常', ['order_id' => $params['orderId'], 'group_id' => $params['orderGroupId']]);
  476. }
  477. _syslog("订单", "订单支付成功");
  478. return json_success('支付成功');
  479. } catch (BusinessException $e) {
  480. dump($e->getMessage());
  481. Db::rollBack();
  482. _syslog("订单", "订单支付失败:" . $e->getMessage());
  483. return json_fail("支付失败:" . $e->getMessage());
  484. } catch (\Exception $e) {
  485. dump($e->getMessage());
  486. Db::rollBack();
  487. _syslog("订单", "订单支付失败");
  488. return json_fail('支付失败');
  489. }
  490. }
  491. /**
  492. * @Desc
  493. * @Author Gorden
  494. * @Date 2024/6/7 10:30
  495. *
  496. * @param $params
  497. * @return void
  498. * @throws BusinessException
  499. */
  500. public function insertMain($params)
  501. {
  502. try {
  503. $orderCategory = 'NORMAL';
  504. if (!empty($params['order_category'])) {
  505. $orderCategory = $params['order_category'];
  506. } else if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  507. $orderCategory = 'DISHES';
  508. } else if (isset($params['goods_classify'])) {
  509. $orderCategory = $params['goods_classify'];
  510. }
  511. if (empty($params['order_extend_json'])) {
  512. $params['order_extend_json'] = [];
  513. } else {
  514. if (is_json($params['order_extend_json'])) {
  515. $params['order_extend_json'] = json_decode($params['order_extend_json'], true);
  516. }
  517. }
  518. // 推荐人
  519. $params['order_extend_json']['referee'] = $params['referee'] ?? '';
  520. $data = [
  521. 'order_id' => $params['orderId'],
  522. 'order_groupby' => $params['orderGroupId'],
  523. 'join_order_member_id' => $params['join_order_member_id'],
  524. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  525. 'order_amount_total' => $params['order_amount_total'],
  526. 'order_amount_pay' => $params['order_status_payment'] == 'SUCCESS' ? $params['order_amount_pay'] : 0,
  527. 'order_amount_paid' => $params['order_status_payment'] == 'SUCCESS' ? $params['order_amount_pay'] : 0,
  528. 'order_category' => $orderCategory,
  529. 'order_classify' => $orderCategory,
  530. 'order_is_complete' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) && $params['order_status_payment'] == 'SUCCESS' ? 'Y' : 'N',
  531. 'order_status_system' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
  532. 'order_status_payment' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'PENDING',
  533. 'order_status_storage' => $params['order_status_storage'],
  534. 'order_platform' => $params['order_platform'],
  535. 'order_remark' => $params['order_remark'] ?? '',
  536. 'order_discount_json' => $params['order_discount_json'] ?? '[]',
  537. 'order_config_json' => json_encode(['order_category' => 'RECHARGE']),
  538. 'order_express_json' => $params['order_express_json'] ?? '[]',
  539. 'order_extend_json' => $params['order_extend_json'] ? json_encode($params['order_extend_json']) : '[]',
  540. 'order_addtimes' => time(),
  541. 'order_add_user_id'=>JwtToken::getCurrentId()
  542. ];
  543. Order::insert($data);
  544. } catch (\Exception $e) {
  545. dump($e->getMessage());
  546. throw new BusinessException('订单创建信息失败');
  547. }
  548. }
  549. /**
  550. * @Desc
  551. * @Author Gorden
  552. * @Date 2024/6/7 10:25
  553. *
  554. * @param $params
  555. * @return void
  556. * @throws BusinessException
  557. */
  558. public function insertSheet($params)
  559. {
  560. try {
  561. $orderSheetIds = [];
  562. foreach ($params['goodsContentList'] as $goods) {
  563. //{"unit": "份", "table": null, "premises": "15"}
  564. $price = floatval($goods['goods_sales_price']);
  565. $extendJson['unit'] = $goods['sku_name'];
  566. if (isset($params['submit_premises_id'])) {
  567. $extendJson['premises'] = $params['submit_premises_id'];
  568. }
  569. if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') {
  570. $extendJson['table'] = null;
  571. }
  572. $orderSheetStatus = 'PAYING';
  573. if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
  574. if (floatval($params['order_amount_pay']) >= floatval($params['order_amount_total'])) {
  575. $orderSheetStatus = 'DONE';
  576. } else {
  577. $orderSheetStatus = 'BEING';
  578. }
  579. }
  580. $data = [
  581. 'join_sheet_member_id' => $params['join_order_member_id'],
  582. 'join_sheet_order_id' => $params['orderId'],
  583. 'join_sheet_goods_id' => $goods['goods_id'],
  584. 'join_sheet_goods_sku_id' => $goods['sku_id'],
  585. 'order_sheet_status' => $orderSheetStatus,
  586. 'order_sheet_category' => 'RECHARGE',
  587. 'order_sheet_num' => $goods['nbr'],
  588. 'order_sheet_price' => $goods['goods_sales_price'],
  589. 'order_sheet_price_pay' => $goods['goods_sales_price'],
  590. 'order_sheet_amount' => $price * $goods['nbr'],
  591. 'order_sheet_pay' => $price * $goods['nbr'],
  592. 'order_sheet_task_status' => 'NONE',
  593. 'order_sheet_remark' => $params['order_remark'] ?? '',
  594. 'order_sheet_addtimes' => time(),
  595. 'order_sheet_extend_json' => json_encode($extendJson)
  596. ];
  597. $orderSheetId = OrderSheet::insertGetId($data);
  598. $orderSheetIds[] = $orderSheetId;
  599. // 减库存,规格和总库存
  600. if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) {
  601. $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first();
  602. $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true);
  603. if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) {
  604. $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr'];
  605. }
  606. if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) {
  607. throw new BusinessException('库存不足');
  608. }
  609. $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson);
  610. $goodsSku->save();
  611. }
  612. $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first();
  613. $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr'];
  614. if ($goodsRunning->goods_running_storage < 0) {
  615. throw new BusinessException('库存不足');
  616. }
  617. if ($params['order_status_payment'] == 'SUCCESS') {
  618. $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr'];
  619. }
  620. $goodsRunning->save();
  621. }
  622. return $orderSheetIds;
  623. } catch (\support\exception\BusinessException $e) {
  624. dump($e->getMessage() . '||' . $e->getLine());
  625. throw new BusinessException($e->getMessage());
  626. } catch (\Exception $e) {
  627. dump($e->getMessage() . '||' . $e->getLine());
  628. throw new BusinessException('订单创建失败');
  629. }
  630. }
  631. /**
  632. * @Desc
  633. * @Author Gorden
  634. * @Date 2024/6/7 10:35
  635. *
  636. * @param $params
  637. * @throws BusinessException
  638. */
  639. public function insertPayDetail($params)
  640. {
  641. try {
  642. if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) {
  643. $payPrepayid = $params['pay_category'];
  644. } else if ($params['pay_category'] == 'OFFLINE') {
  645. $payPrepayid = 'OFFLINE';
  646. } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') {
  647. $payPrepayid = 'OFFLINE_ALIPAY';
  648. } else if ($params['pay_category'] == 'OFFLINE_WXPAY') {
  649. $payPrepayid = 'OFFLINE_WXPAY';
  650. } else if ($params['pay_category'] == 'MONEY') {
  651. $payPrepayid = 'MONEY';
  652. } else {
  653. $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category'];
  654. }
  655. $sheet = OrderSheet::with('goods')->where('join_sheet_order_id', $params['orderId'])->first();
  656. $payExtendJson = [];
  657. if (!empty($sheet->goods)) {
  658. $goodsAttributeJson = json_decode($sheet->goods->goods_attribute_json, true);
  659. $addedRate = 0;
  660. if (!empty($goodsAttributeJson['added'])) {
  661. $addedRate = $goodsAttributeJson['added']['nbr'];
  662. }
  663. $payExtendJson = [
  664. 'added_rate' => $addedRate,
  665. 'added_amount' => round($addedRate * $params['order_amount_pay'], 2)
  666. ];
  667. }
  668. $data = [
  669. 'join_pay_member_id' => $params['join_order_member_id'],
  670. 'join_pay_order_id' => $params['orderGroupId'],
  671. 'pay_status' => $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
  672. 'pay_category' => $params['goods_classify'],
  673. 'pay_amount' => $params['order_amount_pay'],
  674. 'pay_prepayid' => $payPrepayid,
  675. 'pay_paytimes' => date('Y-m-d H:i:s'),
  676. 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
  677. 'pay_json_request' => json_encode($params),
  678. 'pay_json_response' => $params['pay_json_response'] ?? '[]',
  679. 'pay_extend_json' => json_encode($payExtendJson),
  680. 'pay_remark' => $params['order_remark'] ?? '',
  681. 'pay_addtimes' => time(),
  682. ];
  683. $payId = PayDetail::insertGetId($data);
  684. return $payId;
  685. } catch (\Exception $e) {
  686. dump($e->getMessage());
  687. throw new BusinessException('创建支付记录失败');
  688. }
  689. }
  690. /**
  691. * @Desc 订单商品详情
  692. * @Author Gorden
  693. * @Date 2024/3/29 8:50
  694. *
  695. * @param Request $request
  696. * @return Response
  697. */
  698. public function sheet(Request $request)
  699. {
  700. try {
  701. $orderId = $request->get('order_id');
  702. $orderSheet = OrderSheet::with([
  703. 'member' => function ($query) {
  704. $query->select('member_id', 'member_mobile', 'member_is_owner', 'join_member_role_id', 'member_is_vip', 'member_is_partner', 'member_is_referrer', 'member_is_franchisee');
  705. },
  706. 'goods' => function ($query) {
  707. $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify', 'goods_if_express', 'goods_attribute_json');
  708. },
  709. 'memberInfo',
  710. 'cert',
  711. 'sku' => function ($query) {
  712. $query->where('goods_sku_status', 'ON')
  713. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price');
  714. },
  715. 'skus',
  716. 'refund' => function ($query) {
  717. $query->select('join_return_order_id', 'order_return_status');
  718. }
  719. ])->where('join_sheet_order_id', $orderId)
  720. ->orderBy('order_sheet_id', 'DESC')
  721. ->get()
  722. ->toArray();
  723. $order = Order::where('order_id', $orderId)->first();
  724. $express = OrderExpress::where('join_express_order_id', $orderId)->first();
  725. $premises = '';
  726. if ($express && $express->order_express_type == '自提') {
  727. $premises = $express->order_express_company;
  728. }
  729. $sheetAmount = 0;
  730. $coupons = [];
  731. foreach ($orderSheet as &$item) {
  732. $sheetAmount += $item['order_sheet_amount'];
  733. if (!empty($item['goods'])) {
  734. $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
  735. }
  736. if (!empty($item['sku'])) {
  737. if (!empty($item['sku']['goods_sku_specs_json'])) {
  738. $specsJson = json_decode($item['sku']['goods_sku_specs_json'], true);
  739. $skuName = '';
  740. foreach ($specsJson as $specsKey => $skuSpecs) {
  741. if (is_array($skuSpecs)) {
  742. $skuName = $skuName . ' ' . implode(' ', $skuSpecs) . ';';
  743. } else {
  744. $skuName = $skuName . ' ' . $skuSpecs . ';';
  745. }
  746. }
  747. $item['sku']['goods_sku_title'] = rtrim($skuName, ';');
  748. }
  749. }
  750. if (!empty($item['skus'])) {
  751. foreach ($item['skus'] as $key => $skus) {
  752. if (!empty($skus['goods_sku_specs_json'])) {
  753. $item['skus'][$key]['goods_sku_specs_json'] = json_decode($skus['goods_sku_specs_json']);
  754. $skuName = '';
  755. foreach ($item['skus'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  756. $keyStr = ($specsKey == '规格') ? '' : ($specsKey . ':');
  757. if (is_array($skuSpecs)) {
  758. $skuName = $skuName . $keyStr . ' ' . implode(' ', $skuSpecs) . '; ';
  759. } else {
  760. $skuName = $skuName . $keyStr . ' ' . $skuSpecs . '; ';
  761. }
  762. }
  763. $item['skus'][$key]['sku_name'] = rtrim($skuName, '; ');
  764. }
  765. }
  766. } else {
  767. $item['skus'] = [];
  768. }
  769. // 产品携带的优惠券
  770. if (!empty($item['goods']['goods_attribute_json'])) {
  771. $goodsAttributeJson = json_decode($item['goods']['goods_attribute_json'], true);
  772. if (isset($goodsAttributeJson['coupon'])) {
  773. $couponIds = array_keys($goodsAttributeJson['coupon']);
  774. $coupons = Coupon::whereIn('coupon_id', $couponIds)
  775. ->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_is_period', 'coupon_period_json', 'coupon_addtimes')
  776. ->get()
  777. ->toArray();
  778. foreach ($coupons as &$coupon) {
  779. if (isset($goodsAttributeJson['coupon'][$coupon['coupon_id']])) {
  780. $coupon['num'] = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  781. ->where('join_coupon_detail_member_id', $order->join_order_member_id)
  782. ->whereJsonContains('coupon_detail_extend_json->order_id', $order->order_id)
  783. ->count();
  784. $coupon['used'] = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  785. ->where('join_coupon_detail_member_id', $order->join_order_member_id)
  786. ->whereJsonContains('coupon_detail_extend_json->order_id', $order->order_id)
  787. ->where('coupon_detail_status', 'USED')
  788. ->count();
  789. $coupon['invalid'] = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  790. ->where('join_coupon_detail_member_id', $order->join_order_member_id)
  791. ->whereJsonContains('coupon_detail_extend_json->order_id', $order->order_id)
  792. ->whereIn('coupon_detail_status', ['DISABLED', 'CANCEL', 'EXPIRED'])
  793. ->count();
  794. $coupon['unused'] = $coupon['num'] - $coupon['used'] - $coupon['invalid'];
  795. }
  796. }
  797. }
  798. if (isset($goodsAttributeJson['added'])) {
  799. $item['goods']['added_amount'] = floatval($goodsAttributeJson['added']['nbr']) * $item['goods']['goods_sales_price'];
  800. $item['goods']['added_rate'] = floatval($goodsAttributeJson['added']['nbr']) * 100;
  801. }
  802. }
  803. if (!empty($item['order_sheet_extend_json']) && !$express) {
  804. $extendJson = json_decode($item['order_sheet_extend_json'], true);
  805. if (isset($extendJson['address_id'])) {
  806. $address = ClientConfig::where('client_config_id', $extendJson['address_id'])->first();
  807. if (!empty($address)) {
  808. $valJson = json_decode($address->client_config_val_json, true);
  809. $express = [
  810. 'order_express_address' => $valJson['address'] . ($valJson['numbers'] ?? ''),
  811. 'order_express_city' => $valJson['city'],
  812. 'order_express_mobile' => $valJson['mobile'],
  813. 'order_express_person' => $valJson['person']
  814. ];
  815. }
  816. }
  817. }
  818. $item['member']['level'] = '普通会员';
  819. if (!empty($item['member']['join_member_role_id'])) {
  820. $item['member']['level'] = MemberRole::where('member_role_id', $item['member']['join_member_role_id'])->value('member_role_name');
  821. }
  822. $item['member_info']['member_info_nickname'] = MemberService::getNickname(!empty($item['member_info']) ? $item['member_info']['member_info_nickname'] : '', (!empty($item['member']) ? $item['member']['member_mobile'] : ''));
  823. $item['member_info']['member_info_headimg'] = MemberService::getAvatarUrl(!empty($item['member_info']) && !empty($item['member_info']['member_info_headimg']) ? $item['member_info']['member_info_headimg'] : '');
  824. }
  825. $order->sheet_amount = number_format($sheetAmount, 2);
  826. $payDetails = PayDetail::where('join_pay_order_id', $order->order_groupby)
  827. ->whereJsonContains('join_pay_object_json->order_id', $orderId)
  828. ->where('pay_category', '<>', 'REFUND')
  829. ->where('pay_status', 'SUCCESS')
  830. ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json')
  831. ->get();
  832. if (count($payDetails) > 1) {
  833. $order->pay_category = 'CONSTITUTE';
  834. }
  835. $payDetail = !empty($payDetails) && count($payDetails) > 0 ? $payDetails[0] : [];
  836. if (!empty($payDetail) && !empty($payDetail->pay_extend_json)) {
  837. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  838. $order->cancel_times = $payExtendJson['cancel_times'] ?? '';
  839. // $payDetail->cancel_times = $payExtendJson['cancel_times'] ?? '';
  840. }
  841. if (!empty($payDetail) && !empty($payDetail->pay_prepayid)) {
  842. $categoryArray = explode('-', $payDetail->pay_prepayid);
  843. if (isset($categoryArray[1])) {
  844. $payDetail->pay_category = $categoryArray[1];
  845. } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) {
  846. $payDetail->pay_category = $categoryArray[0];
  847. }
  848. }
  849. $refund = OrderReturn::where('join_return_order_id', $orderId)
  850. ->select('order_return_status', 'order_return_apply_datetime', 'order_return_apply_json', 'order_return_accept_datetime', 'order_return_refund_json', 'order_return_extend_json')
  851. ->first();
  852. if (!empty($refund->order_return_refund_json)) {
  853. $returnRefundJson = json_decode($refund->order_return_refund_json, true);
  854. if (isset($returnRefundJson['user_id'])) {
  855. $userName = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name');
  856. $returnRefundJson['person'] = $userName;
  857. unset($returnRefundJson['user_id']);
  858. }
  859. $refund->order_return_refund_json = json_encode($returnRefundJson);
  860. }
  861. if (!empty($order->order_config_json)) {
  862. $orderConfigJson = json_decode($order->order_config_json, true);
  863. if (isset($orderConfigJson['reach'])) {
  864. $order->reach = $orderConfigJson['reach'];
  865. }
  866. if (isset($orderConfigJson['table'])) {
  867. $order->table = $orderConfigJson['table'];
  868. }
  869. if (isset($orderConfigJson['eat'])) {
  870. $order->eat = $orderConfigJson['eat'];
  871. }
  872. if (isset($orderConfigJson['express'])) {
  873. $order->express = $orderConfigJson['express'];
  874. }
  875. if (isset($orderConfigJson['premises'])) {
  876. $order->premises = $orderConfigJson['premises'];
  877. }
  878. if (isset($orderConfigJson['tableid'])) {
  879. $order->dept_table_id = $orderConfigJson['tableid'];
  880. }
  881. }
  882. if (!empty($order->order_extend_json)) {
  883. $orderExtendJson = json_decode($order->order_extend_json, true);
  884. $order->referee = $orderExtendJson['referee'] ?? '';
  885. if (isset($orderExtendJson['cancel_times'])) {
  886. $order->cancel_times = $orderExtendJson['cancel_times'];
  887. }
  888. if (isset($orderExtendJson['free_remark'])) {
  889. $order->free_remark = $orderExtendJson['free_remark'];
  890. }
  891. if (isset($orderExtendJson['dept_id'])) {
  892. $order->dept_id = $orderExtendJson['dept_id'];
  893. }
  894. }
  895. $order->premises = $order->premises ?? $premises;
  896. $data = [
  897. 'coupons' => $coupons,
  898. 'order' => $order,
  899. 'refund' => json_decode(json_encode($refund)),
  900. 'sheet' => json_decode(json_encode($orderSheet)),
  901. 'express' => json_decode(json_encode($express)),
  902. 'payDetail' => json_decode(json_encode($payDetail)),
  903. 'payDetails' => json_decode(json_encode($payDetails))
  904. ];
  905. return json_success('', $data);
  906. } catch (\Exception $e) {
  907. dump($e->getMessage(), $e->getLine());
  908. }
  909. }
  910. /**
  911. * @Desc 获取分期的订单
  912. * @Author Gorden
  913. * @Date 2024/9/10 15:14
  914. *
  915. * @param Request $request
  916. * @return Response
  917. */
  918. public function getPaidOrder(Request $request)
  919. {
  920. $memberId = $request->get('member_id');
  921. if (!$memberId) {
  922. return json_fail('参数异常');
  923. }
  924. $order = Order::where('join_order_member_id', $memberId)
  925. ->where('order_category', 'RECHARGE')
  926. ->where('order_status_system', 'PAYING')
  927. ->select('order_id', 'order_amount_total', 'order_amount_paid', 'order_amount_pay', 'order_extend_json')
  928. ->first();
  929. return json_success('', $order);
  930. }
  931. /**
  932. * @Desc 上传支付凭证
  933. * @Author Gorden
  934. * @Date 2024/9/12 10:14
  935. *
  936. * @param Request $request
  937. * @return Response
  938. */
  939. public function uploadTicket(Request $request)
  940. {
  941. $payId = $request->post('pay_id');
  942. $url = $request->post('url');
  943. if (!$payId || !$url) {
  944. return json_fail("参数异常");
  945. }
  946. $url = str_replace(getenv("STORAGE_DOMAIN"), '', $url);
  947. try {
  948. $payDetail = PayDetail::where('pay_id', $payId)->first();
  949. if (!$payDetail) {
  950. return json_fail('数据异常');
  951. }
  952. $extendJson = [];
  953. if (!empty($payDetail->pay_extend_json)) {
  954. $extendJson = json_decode($payDetail->pay_extend_json, true);
  955. }
  956. $extendJson['ticket'] = $url;
  957. $payDetail->pay_extend_json = json_encode($extendJson);
  958. dump($payDetail->save());
  959. return json_success('success');
  960. } catch (\Exception $e) {
  961. return json_fail("上传附件失败");
  962. }
  963. }
  964. /**
  965. * @Desc 查询订单状态
  966. * @Author Gorden
  967. * @Date 2024/8/19 11:55
  968. *
  969. * @param Request $request
  970. * @return Response|void
  971. */
  972. public function getOrderPayStatus(Request $request)
  973. {
  974. $groupId = $request->get('group_id');
  975. if (!$groupId) {
  976. return json_fail('参数异常');
  977. }
  978. $order = Order::where('order_groupby', $groupId)->first();
  979. if (empty($order)) {
  980. return json_fail('订单不存在');
  981. }
  982. $order = $order->toArray();
  983. $sheet = OrderSheet::where('join_sheet_order_id', $order['order_id'])
  984. ->select('order_sheet_id', 'join_sheet_goods_id', 'join_sheet_goods_sku_id')
  985. ->first();
  986. $payDetailType = PayDetail::where('join_pay_order_id', $groupId)->pluck('pay_prepayid')->toArray();
  987. try {
  988. Db::beginTransaction();
  989. $payStatus = 'N';
  990. $payAmount = 0;
  991. if (in_array('WXPAY', $payDetailType)) {
  992. $result = Pay::wechat(config('payment.wxpay'))->find($groupId, 'pos');
  993. $result = json_decode(json_encode($result), true);
  994. // $result = '{"return_code":"SUCCESS","return_msg":"OK","result_code":"SUCCESS","mch_id":"1680393367","appid":"wxc6274da7198e3eb4","openid":"o3JAn6Ii_bAlxS-jbNEC4WnPhdwM","is_subscribe":"N","trade_type":"MICROPAY","trade_state":"SUCCESS","bank_type":"OTHERS","total_fee":"50","fee_type":"CNY","cash_fee":"1000","cash_fee_type":"CNY","transaction_id":"4200067718202409250802875650","out_trade_no":"OD24092518408RV7","attach":[],"time_end":"20240925184009","trade_state_desc":"支付成功","nonce_str":"OeGOkjch4eaV5qIt","sign":"6DCB3BFC594EBC018A2BEE2C3DFEA4E3"}';
  995. // $result = json_decode($result, true);
  996. if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
  997. $payStatus = 'Y';
  998. $payAmount = ($result['total_fee'] / 100);
  999. $orderUpdateData['order_status_payment'] = 'SUCCESS';
  1000. $orderUpdateData['order_status_system'] = 'DONE';
  1001. $orderUpdateData['order_is_complete'] = 'Y';
  1002. $orderUpdateData['order_amount_pay'] = $payAmount;
  1003. // 主订单
  1004. Order::where('order_groupby', $groupId)->update($orderUpdateData);
  1005. // Sheet
  1006. OrderSheet::where('join_sheet_order_id', $order['order_id'])->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
  1007. // 支付记录
  1008. $payDetail = PayDetail::where('join_pay_order_id', $order['order_groupby'])->where('pay_prepayid', 'WXPAY')->first();
  1009. $payDetail->pay_status = 'SUCCESS';
  1010. $payDetail->pay_paytimes = date('Y-m-d H:i:s');
  1011. $payDetail->pay_json_response = json_encode($result);
  1012. $payDetail->save();
  1013. }
  1014. } else if (in_array('ALIPAY', $payDetailType)) {
  1015. $result = Pay::alipay(config('payment.alipay'))->find($groupId);
  1016. $result = json_decode(json_encode($result), true);
  1017. // $result = '{"code":"10000","msg":"Success","buyer_logon_id":"138******93","buyer_pay_amount":"5.87","fund_bill_list":[{"amount":"5.69","fund_channel":"ALIPAYACCOUNT"},{"amount":"0.18","fund_channel":"COUPON"},{"amount":"0.13","fund_channel":"DISCOUNT"}],"invoice_amount":"5.69","out_trade_no":"OD2409251550Q07A","point_amount":"0.00","receipt_amount":"0.01","send_pay_date":"2024-09-25 15:50:22","total_amount":"0.01","trade_no":"2024092523001439431419750998","trade_status":"TRADE_SUCCESS","buyer_open_id":"04309N2aVhSZz4cKwN_DN2DQa7ekM3z5n8kscQHsmIZOJsf"}';
  1018. // $result = json_decode($result, true);
  1019. if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
  1020. $payStatus = 'Y';
  1021. $payAmount = $result['total_amount'];
  1022. $orderUpdateData['order_status_payment'] = 'SUCCESS';
  1023. $orderUpdateData['order_status_system'] = 'DONE';
  1024. $orderUpdateData['order_is_complete'] = 'Y';
  1025. $orderUpdateData['order_amount_pay'] = $payAmount;
  1026. // 主订单
  1027. Order::where('order_groupby', $groupId)->update($orderUpdateData);
  1028. // Sheet
  1029. OrderSheet::where('join_sheet_order_id', $order['order_id'])->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
  1030. // 支付记录
  1031. $payDetail = PayDetail::where('join_pay_order_id', $order['order_groupby'])->where('pay_prepayid', 'WXPAY')->first();
  1032. $payDetail->pay_status = 'SUCCESS';
  1033. $payDetail->pay_paytimes = date('Y-m-d H:i:s');
  1034. $payDetail->pay_json_response = json_encode($result);
  1035. $payDetail->save();
  1036. }
  1037. }
  1038. if ($payStatus == 'Y') {
  1039. Db::commit();
  1040. $params['member_id'] = $order['join_order_member_id'];
  1041. $params['orderId'] = $order['order_id'];
  1042. $params['order_amount_pay'] = $payAmount;
  1043. $params['order_amount_total'] = $order['order_amount_total'];
  1044. $params['join_sheet_goods_id'] = $sheet->join_sheet_goods_id;
  1045. if (!empty($order['order_extend_json'])) {
  1046. $orderExtendJson = json_decode($order['order_extend_json'], true);
  1047. $params['dept_id'] = $orderExtendJson['dept_id'] ?? '';
  1048. $params['dept_name'] = $orderExtendJson['dept_name'] ?? '';
  1049. }
  1050. // 入收支明细表
  1051. $params['orderId'] = $order['order_id'];
  1052. $params['inout_category'] = '会员充值';
  1053. Event::dispatch('statistics.inout.in', $params);
  1054. // 充值回调
  1055. (new \app\admin\controller\notify\RechargeController)->disposePaySuccess($payDetail->pay_id);
  1056. return json_success('success');
  1057. }
  1058. Db::rollBack();
  1059. return json_fail('没有查询到记录');
  1060. } catch (BusinessException $e) {
  1061. Db::rollBack();
  1062. _syslog("支付轮询", '订单支付异常:' . $e->getMessage());
  1063. return json_fail($e->getMessage());
  1064. } catch (\Exception $e) {
  1065. dump($e->getMessage());
  1066. Db::rollBack();
  1067. return json_fail('查询失败');
  1068. }
  1069. }
  1070. }