RefundController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. $response = $this->refundToAlipay($payDetail0, $amount);
  344. } elseif ($payWay[0] == 'MONEY') {
  345. $prepayid = 'MONEY';
  346. $response = $this->refundToCash($payDetail0, $amount);
  347. } elseif (isset($payWay[1]) && $payWay[1] == 'CASH') {
  348. $prepayid = $order->join_order_member_id . '-CASH';
  349. $this->refundToCash($order->join_order_member_id, $amount);
  350. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  351. } elseif (isset($payWay[1]) && $payWay[1] == 'WELFARE') {
  352. $prepayid = $order->join_order_member_id . '-WELFARE';
  353. $this->refundToWelfare($order->join_order_member_id, $amount);
  354. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  355. } elseif (isset($payWay[1]) && $payWay[1] == 'VIP') {
  356. $prepayid = $order->join_order_member_id . '-VIP';
  357. $this->refundToVIP($order->join_order_member_id, $amount);
  358. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  359. } elseif (isset($payWay['1']) && $payWay[1] == 'CARD') {
  360. $prepayid = $order->join_order_member_id . '-CARD';
  361. $this->refundToCard($order->join_order_member_id, $amount);
  362. $response = ['order_id' => $order->order_id, 'member_id' => $order->join_order_member_id];
  363. } elseif ($payWay[0] == 'OFFLINE') {
  364. $prepayid = 'OFFLINE';
  365. $response = [];
  366. } elseif ($payWay[0] == 'OFFLINE_ALIPAY') {
  367. $prepayid = 'OFFLINE_ALIPAY';
  368. $response = [];
  369. } elseif ($payWay[0] == 'OFFLINE_WXPAY') {
  370. $prepayid = 'OFFLINE_WXPAY';
  371. $response = [];
  372. }
  373. }
  374. // payDetail 表记录
  375. $this->createReturnPayDetail($order, $refundPayIds, $prepayid, $amount, $response);
  376. Db::commit();
  377. return json_success('success');
  378. } catch (BusinessException $e) {
  379. Db::rollBack();
  380. dump($e->getMessage());
  381. return json_fail($e->getMessage());
  382. } catch (\Exception $e) {
  383. Db::rollBack();
  384. dump($e->getMessage());
  385. return json_fail("退款失败");
  386. }
  387. }
  388. /**
  389. * @Desc 退款到余额
  390. * @Author Gorden
  391. * @Date 2024/8/20 11:54
  392. *
  393. * @param $memberId
  394. * @param $amount
  395. * @return void
  396. * @throws BusinessException
  397. */
  398. private function refundToCash($memberId, $amount)
  399. {
  400. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  401. if (!$account) {
  402. throw new BusinessException("余额账户异常");
  403. }
  404. $account->member_account_surplus = $account->member_account_surplus + $amount;
  405. $account->save();
  406. }
  407. /**
  408. * @Desc 退款到福利账户
  409. * @Author Gorden
  410. * @Date 2024/9/20 17:09
  411. *
  412. * @param $memberId
  413. * @param $amount
  414. * @return void
  415. * @throws BusinessException
  416. */
  417. private function refundToWelfare($memberId, $amount)
  418. {
  419. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'WELFARE')->first();
  420. if (!$account) {
  421. throw new BusinessException("余额账户异常");
  422. }
  423. $account->member_account_surplus = $account->member_account_surplus + $amount;
  424. $account->save();
  425. }
  426. /**
  427. * @Desc 退款到VIP账户
  428. * @Author Gorden
  429. * @Date 2024/9/20 17:12
  430. *
  431. * @param $memberId
  432. * @param $amount
  433. * @return void
  434. * @throws BusinessException
  435. */
  436. private function refundToVIP($memberId, $amount)
  437. {
  438. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'VIP')->first();
  439. if (!$account) {
  440. throw new BusinessException("余额账户异常");
  441. }
  442. $account->member_account_surplus = $account->member_account_surplus + $amount;
  443. $account->save();
  444. }
  445. /**
  446. * @Desc 退款到储蓄卡
  447. * @Author Gorden
  448. * @Date 2024/8/20 14:47
  449. *
  450. * @param $memberId
  451. * @param $amount
  452. * @return void
  453. * @throws BusinessException
  454. */
  455. private function refundToCard($cardNbr, $amount)
  456. {
  457. $account = MemberAccount::where('member_account_nbr', $cardNbr)->where('member_account_classify', 'CARD')->first();
  458. if (!$account) {
  459. throw new BusinessException("储值卡账户异常");
  460. }
  461. $account->member_account_surplus = $account->member_account_surplus + $amount;
  462. $account->save();
  463. }
  464. /**
  465. * @Desc
  466. * @Author Gorden
  467. * @Date 2024/8/20 14:17
  468. *
  469. * @param $params
  470. * @param $returnId
  471. * @param $amount
  472. * @return mixed
  473. * @throws BusinessException
  474. * @throws \Yansongda\Pay\Exceptions\GatewayException
  475. * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
  476. * @throws \Yansongda\Pay\Exceptions\InvalidSignException
  477. */
  478. private function refundToWx($params, $returnId, $amount)
  479. {
  480. try {
  481. $data = [
  482. 'type' => 'app',
  483. 'out_trade_no' => $params['join_pay_order_id'],
  484. 'out_refund_no' => $returnId,
  485. 'total_fee' => $params['pay_amount'] * 100,
  486. 'refund_fee' => $amount * 100,
  487. 'refund_desc' => '退款',
  488. ];
  489. $res = Pay::wechat(config('payment.wxpay'))->refund($data);
  490. $resArray = json_decode($res, true);
  491. if (!$resArray['result_code'] == 'SUCCESS' || !$resArray['return_code'] == 'SUCCESS') {
  492. Log::channel('pay')->error('WXPAY_REFUND_FAIL', $resArray);
  493. throw new BusinessException("退款失败");
  494. }
  495. return $resArray;
  496. } catch (\Exception $e) {
  497. throw new BusinessException("退款失败");
  498. }
  499. }
  500. /**
  501. * @Desc 支付宝退款
  502. * @Author Gorden
  503. * @Date 2024/8/20 14:40
  504. *
  505. * @param $params
  506. * @param $amount
  507. * @return mixed
  508. * @throws BusinessException
  509. */
  510. private function refundToAlipay($params, $amount)
  511. {
  512. $data = [
  513. 'out_trade_no' => $params['join_pay_order_id'],
  514. 'refund_amount' => $amount,
  515. ];
  516. try {
  517. $res = Pay::alipay(config('payment.alipay'))->refund($data);
  518. $resArray = json_decode($res, true);
  519. if ($resArray['fund_change'] != 'Y' || $resArray['msg'] != 'Success') {
  520. Log::channel('pay')->error('ALIPAY_REFUND_FAIL', $resArray);
  521. throw new BusinessException("退款失败");
  522. }
  523. return $resArray;
  524. } catch (\Exception $e) {
  525. throw new BusinessException("退款失败");
  526. }
  527. }
  528. /**
  529. * @Desc 更新主订单
  530. * @Author Gorden
  531. * @Date 2024/8/20 14:56
  532. *
  533. * @param $order
  534. * @param $amount
  535. * @param $remark
  536. * @return void
  537. * @throws BusinessException
  538. */
  539. private function updateMainOrderByRefund($order, $amount, $remark)
  540. {
  541. $orderDiscountJson = [];
  542. if (!empty($order->order_discount_json)) {
  543. $orderDiscountJson = json_decode($order->order_discount_json, true);
  544. }
  545. try {
  546. $orderDiscountJson[date('YH:i:s H:i:s')] = [
  547. 'coupon_id' => null,
  548. 'coupon_value' => $amount,
  549. 'coupon_classify' => '退款',
  550. 'coupon_detail_id' => [$remark]
  551. ];
  552. $order->order_discount_json = json_encode($orderDiscountJson, JSON_UNESCAPED_UNICODE);
  553. $order->order_is_complete = 'R';
  554. // $order->order_amount_pay = $order->order_amount_pay - $amount;
  555. $order->save();
  556. } catch (\Exception $e) {
  557. throw new BusinessException("退款失败");
  558. }
  559. }
  560. /**
  561. * @Desc
  562. * @Author Gorden
  563. * @Date 2024/8/20 13:51
  564. *
  565. * @param $order
  566. * @return int
  567. * @throws BusinessException
  568. */
  569. private function createReturnRecord($order, $amount, $remark)
  570. {
  571. try {
  572. return OrderReturn::insertGetId([
  573. 'join_order_return_user_id' => JwtToken::getCurrentId(),
  574. 'join_return_member_id' => $order->join_order_member_id,
  575. 'join_return_order_id' => $order->order_id,
  576. 'order_return_status' => 'DONE',
  577. 'order_return_category' => '退款',
  578. 'order_return_apply_datetime' => date('Y-m-d H:i:s'),
  579. 'order_return_apply_json' => json_encode(['reason' => '后台自定义退款']),
  580. 'order_return_accept_datetime' => date('Y-m-d H:i:s'),
  581. 'order_return_refund_json' => json_encode(['amount' => $amount, 'user_id' => JwtToken::getCurrentId(), 'datetime' => date('Y-m-d H:i:s'), 'remark' => $remark ?? '']),
  582. 'order_return_addtimes' => time()
  583. ]);
  584. } catch (\Exception $e) {
  585. throw new BusinessException("创建退款信息失败");
  586. }
  587. }
  588. private function createReturnPayDetail($order, $refundPayIds, $prepayid, $amount, $response = [])
  589. {
  590. try {
  591. return PayDetail::insert([
  592. 'join_pay_member_id' => $order->join_order_member_id,
  593. 'join_pay_order_id' => $order->order_groupby,
  594. 'pay_status' => 'SUCCESS',
  595. 'pay_category' => 'REFUND',
  596. 'pay_amount' => $amount,
  597. 'pay_paytimes' => date('Y-m-d H:i:s'),
  598. 'pay_prepayid' => $prepayid,
  599. 'join_pay_object_json' => json_encode(['order_id' => $order->order_id, 'refund_pay_ids' => $refundPayIds]),
  600. 'pay_json_request' => json_encode(['order_id' => $order->order_id]),
  601. 'pay_json_response' => json_encode($response),
  602. 'pay_addtimes' => time()
  603. ]);
  604. } catch (\Exception $e) {
  605. }
  606. }
  607. }