DepositOrderController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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\CloseOrderJob;
  11. use App\Jobs\DepositRefundJob;
  12. use App\Models\AreaSetting;
  13. use App\Models\DepositOrder;
  14. use App\Models\RefundLog;
  15. use App\Models\User;
  16. use App\Repositories\DepositOrderRepository;
  17. use App\Repositories\OrderRepository;
  18. use App\Repositories\PunishmentOrderRepository;
  19. use App\Repositories\RefundLogRepository;
  20. use App\Repositories\RentOrderRepository;
  21. use App\Repositories\UserRepository;
  22. use Carbon\Carbon;
  23. use Dingo\Api\Http\Request;
  24. use function EasyWeChat\Kernel\Support\generate_sign;
  25. use Illuminate\Support\Facades\Cache;
  26. use Illuminate\Support\Facades\DB;
  27. /**
  28. * 押金管理模块
  29. * Class DepositOrderController
  30. * @package App\Http\Controllers\V1
  31. */
  32. class DepositOrderController extends BaseController
  33. {
  34. /**
  35. * 支付押金
  36. * @param Request $request
  37. * User: Mead
  38. */
  39. public function store(AreaIdRequest $request, DepositOrderRepository $depositOrderRepository, RefundLogRepository $refundLogRepository)
  40. {
  41. try {
  42. $area_id = $request->get('area_id');
  43. $is_deposit = AreaSetting::where('area_id', $area_id)->value('is_deposit');
  44. if (!(int)$is_deposit) {
  45. return $this->errorNoValidation('该区域免押金骑行');
  46. }
  47. $user_id = $this->user->id;
  48. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($user_id);
  49. if (!$result['status']) {
  50. if ($result['code'] === 'REFUNDNOTEXIST') {
  51. //发起退款
  52. $payment = app('wechat.payment'); // 微信支付
  53. $refund = $refundLogRepository->byUserIdUserRefundNoModel($user_id);
  54. $order = $depositOrderRepository->byIdGetModel($refund->deposit_id);
  55. $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  56. // 可在此处传入其他参数,详细参数见微信支付文档
  57. 'refund_desc' => '用户申请退回押金',
  58. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify',
  59. ]);
  60. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  61. // 判断是否有免押金卡
  62. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  63. if (Carbon::now()->gte($deposit_expire_time)) {
  64. // 免押金卡已经过期
  65. User::where('id', $this->user->id)->update([
  66. 'deposit_money' => 0,
  67. 'is_deposit' => User::DEPOSIT_NO,
  68. 'deposit_type' => User::DEPOSIT_TYPE_NO
  69. ]);
  70. } else {
  71. // 免押金卡未过期
  72. User::where('id', $this->user->id)->update([
  73. 'deposit_money' => 0,
  74. 'is_deposit' => User::DEPOSIT_OK,
  75. 'deposit_type' => User::DEPOSIT_CARD
  76. ]);
  77. }
  78. } else {
  79. $refund->result = $result['err_code_des'];
  80. $refund->save();
  81. return $this->errorNoValidation($refund->result);
  82. }
  83. } else {
  84. return $this->errorNoValidation($result['msg']);
  85. }
  86. }
  87. $cache_key = "DEPOSIT_ORDER_{$user_id}";
  88. if (Cache::has($cache_key)) {
  89. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  90. }
  91. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  92. $order = $depositOrderRepository->byUserIdGetUserDepositOrder($user_id);
  93. if ($order) {
  94. return $this->errorNoValidation('您已经缴纳押金!');
  95. }
  96. $money = AreaSetting::where('area_id', $area_id)->value('deposit');
  97. // 开启一个数据库事务
  98. $data = [
  99. 'money' => $money,
  100. 'user_id' => $user_id,
  101. 'no' => DepositOrder::makeNo(),
  102. 'area_id' => $area_id,
  103. 'pay_status' => DepositOrder::PAY_STATUS_NO,
  104. 'pay_money' => $money,
  105. 'pay_type' => DepositOrder::PAY_TYPE_WECHAT,
  106. ];
  107. $order = DepositOrder::create($data);
  108. if (!$order) {
  109. return $this->errorNoValidation('订单错误');
  110. }
  111. $this->dispatch(new CloseOrderJob($order, Carbon::now()->addMinutes(30)));
  112. $payment = app('wechat.payment'); // 微信支付
  113. $auth = $this->user->auth;
  114. $result = $payment->order->unify([
  115. 'body' => "用户缴纳押金-" . config('app.name', '未来bike'),
  116. 'out_trade_no' => $order->no,
  117. 'trade_type' => 'JSAPI', // 必须为JSAPI
  118. 'openid' => $auth['credential'], // 这里的openid为付款人的openid
  119. 'total_fee' => wechat_fee($order->pay_money), // 总价
  120. 'attach' => makeNoTag(DepositOrder::NO_TAG)
  121. ]);
  122. // 如果成功生成统一下单的订单,那么进行二次签名
  123. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  124. // 二次签名的参数必须与下面相同
  125. $params = [
  126. 'appId' => $auth['identifier'],
  127. 'timeStamp' => time(),
  128. 'nonceStr' => $result['nonce_str'],
  129. 'package' => 'prepay_id=' . $result['prepay_id'],
  130. 'signType' => 'MD5',
  131. ];
  132. // config('wechat.payment.default.key')为商户的key
  133. $params['paySign'] = generate_sign($params, config('wechat.payment.default.key'));
  134. return $this->response->array($params);
  135. }
  136. return $this->errorNoValidation('下单失败');
  137. } catch (\Exception $exception) {
  138. return $this->errorException($exception->getMessage());
  139. }
  140. }
  141. /**
  142. * 退押金
  143. * @param OrderRepository $orderRepository
  144. * @param RentOrderRepository $rentOrderRepository
  145. * @param RefundLogRepository $refundLogRepository
  146. * @param PunishmentOrderRepository $punishmentOrderRepository
  147. * Author: Mead
  148. */
  149. public function refundJob(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  150. {
  151. try {
  152. $user_id = $this->user->id;
  153. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  154. return $this->errorNoValidation('账号状态异常,暂不能退款!');
  155. }
  156. // 有未结账的订单是能不能退
  157. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  158. return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  159. }
  160. if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  161. return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  162. }
  163. if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  164. return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  165. }
  166. $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  167. if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  168. if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  169. return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  170. }
  171. $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();
  172. if (!$order) {
  173. return $this->errorNoValidation('你还没有缴纳押金');
  174. }
  175. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_WAIT)->exists()) {
  176. return $this->response()->array([
  177. 'type' => 2
  178. ]);
  179. }
  180. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  181. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  182. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  183. return $this->errorNoValidation($result['msg']);
  184. }
  185. }
  186. $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  187. if (Cache::has($cache_key)) {
  188. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  189. }
  190. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  191. $refund = DB::transaction(function () use ($order) {
  192. $refund = RefundLog::firstOrCreate([
  193. 'deposit_id' => $order->id,
  194. 'user_id' => $this->user->id,
  195. ], [
  196. 'no' => RefundLog::makeNo(),
  197. 'money' => $order->pay_money,
  198. 'type' => RefundLog::TYPE_USER,
  199. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  200. 'area_id' => $order->area_id,
  201. 'pay_type' => $order->pay_type,
  202. 'pay_money' => $order->pay_money,
  203. 'pay_status' => RefundLog::PAY_STATUS_WAIT,
  204. ]);
  205. // 修改用户押金
  206. return $refund;
  207. });
  208. $this->dispatch(new DepositRefundJob($refund, Carbon::now()->addMinutes(5)));
  209. return $this->response()->array([
  210. 'type' => 1
  211. ]);
  212. // return $this->errorNoValidation($refund->result);
  213. } catch (\Exception $exception) {
  214. return $this->errorNoValidation($exception->getMessage());
  215. }
  216. }
  217. /**
  218. * 退还押金
  219. * @param DepositOrderRepository $depositOrderRepository
  220. * User: Mead
  221. */
  222. public function refund(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  223. {
  224. try {
  225. $user_id = $this->user->id;
  226. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  227. return $this->errorNoValidation('账号状态异常,暂不能退款!');
  228. }
  229. // 有未结账的订单是能不能退
  230. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  231. return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  232. }
  233. if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  234. return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  235. }
  236. if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  237. return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  238. }
  239. $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  240. if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  241. if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  242. return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  243. }
  244. $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();
  245. if (!$order) {
  246. return $this->errorNoValidation('你还没有缴纳押金');
  247. }
  248. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  249. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  250. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  251. return $this->errorNoValidation($result['msg']);
  252. }
  253. }
  254. $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  255. if (Cache::has($cache_key)) {
  256. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  257. }
  258. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  259. $refund = DB::transaction(function () use ($order) {
  260. $refund = RefundLog::firstOrCreate([
  261. 'deposit_id' => $order->id,
  262. 'user_id' => $this->user->id,
  263. ], [
  264. 'no' => RefundLog::makeNo(),
  265. 'money' => $order->pay_money,
  266. 'type' => RefundLog::TYPE_USER,
  267. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  268. 'area_id' => $order->area_id,
  269. 'pay_type' => $order->pay_type,
  270. 'pay_money' => $order->pay_money,
  271. 'pay_status' => RefundLog::PAY_STATUS_NO
  272. ]);
  273. // 修改用户押金
  274. if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) {
  275. $refund->pay_status = RefundLog::PAY_STATUS_NO;
  276. $refund->type = RefundLog::TYPE_USER_SPEED;
  277. $refund->save();
  278. }
  279. return $refund;
  280. });
  281. $payment = app('wechat.payment'); // 微信支付
  282. $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  283. // 可在此处传入其他参数,详细参数见微信支付文档
  284. 'refund_desc' => '用户申请退回押金',
  285. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify',
  286. ]);
  287. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  288. // 判断是否有免押金卡
  289. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  290. if (Carbon::now()->gte($deposit_expire_time)) {
  291. // 免押金卡已经过期
  292. User::where('id', $this->user->id)->update([
  293. 'deposit_money' => 0,
  294. 'is_deposit' => User::DEPOSIT_NO,
  295. 'deposit_type' => User::DEPOSIT_TYPE_NO
  296. ]);
  297. } else {
  298. // 免押金卡未过期
  299. User::where('id', $this->user->id)->update([
  300. 'deposit_money' => 0,
  301. 'is_deposit' => User::DEPOSIT_OK,
  302. 'deposit_type' => User::DEPOSIT_CARD
  303. ]);
  304. }
  305. return $this->response->array([
  306. 'is_refund_status' => true
  307. ]);
  308. } else {
  309. $refund->result = $result['err_code_des'];
  310. }
  311. $refund->save();
  312. return $this->errorNoValidation($refund->result);
  313. } catch (\Exception $exception) {
  314. return $this->errorNoValidation($exception->getMessage());
  315. }
  316. }
  317. }