DepositOrderController.php 16 KB

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