RefundController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\validate\order\ReturnValidate;
  4. use app\controller\Curd;
  5. use app\model\CouponDetail;
  6. use app\model\GoodsComponent;
  7. use app\model\MemberAccount;
  8. use app\model\Order;
  9. use app\model\OrderExpress;
  10. use app\model\OrderReturn;
  11. use app\model\OrderSheet;
  12. use app\model\PayDetail;
  13. use app\model\SysUser;
  14. use Payment\Common\PayException;
  15. use support\Db;
  16. use support\exception\BusinessException;
  17. use support\Log;
  18. use support\Request;
  19. use support\Response;
  20. use Tinywan\Jwt\JwtToken;
  21. use Yansongda\Pay\Pay;
  22. class RefundController extends Curd
  23. {
  24. public function __construct()
  25. {
  26. $this->model = new OrderReturn();
  27. $this->validate = true;
  28. $this->validateClass = new ReturnValidate();
  29. }
  30. public function select(Request $request): Response
  31. {
  32. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  33. $order = $request->get('order', 'desc');
  34. $type = $request->get('type', '');
  35. $field = $field ?? 'order_return_addtimes';
  36. $where['order_return_category'] = '退款';
  37. if (!empty($where['order_return_addtimes'])) {
  38. $where['order_return_addtimes'][0] = strtotime($where['order_return_addtimes'][0]);
  39. $where['order_return_addtimes'][1] = strtotime($where['order_return_addtimes'][1]);
  40. }
  41. if ($type == 'today') {
  42. $where['order_return_addtimes'] = [
  43. strtotime(date('Y-m-d') . ' 00:00:00'),
  44. strtotime(date('Y-m-d') . ' 23:59:59')
  45. ];
  46. }
  47. $query = $this->doSelect($where, $field, $order);
  48. return $this->doFormat($query, $format, $limit);
  49. }
  50. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  51. {
  52. $model = $this->model->with([
  53. 'member' => function ($query) {
  54. $query->select('member_id', 'member_mobile');
  55. },
  56. 'cert' => function ($query) {
  57. $query->select('join_cert_member_id', 'member_cert_name');
  58. },
  59. 'order' => function ($query) {
  60. $query->select('order_id', 'order_name');
  61. }
  62. ]);
  63. foreach ($where as $column => $value) {
  64. if (is_array($value)) {
  65. if ($value[0] === 'like' || $value[0] === 'not like') {
  66. $model = $model->where($column, $value[0], "%$value[1]%");
  67. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  68. $model = $model->where($column, $value[0], $value[1]);
  69. } elseif ($value[0] == 'in' && !empty($value[1])) {
  70. $valArr = $value[1];
  71. if (is_string($value[1])) {
  72. $valArr = explode(",", trim($value[1]));
  73. }
  74. $model = $model->whereIn($column, $valArr);
  75. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  76. $valArr = $value[1];
  77. if (is_string($value[1])) {
  78. $valArr = explode(",", trim($value[1]));
  79. }
  80. $model = $model->whereNotIn($column, $valArr);
  81. } elseif ($value[0] == 'null') {
  82. $model = $model->whereNull($column);
  83. } elseif ($value[0] == 'not null') {
  84. $model = $model->whereNotNull($column);
  85. } elseif ($value[0] !== '' || $value[1] !== '') {
  86. $model = $model->whereBetween($column, $value);
  87. }
  88. } else {
  89. $model = $model->where($column, $value);
  90. }
  91. }
  92. if ($field) {
  93. $model = $model->orderBy($field, $order);
  94. }
  95. return $model;
  96. }
  97. public function afterQuery($items)
  98. {
  99. foreach ($items as &$item) {
  100. if (!empty($item['order'])) {
  101. $orderSheet = OrderSheet::with([
  102. 'goods' => function ($query) {
  103. $query->select('goods_id', 'goods_name');
  104. }
  105. ])->select('join_sheet_order_id', 'join_sheet_goods_id')
  106. ->where('join_sheet_order_id', $item['order']->order_id)
  107. ->get()
  108. ->toArray();
  109. $goodsName = !empty($orderSheet) ? $orderSheet[0]['goods']['goods_name'] : '';
  110. if (count($orderSheet) > 1) {
  111. $goodsName .= ' 等';
  112. }
  113. $item['goods_name'] = $goodsName;
  114. }
  115. if (!empty($item['order_return_apply_json']) && is_json($item['order_return_apply_json'])) {
  116. $json = json_decode($item['order_return_apply_json'], true);
  117. $item['order_return_apply_json'] = $json['apply'] ?? '';
  118. }
  119. if (!empty($item['order_return_recharge_json']) && is_json($item['order_return_recharge_json'])) {
  120. $json = json_decode($item['order_return_recharge_json'], true);
  121. $item['order_return_recharge_json'] = $json['change'] ?? '';
  122. }
  123. if (!empty($item['order_return_refund_json']) && is_json($item['order_return_refund_json'])) {
  124. $returnRefundJson = json_decode($item['order_return_refund_json'], true);
  125. if (!empty($returnRefundJson['user_id'])) {
  126. $item['userName'] = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name');
  127. }
  128. }
  129. }
  130. return $items;
  131. }
  132. protected function updateInput(Request $request): array
  133. {
  134. $primary_key = $this->model->getKeyName();
  135. $id = $request->post($primary_key);
  136. $data = $this->inputFilter($request->post());
  137. $model = $this->model->find($id);
  138. if (!$model) {
  139. throw new BusinessException('记录不存在', 2);
  140. }
  141. if (empty($model->order_return_accept_datetimes) && $model->order_return_status == 'PENDING' && $data['order_return_status'] == 'DOING') {
  142. $data['order_return_accept_datetimes'] = date('Y-m-d H:i:s');
  143. }
  144. if (!empty($data['order_return_apply_json'])) {
  145. $data['order_return_apply_json'] = json_encode(['apply' => $data['order_return_apply_json']]);
  146. }
  147. if (!empty($data['order_return_recharge_json'])) {
  148. $data['order_return_recharge_json'] = json_encode(['change' => $data['order_return_recharge_json']]);
  149. }
  150. unset($data[$primary_key]);
  151. return [$id, $data];
  152. }
  153. /**
  154. * @Desc 订单商品详情
  155. * @Author Gorden
  156. * @Date 2024/3/29 8:50
  157. *
  158. * @param Request $request
  159. * @return Response
  160. */
  161. public function sheet(Request $request)
  162. {
  163. $orderId = $request->get('order_id');
  164. $orderSheet = OrderSheet::with([
  165. 'member' => function ($query) {
  166. $query->select('member_id', 'member_mobile');
  167. },
  168. 'goods' => function ($query) {
  169. $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify');
  170. },
  171. 'sku' => function ($query) {
  172. $query->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price');
  173. },
  174. 'memberInfo',
  175. 'cert',
  176. 'refund'
  177. ])->where('join_sheet_order_id', $orderId)
  178. ->get()
  179. ->toArray();
  180. $member = [];
  181. $cert = [];
  182. $memberInfo = [];
  183. foreach ($orderSheet as &$item) {
  184. if (isset($item['member']) && empty($member)) {
  185. $member = $item['member'];
  186. }
  187. if (isset($item['cert']) && empty($cert)) {
  188. $cert = $item['cert'];
  189. }
  190. if (isset($item['member_info']) && empty($memberInfo)) {
  191. $memberInfo = $item['member_info'];
  192. }
  193. $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover'];
  194. if (!empty($item['goods']) && $item['goods']['goods_classify'] == 'PACKAGE') {
  195. $components = GoodsComponent::with('goods')
  196. ->where('join_component_master_goods_id', $item['goods']['goods_id'])
  197. ->select('join_component_master_goods_id', 'join_component_goods_id', 'goods_component_price',
  198. 'goods_component_price')
  199. ->get()
  200. ->toArray();
  201. $goodsArr = [];
  202. foreach ($components as $component) {
  203. $configJson = !empty($component['goods_component_config_json']) ? json_decode($component['goods_component_config_json'], true) : [];
  204. if (!empty($component['goods'])) {
  205. $goodsArr[] = [
  206. 'goods_name' => $component['goods']['goods_name'],
  207. 'goods_cover' => getenv('STORAGE_DOMAIN') . $component['goods']['goods_cover'],
  208. 'nbr' => $configJson['nbr'] ?? 0,
  209. ];
  210. }
  211. }
  212. $item['goods']['components'] = $goodsArr;
  213. }
  214. if (!empty($item['sku'])) {
  215. if (!empty($item['sku']['goods_sku_specs_json'])) {
  216. $specsJson = json_decode($item['sku']['goods_sku_specs_json'], true);
  217. $skuName = '';
  218. foreach ($specsJson as $specsKey => $skuSpecs) {
  219. if (is_array($skuSpecs)) {
  220. $skuName = $skuName . ' ' . implode(' ', $skuSpecs) . ';';
  221. } else {
  222. $skuName = $skuName . ' ' . $skuSpecs . ';';
  223. }
  224. }
  225. $item['sku']['goods_sku_title'] = rtrim($skuName, ';');
  226. }
  227. }
  228. if (!empty($item['refund'])) {
  229. if (!empty($item['refund']['order_return_apply_json']) && is_json($item['refund']['order_return_apply_json'])) {
  230. $json = json_decode($item['refund']['order_return_apply_json'], true);
  231. $item['refund']['order_return_apply_json'] = $json['reason'] ?? '';
  232. }
  233. if (!empty($item['refund']['order_return_recharge_json']) && is_json($item['refund']['order_return_recharge_json'])) {
  234. $json = json_decode($item['refund']['order_return_recharge_json'], true);
  235. $item['refund']['order_return_recharge_json'] = $json['change'] ?? '';
  236. }
  237. }
  238. }
  239. $order = Order::where('order_id', $orderId)->first();
  240. $return = OrderReturn::where('join_return_order_id', $orderId)->first();
  241. if (!empty($return->order_return_refund_json)) {
  242. $returnRefundJson = json_decode($return->order_return_refund_json, true);
  243. if (!empty($returnRefundJson['user_id'])) {
  244. $return->userName = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name');
  245. }
  246. }
  247. $express = OrderExpress::where('join_express_order_id', $orderId)->first();
  248. $data = [
  249. 'member' => $member,
  250. 'cert' => $cert,
  251. 'member_info' => $memberInfo,
  252. 'order' => $order,
  253. 'refund' => $return,
  254. 'sheet' => $orderSheet,
  255. 'express' => $express
  256. ];
  257. return json_success('', $data);
  258. }
  259. public function customRefund(Request $request)
  260. {
  261. $orderId = $request->post('order_id', '');
  262. $amount = $request->post('refund_amount', 0);
  263. $password = $request->post('refund_password', '');
  264. $remark = $request->post('refund_remark', '');
  265. if (!$orderId || !$amount || !$password) {
  266. return json_fail('参数异常');
  267. }
  268. if ($password != '123456') {
  269. return json_fail('支付密码错误');
  270. }
  271. $order = Order::where('order_id', $orderId)->where('order_status_payment', 'SUCCESS')->first();
  272. if (!$order) {
  273. return json_fail("订单异常");
  274. }
  275. if ($amount > $order->order_amount_pay) {
  276. return json_fail("退款金额不能超过支付金额");
  277. }
  278. $payDetail = PayDetail::where('join_pay_order_id', $order->order_groupby)
  279. ->where('pay_status', 'SUCCESS')
  280. ->whereIn('pay_category', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'DISHES', 'VIP', 'PACKAGE'])
  281. ->get()
  282. ->toArray();
  283. if (empty($payDetail)) {
  284. return json_fail("支付状态异常");
  285. }
  286. $refundPayIds = array_column($payDetail, 'pay_id');
  287. $response = [];
  288. Db::beginTransaction();
  289. try {
  290. // 全额退款,检查优惠券,恢复可使用
  291. if (sprintf("%.2f", $amount) == sprintf("%.2f", $order->order_amount_pay)) {
  292. if (!empty($order->order_discount_json)) {
  293. $discountJson = json_decode($order->order_discount_json, true);
  294. foreach ($discountJson as $item) {
  295. if (empty($item['coupon_id'])) {
  296. continue;
  297. }
  298. // 是否有其他订单一起使用优惠券
  299. if (!Order::where('order_groupby', $order->order_groupby)->where('order_id', '<>', $order->order_id)->where('order_is_complete', '<>', 'R')->exists()) {
  300. foreach ($item['coupon_detail_id'] as $detailId) {
  301. CouponDetail::where('coupon_detail_id', $detailId)->update([
  302. 'coupon_detail_status' => 'ACTIVED',
  303. 'coupon_detail_used_datetime' => ''
  304. ]);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. // 主订单,退款作为优惠入库
  311. $this->updateMainOrderByRefund($order, $amount, $remark);
  312. // return 表记录
  313. $return = OrderReturn::where('join_return_order_id', $orderId)->first();
  314. if (empty($return)) {
  315. $returnId = $this->createReturnRecord($order, $amount, $remark);
  316. } else {
  317. $returnId = $return->orders_return_id;
  318. //['amount' => $amount, 'user_id' => JwtToken::getCurrentId(), 'datetime' => date('Y-m-d H:i:s'), 'remark' => $remark ?? '']
  319. OrderReturn::where('join_return_order_id', $orderId)->update([
  320. 'order_return_status' => 'DONE',
  321. 'order_return_refund_json' => json_encode([
  322. 'amount' => $amount,
  323. 'user_id' => JwtToken::getCurrentId(),
  324. 'datetime' => date('Y-m-d H:i:s'),
  325. 'remark' => $remark ?? ''
  326. ])
  327. ]);
  328. }
  329. // 组合支付,退到余额账户
  330. $prepayid = '';
  331. if (count($payDetail) > 1) {
  332. $prepayid = $order->join_order_member_id . '-CASH';
  333. $this->refundToCash($order->join_order_member_id, $amount);
  334. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  335. } else if (count($payDetail) == 1) {
  336. $payDetail0 = $payDetail[0];
  337. $payWay = explode('-', $payDetail0['pay_prepayid']);
  338. if ($payWay[0] == 'WXPAY') {
  339. $prepayid = 'WXPAY';
  340. $response = $this->refundToWx($payDetail0, $returnId, $amount);
  341. } elseif ($payWay[0] == 'ALIPAY') {
  342. $prepayid = 'ALIPAY';
  343. $payDetail0['order_id'] = $order->order_id;
  344. $response = $this->refundToAlipay($payDetail0, $amount);
  345. } elseif ($payWay[0] == 'MONEY') {
  346. $prepayid = 'MONEY';
  347. $response = $this->refundToCash($payDetail0, $amount);
  348. } elseif (isset($payWay[1]) && $payWay[1] == 'CASH') {
  349. $prepayid = $order->join_order_member_id . '-CASH';
  350. $this->refundToCash($order->join_order_member_id, $amount);
  351. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  352. } elseif (isset($payWay[1]) && $payWay[1] == 'WELFARE') {
  353. $prepayid = $order->join_order_member_id . '-WELFARE';
  354. $this->refundToWelfare($order->join_order_member_id, $amount);
  355. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  356. } elseif (isset($payWay[1]) && $payWay[1] == 'VIP') {
  357. $prepayid = $order->join_order_member_id . '-VIP';
  358. $this->refundToVIP($order->join_order_member_id, $amount);
  359. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  360. } elseif (isset($payWay['1']) && $payWay[1] == 'CARD') {
  361. $prepayid = $order->join_order_member_id . '-CARD';
  362. $this->refundToCard($order->join_order_member_id, $amount);
  363. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  364. } elseif ($payWay[0] == 'OFFLINE') {
  365. $prepayid = 'OFFLINE';
  366. $response = [];
  367. } elseif ($payWay[0] == 'OFFLINE_ALIPAY') {
  368. $prepayid = 'OFFLINE_ALIPAY';
  369. $response = [];
  370. } elseif ($payWay[0] == 'OFFLINE_WXPAY') {
  371. $prepayid = 'OFFLINE_WXPAY';
  372. $response = [];
  373. }
  374. }
  375. // payDetail 表记录
  376. $this->createReturnPayDetail($order, $refundPayIds, $prepayid, $amount, $response);
  377. Db::commit();
  378. return json_success('success');
  379. } catch (BusinessException $e) {
  380. Db::rollBack();
  381. Log::info("退款失败", ['msg' => $e->getMessage()]);
  382. return json_fail($e->getMessage());
  383. } catch (\Exception $e) {
  384. Db::rollBack();
  385. Log::info("退款失败", ['msg' => $e->getMessage()]);
  386. return json_fail("退款失败");
  387. }
  388. }
  389. /**
  390. * @Desc 退款到余额
  391. * @Author Gorden
  392. * @Date 2024/8/20 11:54
  393. *
  394. * @param $memberId
  395. * @param $amount
  396. * @return void
  397. * @throws BusinessException
  398. */
  399. private function refundToCash($memberId, $amount)
  400. {
  401. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  402. if (!$account) {
  403. throw new BusinessException("余额账户异常");
  404. }
  405. $account->member_account_surplus = $account->member_account_surplus + $amount;
  406. $account->save();
  407. }
  408. /**
  409. * @Desc 退款到福利账户
  410. * @Author Gorden
  411. * @Date 2024/9/20 17:09
  412. *
  413. * @param $memberId
  414. * @param $amount
  415. * @return void
  416. * @throws BusinessException
  417. */
  418. private function refundToWelfare($memberId, $amount)
  419. {
  420. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'WELFARE')->first();
  421. if (!$account) {
  422. throw new BusinessException("余额账户异常");
  423. }
  424. $account->member_account_surplus = $account->member_account_surplus + $amount;
  425. $account->save();
  426. }
  427. /**
  428. * @Desc 退款到VIP账户
  429. * @Author Gorden
  430. * @Date 2024/9/20 17:12
  431. *
  432. * @param $memberId
  433. * @param $amount
  434. * @return void
  435. * @throws BusinessException
  436. */
  437. private function refundToVIP($memberId, $amount)
  438. {
  439. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'VIP')->first();
  440. if (!$account) {
  441. throw new BusinessException("余额账户异常");
  442. }
  443. $account->member_account_surplus = $account->member_account_surplus + $amount;
  444. $account->save();
  445. }
  446. /**
  447. * @Desc 退款到储蓄卡
  448. * @Author Gorden
  449. * @Date 2024/8/20 14:47
  450. *
  451. * @param $memberId
  452. * @param $amount
  453. * @return void
  454. * @throws BusinessException
  455. */
  456. private function refundToCard($cardNbr, $amount)
  457. {
  458. $account = MemberAccount::where('member_account_nbr', $cardNbr)->where('member_account_classify', 'CARD')->first();
  459. if (!$account) {
  460. throw new BusinessException("储值卡账户异常");
  461. }
  462. $account->member_account_surplus = $account->member_account_surplus + $amount;
  463. $account->save();
  464. }
  465. /**
  466. * @Desc
  467. * @Author Gorden
  468. * @Date 2024/8/20 14:17
  469. *
  470. * @param $params
  471. * @param $returnId
  472. * @param $amount
  473. * @return mixed
  474. * @throws BusinessException
  475. * @throws \Yansongda\Pay\Exceptions\GatewayException
  476. * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
  477. * @throws \Yansongda\Pay\Exceptions\InvalidSignException
  478. */
  479. private function refundToWx($params, $returnId, $amount)
  480. {
  481. try {
  482. $data = [
  483. 'type' => 'app',
  484. 'out_trade_no' => $params['join_pay_order_id'],
  485. 'out_refund_no' => $returnId,
  486. 'total_fee' => $params['pay_amount'] * 100,
  487. 'refund_fee' => $amount * 100,
  488. 'refund_desc' => '退款',
  489. ];
  490. $res = Pay::wechat(config('payment.wxpay'))->refund($data);
  491. $resArray = json_decode($res, true);
  492. if (!$resArray['result_code'] == 'SUCCESS' || !$resArray['return_code'] == 'SUCCESS') {
  493. Log::channel('pay')->error('WXPAY_REFUND_FAIL', $resArray);
  494. throw new BusinessException("退款失败");
  495. }
  496. return $resArray;
  497. } catch (\Exception $e) {
  498. throw new BusinessException("退款失败");
  499. }
  500. }
  501. /**
  502. * @Desc 支付宝退款
  503. * @Author Gorden
  504. * @Date 2024/8/20 14:40
  505. *
  506. * @param $params
  507. * @param $amount
  508. * @return mixed
  509. * @throws BusinessException
  510. */
  511. private function refundToAlipay($params, $amount)
  512. {
  513. $data = [
  514. 'out_trade_no' => $params['join_pay_order_id'],
  515. 'refund_amount' => $amount,
  516. 'out_request_no' => $params['order_id']
  517. ];
  518. Log::error("支付宝退款参数", $data);
  519. try {
  520. $res = Pay::alipay(config('payment.alipay'))->refund($data);
  521. $resArray = json_decode($res, true);
  522. if ($resArray['fund_change'] != 'Y' || $resArray['msg'] != 'Success') {
  523. Log::channel('pay')->error('ALIPAY_REFUND_FAIL', $resArray);
  524. throw new BusinessException("退款失败");
  525. }
  526. return $resArray;
  527. } catch (\Exception $e) {
  528. Log::error("支付宝退款失败", ['msg' => $e->getMessage()]);
  529. throw new BusinessException("退款失败");
  530. }
  531. }
  532. /**
  533. * @Desc 更新主订单
  534. * @Author Gorden
  535. * @Date 2024/8/20 14:56
  536. *
  537. * @param $order
  538. * @param $amount
  539. * @param $remark
  540. * @return void
  541. * @throws BusinessException
  542. */
  543. private function updateMainOrderByRefund($order, $amount, $remark)
  544. {
  545. $orderDiscountJson = [];
  546. if (!empty($order->order_discount_json)) {
  547. $orderDiscountJson = json_decode($order->order_discount_json, true);
  548. }
  549. try {
  550. $orderDiscountJson[date('Y-m-d H:i:s')] = [
  551. 'coupon_id' => null,
  552. 'coupon_value' => $amount,
  553. 'coupon_classify' => '退款',
  554. 'coupon_detail_id' => [$remark]
  555. ];
  556. $order->order_discount_json = json_encode($orderDiscountJson, JSON_UNESCAPED_UNICODE);
  557. $order->order_is_complete = 'R';
  558. $order->order_category = 'RETURN';
  559. // $order->order_amount_pay = $order->order_amount_pay - $amount;
  560. $order->save();
  561. } catch (\Exception $e) {
  562. throw new BusinessException("退款失败");
  563. }
  564. }
  565. /**
  566. * @Desc
  567. * @Author Gorden
  568. * @Date 2024/8/20 13:51
  569. *
  570. * @param $order
  571. * @return int
  572. * @throws BusinessException
  573. */
  574. private function createReturnRecord($order, $amount, $remark)
  575. {
  576. try {
  577. return OrderReturn::insertGetId([
  578. 'join_order_return_user_id' => JwtToken::getCurrentId(),
  579. 'join_return_member_id' => $order->join_order_member_id,
  580. 'join_return_order_id' => $order->order_id,
  581. 'order_return_status' => 'DONE',
  582. 'order_return_category' => '退款',
  583. 'order_return_apply_datetime' => date('Y-m-d H:i:s'),
  584. 'order_return_apply_json' => json_encode(['reason' => '后台自定义退款']),
  585. 'order_return_accept_datetime' => date('Y-m-d H:i:s'),
  586. 'order_return_refund_json' => json_encode(['amount' => $amount, 'user_id' => JwtToken::getCurrentId(), 'datetime' => date('Y-m-d H:i:s'), 'remark' => $remark ?? '']),
  587. 'order_return_addtimes' => time()
  588. ]);
  589. } catch (\Exception $e) {
  590. throw new BusinessException("创建退款信息失败");
  591. }
  592. }
  593. private function createReturnPayDetail($order, $refundPayIds, $prepayid, $amount, $response = [])
  594. {
  595. try {
  596. return PayDetail::insert([
  597. 'join_pay_member_id' => $order->join_order_member_id,
  598. 'join_pay_order_id' => $order->order_groupby,
  599. 'pay_status' => 'SUCCESS',
  600. 'pay_category' => 'REFUND',
  601. 'pay_amount' => $amount,
  602. 'pay_paytimes' => date('Y-m-d H:i:s'),
  603. 'pay_prepayid' => $prepayid,
  604. 'join_pay_object_json' => json_encode(['order_id' => $order->order_id, 'refund_pay_ids' => $refundPayIds]),
  605. 'pay_json_request' => json_encode(['order_id' => $order->order_id]),
  606. 'pay_json_response' => json_encode($response),
  607. 'pay_addtimes' => time()
  608. ]);
  609. } catch (\Exception $e) {
  610. }
  611. }
  612. }