DepositOrderController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/19
  6. * Time: 2:11 PM
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Http\Requests\AreaIdRequest;
  10. use App\Jobs\CheckRedOrderJob;
  11. use App\Jobs\CloseOrderJob;
  12. use App\Jobs\DepositRefundJob;
  13. use App\Models\AreaSetting;
  14. use App\Models\DepositOrder;
  15. use App\Models\RefundLog;
  16. use App\Models\User;
  17. use App\Repositories\AreaSettingRepository;
  18. use App\Repositories\DepositOrderRepository;
  19. use App\Repositories\OrderRepository;
  20. use App\Repositories\PunishmentOrderRepository;
  21. use App\Repositories\RefundLogRepository;
  22. use App\Repositories\RentOrderRepository;
  23. use App\Repositories\UserRepository;
  24. use Carbon\Carbon;
  25. use Dingo\Api\Http\Request;
  26. use Illuminate\Support\Facades\Log;
  27. use function EasyWeChat\Kernel\Support\generate_sign;
  28. use Illuminate\Support\Facades\Cache;
  29. use Illuminate\Support\Facades\DB;
  30. /**
  31. * 押金管理模块
  32. * Class DepositOrderController
  33. * @package App\Http\Controllers\V1
  34. */
  35. class DepositOrderController extends BaseController
  36. {
  37. /**
  38. * 支付押金
  39. * @param Request $request
  40. * User: Mead
  41. */
  42. public function store(AreaIdRequest $request, DepositOrderRepository $depositOrderRepository, RefundLogRepository $refundLogRepository, AreaSettingRepository $areaSettingRepository)
  43. {
  44. try {
  45. $area_id = $request->get('area_id');
  46. $setting = $areaSettingRepository->byAreaId($area_id);
  47. // 判断区域是否需要缴纳押金
  48. if ($setting['is_deposit'] == AreaSetting::DEPOSIT_NO) {
  49. return $this->errorNoValidation('此区域暂时无需缴纳押金');
  50. }
  51. $user_id = $this->user->id;
  52. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($user_id);
  53. if (!$result['status']) {
  54. if ($result['code'] === 'REFUNDNOTEXIST') {
  55. //发起退款
  56. $payment = app('wechat.payment'); // 微信支付
  57. $refund = $refundLogRepository->byUserIdUserRefundNoModel($user_id);
  58. $order = $depositOrderRepository->byIdGetModel($refund->deposit_id);
  59. $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  60. // 可在此处传入其他参数,详细参数见微信支付文档
  61. 'refund_desc' => '用户申请退回押金',
  62. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify',
  63. ]);
  64. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  65. // 判断是否有免押金卡
  66. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  67. if (Carbon::now()->gte($deposit_expire_time)) {
  68. // 免押金卡已经过期
  69. User::where('id', $this->user->id)->update([
  70. 'deposit_money' => 0,
  71. 'is_deposit' => User::DEPOSIT_NO,
  72. 'deposit_type' => User::DEPOSIT_TYPE_NO
  73. ]);
  74. } else {
  75. // 免押金卡未过期
  76. User::where('id', $this->user->id)->update([
  77. 'deposit_money' => 0,
  78. 'is_deposit' => User::DEPOSIT_OK,
  79. 'deposit_type' => User::DEPOSIT_CARD
  80. ]);
  81. }
  82. } else {
  83. $refund->result = $result['err_code_des'];
  84. $refund->save();
  85. return $this->errorNoValidation($refund->result);
  86. }
  87. } else {
  88. return $this->errorNoValidation($result['msg']);
  89. }
  90. }
  91. $cache_key = "DEPOSIT_ORDER_{$user_id}";
  92. if (Cache::has($cache_key)) {
  93. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  94. }
  95. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  96. $order = $depositOrderRepository->byUserIdGetUserDepositOrder($user_id);
  97. if ($order) {
  98. return $this->errorNoValidation('您已经缴纳押金!');
  99. }
  100. $money = AreaSetting::where('area_id', $area_id)->value('deposit');
  101. // 开启一个数据库事务
  102. $data = [
  103. 'money' => $money,
  104. 'user_id' => $user_id,
  105. 'no' => DepositOrder::makeNo(),
  106. 'area_id' => $area_id,
  107. 'pay_status' => DepositOrder::PAY_STATUS_NO,
  108. 'pay_money' => $money,
  109. 'pay_type' => DepositOrder::PAY_TYPE_WECHAT,
  110. ];
  111. $order = DepositOrder::create($data);
  112. if (!$order) {
  113. return $this->errorNoValidation('订单错误');
  114. }
  115. $this->dispatch(new CloseOrderJob($order, Carbon::now()->addMinutes(30)));
  116. $payment = app('wechat.payment'); // 微信支付
  117. $auth = $this->user->auth;
  118. $result = $payment->order->unify([
  119. 'body' => "用户缴纳押金-" . config('app.name', '未来bike'),
  120. 'out_trade_no' => $order->no,
  121. 'trade_type' => 'JSAPI', // 必须为JSAPI
  122. 'openid' => $auth['credential'], // 这里的openid为付款人的openid
  123. 'total_fee' => wechat_fee($order->pay_money), // 总价
  124. 'attach' => makeNoTag(DepositOrder::NO_TAG)
  125. ]);
  126. // 如果成功生成统一下单的订单,那么进行二次签名
  127. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  128. // 二次签名的参数必须与下面相同
  129. $params = [
  130. 'appId' => $auth['identifier'],
  131. 'timeStamp' => time(),
  132. 'nonceStr' => $result['nonce_str'],
  133. 'package' => 'prepay_id=' . $result['prepay_id'],
  134. 'signType' => 'MD5',
  135. ];
  136. // config('wechat.payment.default.key')为商户的key
  137. $params['paySign'] = generate_sign($params, config('wechat.payment.default.key'));
  138. return $this->response->array($params);
  139. }
  140. return $this->errorNoValidation('下单失败');
  141. } catch (\Exception $exception) {
  142. return $this->errorException($exception->getMessage());
  143. }
  144. }
  145. // /**
  146. // * 退押金
  147. // * @param OrderRepository $orderRepository
  148. // * @param RentOrderRepository $rentOrderRepository
  149. // * @param RefundLogRepository $refundLogRepository
  150. // * @param PunishmentOrderRepository $punishmentOrderRepository
  151. // * Author: Mead
  152. // */
  153. // public function refundJob(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  154. // {
  155. // try {
  156. // $user_id = $this->user->id;
  157. //
  158. // return $this->errorNoValidation('退款己达到小程序每日支付数据上限!为避免系统崩溃,请您次日再操作!');
  159. // if (!$userRepository->byIdCheckStatusOk($user_id)) {
  160. // return $this->errorNoValidation('账号状态异常,暂不能退款!');
  161. // }
  162. //
  163. // // 有未结账的订单是能不能退
  164. // if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  165. // return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  166. // }
  167. //
  168. // if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  169. // return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  170. // }
  171. //
  172. // if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  173. // return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  174. // }
  175. //
  176. // $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  177. // if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  178. //
  179. // if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  180. // return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  181. // }
  182. //
  183. // $order = DepositOrder::where('user_id', $this->user->id)->where('pay_status', DepositOrder::PAY_STATUS_OK)->where('is_refund', DepositOrder::REFUND_NO)->orderBy('id', 'desc')->first();
  184. //
  185. // if (!$order) {
  186. // return $this->errorNoValidation('你还没有缴纳押金');
  187. // }
  188. //
  189. // if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_WAIT)->exists()) {
  190. // return $this->response()->array([
  191. // 'type' => 2
  192. // ]);
  193. // }
  194. //
  195. // if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  196. // $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  197. // if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  198. // return $this->errorNoValidation($result['msg']);
  199. // }
  200. // }
  201. //
  202. //
  203. // $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  204. // if (Cache::has($cache_key)) {
  205. // return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  206. // }
  207. // Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  208. //
  209. // $refund = DB::transaction(function () use ($order) {
  210. //
  211. // $refund = RefundLog::firstOrCreate([
  212. // 'deposit_id' => $order->id,
  213. // 'user_id' => $this->user->id,
  214. // ], [
  215. // 'no' => RefundLog::makeNo(),
  216. // 'money' => $order->pay_money,
  217. // 'type' => RefundLog::TYPE_USER,
  218. // 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  219. // 'area_id' => $order->area_id,
  220. // 'pay_type' => $order->pay_type,
  221. // 'pay_money' => $order->pay_money,
  222. // 'pay_status' => RefundLog::PAY_STATUS_WAIT,
  223. // ]);
  224. //
  225. // // 修改用户押金
  226. //
  227. // return $refund;
  228. //
  229. // });
  230. //
  231. // $this->dispatch(new DepositRefundJob($refund, Carbon::now()->addMinutes(5)));
  232. //
  233. // return $this->response()->array([
  234. // 'type' => 1
  235. // ]);
  236. //
  237. //// return $this->errorNoValidation($refund->result);
  238. // } catch (\Exception $exception) {
  239. // return $this->errorNoValidation($exception->getMessage());
  240. // }
  241. // }
  242. /**
  243. * 退还押金
  244. * @param DepositOrderRepository $depositOrderRepository
  245. * User: Mead
  246. */
  247. public function refund(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  248. {
  249. try {
  250. $user_id = $this->user->id;
  251. $day = date('Y-m-d');
  252. $d = Cache::get('refund:nums:day:' . $day, 0);
  253. if ($d >= 150) return $this->errorNoValidation('退款己达到小程序每日支付数据上限!请次日操作吧');
  254. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  255. return $this->errorNoValidation('账号状态异常,暂不能退款!');
  256. }
  257. // 有未结账的订单是能不能退
  258. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  259. return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  260. }
  261. if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  262. return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  263. }
  264. if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  265. return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  266. }
  267. $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  268. if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  269. if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  270. return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  271. }
  272. $order = DepositOrder::where('user_id', $this->user->id)->where('pay_status', DepositOrder::PAY_STATUS_OK)->where('is_refund', DepositOrder::REFUND_NO)->orderBy('id', 'desc')->first();
  273. if (!$order) {
  274. return $this->errorNoValidation('你还没有缴纳押金');
  275. }
  276. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  277. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  278. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  279. return $this->errorNoValidation($result['msg']);
  280. }
  281. }
  282. $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  283. if (Cache::has($cache_key)) {
  284. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  285. }
  286. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  287. $refund = DB::transaction(function () use ($order) {
  288. $refund = RefundLog::firstOrCreate([
  289. 'deposit_id' => $order->id,
  290. 'user_id' => $this->user->id,
  291. ], [
  292. 'no' => RefundLog::makeNo(),
  293. 'money' => $order->pay_money,
  294. 'type' => RefundLog::TYPE_USER,
  295. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  296. 'area_id' => $order->area_id,
  297. 'pay_type' => $order->pay_type,
  298. 'pay_money' => $order->pay_money,
  299. 'pay_status' => RefundLog::PAY_STATUS_NO
  300. ]);
  301. // 修改用户押金
  302. if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) {
  303. $refund->pay_status = RefundLog::PAY_STATUS_NO;
  304. $refund->type = RefundLog::TYPE_USER_SPEED;
  305. $refund->save();
  306. }
  307. return $refund;
  308. });
  309. //超期押金
  310. if (strtotime($order->pay_time) < strtotime(Carbon::now()->addYears(-1)->toDateTimeString())) {
  311. $user = $this->user;
  312. $auth = $user->auth;
  313. //超期退款
  314. $payment = app('wechat.payment'); // 微信支付
  315. $result = $payment->redpack->sendMiniprogramNormal([
  316. 'mch_billno' => $refund->no,
  317. 'send_name' => '未来GO',
  318. 're_openid' => $auth['credential'],
  319. 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
  320. 'wishing' => '退还超期押金',
  321. 'act_name' => '退还超期押金',
  322. 'remark' => '退还超期押金',
  323. ]);
  324. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'FAIL') {
  325. if ($result['err_code'] === 'SEND_FAILED') {
  326. $result = $payment->redpack->sendMiniprogramNormal([
  327. 'mch_billno' => $refund->no . '-1',
  328. 'send_name' => '未来GO',
  329. 're_openid' => $auth['credential'],
  330. 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
  331. 'wishing' => '退还超期押金',
  332. 'act_name' => '退还超期押金',
  333. 'remark' => '退还超期押金',
  334. ]);
  335. }
  336. }
  337. $refund->type = RefundLog::TYPE_RED_PACKET;
  338. $refund->save();
  339. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(1)));
  340. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(5)));
  341. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(10)));
  342. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addHours(6)));
  343. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  344. $order->is_refund = DepositOrder::REFUND_OK;
  345. $order->save();
  346. // $d += 1;
  347. // Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1));
  348. }
  349. $ss = php2js($result);
  350. Log::error("
  351. ********红包1*************
  352. no: {$refund->no}
  353. result: {$ss}
  354. ");
  355. } else {
  356. $payment = app('wechat.payment'); // 微信支付
  357. $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  358. // 可在此处传入其他参数,详细参数见微信支付文档
  359. 'refund_desc' => '用户申请退回押金',
  360. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify',
  361. ]);
  362. }
  363. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  364. $d += 1;
  365. Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1));
  366. // 判断是否有免押金卡
  367. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  368. if (Carbon::now()->gte($deposit_expire_time)) {
  369. // 免押金卡已经过期
  370. User::where('id', $this->user->id)->update([
  371. 'deposit_money' => 0,
  372. 'is_deposit' => User::DEPOSIT_NO,
  373. 'deposit_type' => User::DEPOSIT_TYPE_NO
  374. ]);
  375. } else {
  376. // 免押金卡未过期
  377. User::where('id', $this->user->id)->update([
  378. 'deposit_money' => 0,
  379. 'is_deposit' => User::DEPOSIT_OK,
  380. 'deposit_type' => User::DEPOSIT_CARD
  381. ]);
  382. }
  383. return $this->response->array([
  384. 'is_refund_status' => true
  385. ]);
  386. } else {
  387. $refund->result = $result['err_code_des'];
  388. }
  389. $refund->save();
  390. return $this->errorNoValidation($refund->result);
  391. } catch (\Exception $exception) {
  392. return $this->errorNoValidation($exception->getMessage());
  393. }
  394. }
  395. public function refundJob(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  396. {
  397. try {
  398. $user_id = $this->user->id;
  399. $day = date('Y-m-d');
  400. $d = Cache::get('refund:nums:day:' . $day, 0);
  401. if ($d >= 150) return $this->errorNoValidation('退款己达到小程序每日支付数据上限!请次日操作吧');
  402. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  403. return $this->errorNoValidation('账号状态异常,暂不能退款!');
  404. }
  405. // 有未结账的订单是能不能退
  406. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  407. return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  408. }
  409. if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  410. return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  411. }
  412. if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  413. return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  414. }
  415. $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  416. if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  417. if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  418. return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  419. }
  420. $order = DepositOrder::where('user_id', $this->user->id)->where('pay_status', DepositOrder::PAY_STATUS_OK)->where('is_refund', DepositOrder::REFUND_NO)->orderBy('id', 'desc')->first();
  421. if (!$order) {
  422. return $this->errorNoValidation('你还没有缴纳押金');
  423. }
  424. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  425. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  426. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  427. return $this->errorNoValidation($result['msg']);
  428. }
  429. }
  430. $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  431. if (Cache::has($cache_key)) {
  432. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  433. }
  434. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  435. $refund = DB::transaction(function () use ($order) {
  436. $refund = RefundLog::firstOrCreate([
  437. 'deposit_id' => $order->id,
  438. 'user_id' => $this->user->id,
  439. ], [
  440. 'no' => RefundLog::makeNo(),
  441. 'money' => $order->pay_money,
  442. 'type' => RefundLog::TYPE_USER,
  443. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  444. 'area_id' => $order->area_id,
  445. 'pay_type' => $order->pay_type,
  446. 'pay_money' => $order->pay_money,
  447. 'pay_status' => RefundLog::PAY_STATUS_NO
  448. ]);
  449. // 修改用户押金
  450. if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) {
  451. $refund->pay_status = RefundLog::PAY_STATUS_NO;
  452. $refund->type = RefundLog::TYPE_USER_SPEED;
  453. $refund->save();
  454. }
  455. return $refund;
  456. });
  457. //超期押金
  458. if (strtotime($order->pay_time) < strtotime(Carbon::now()->addYears(-1)->toDateTimeString())) {
  459. $user = $this->user;
  460. $auth = $user->auth;
  461. //超期退款
  462. $payment = app('wechat.payment'); // 微信支付
  463. $result = $payment->redpack->sendMiniprogramNormal([
  464. 'mch_billno' => $refund->no,
  465. 'send_name' => '未来GO',
  466. 're_openid' => $auth['credential'],
  467. 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
  468. 'wishing' => '退还超期押金',
  469. 'act_name' => '退还超期押金',
  470. 'remark' => '退还超期押金',
  471. ]);
  472. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'FAIL') {
  473. if ($result['err_code'] === 'SEND_FAILED') {
  474. $result = $payment->redpack->sendMiniprogramNormal([
  475. 'mch_billno' => $refund->no . '-1',
  476. 'send_name' => '未来GO',
  477. 're_openid' => $auth['credential'],
  478. 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
  479. 'wishing' => '退还超期押金',
  480. 'act_name' => '退还超期押金',
  481. 'remark' => '退还超期押金',
  482. ]);
  483. }
  484. }
  485. $refund->type = RefundLog::TYPE_RED_PACKET;
  486. $refund->save();
  487. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(1)));
  488. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(5)));
  489. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(10)));
  490. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addHours(6)));
  491. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  492. $order->is_refund = DepositOrder::REFUND_OK;
  493. $order->save();
  494. // $d += 1;
  495. // Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1));
  496. }
  497. $ss = php2js($result);
  498. Log::error("
  499. ********红包1*************
  500. no: {$refund->no}
  501. result: {$ss}
  502. ");
  503. } else {
  504. $payment = app('wechat.payment'); // 微信支付
  505. $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  506. // 可在此处传入其他参数,详细参数见微信支付文档
  507. 'refund_desc' => '用户申请退回押金',
  508. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify',
  509. ]);
  510. }
  511. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  512. $d += 1;
  513. Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1));
  514. // 判断是否有免押金卡
  515. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  516. if (Carbon::now()->gte($deposit_expire_time)) {
  517. // 免押金卡已经过期
  518. User::where('id', $this->user->id)->update([
  519. 'deposit_money' => 0,
  520. 'is_deposit' => User::DEPOSIT_NO,
  521. 'deposit_type' => User::DEPOSIT_TYPE_NO
  522. ]);
  523. } else {
  524. // 免押金卡未过期
  525. User::where('id', $this->user->id)->update([
  526. 'deposit_money' => 0,
  527. 'is_deposit' => User::DEPOSIT_OK,
  528. 'deposit_type' => User::DEPOSIT_CARD
  529. ]);
  530. }
  531. return $this->response->array([
  532. 'is_refund_status' => true
  533. ]);
  534. } else {
  535. $refund->result = $result['err_code_des'];
  536. }
  537. $refund->save();
  538. return $this->errorNoValidation($refund->result);
  539. } catch (\Exception $exception) {
  540. return $this->errorNoValidation($exception->getMessage());
  541. }
  542. }
  543. }