get('area_id'); $user_id = $this->user->id; // $order = $depositOrderRepository->byUserIdGetUserDepositOrder($user_id); // // if ($order) { // return $this->errorNoValidation('您已经缴纳押金!'); // } $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($user_id); if (!$result['status']) { return $this->errorNoValidation($result['msg']); } $cache_key = "DEPOSIT_ORDER_{$user_id}"; if (Cache::has($cache_key)) { return $this->errorNoValidation('您提交的太频繁了,请一会再提交!'); } Cache::put($cache_key, 1, Carbon::now()->addSeconds(5)); $money = AreaSetting::where('area_id', $area_id)->value('deposit'); // 开启一个数据库事务 $data = [ 'money' => $money, 'user_id' => $user_id, 'no' => DepositOrder::makeNo(), 'area_id' => $area_id, 'pay_status' => DepositOrder::PAY_STATUS_NO, 'pay_money' => $money, 'pay_type' => DepositOrder::PAY_TYPE_WECHAT, ]; $order = DepositOrder::create($data); if (!$order) { return $this->errorNoValidation('订单错误'); } $this->dispatch(new CloseOrderJob($order, Carbon::now()->addMinutes(30))); $payment = app('wechat.payment'); // 微信支付 $username = $this->user->truename; $auth = $this->user->auth; $result = $payment->order->unify([ 'body' => "[{$username}]缴纳押金", 'out_trade_no' => $order->no, 'trade_type' => 'JSAPI', // 必须为JSAPI 'openid' => $auth['credential'], // 这里的openid为付款人的openid 'total_fee' => wechat_fee($order->pay_money), // 总价 'attach' => DepositOrder::NO_TAG ]); // 如果成功生成统一下单的订单,那么进行二次签名 if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { // 二次签名的参数必须与下面相同 $params = [ 'appId' => $auth['identifier'], 'timeStamp' => time(), 'nonceStr' => $result['nonce_str'], 'package' => 'prepay_id=' . $result['prepay_id'], 'signType' => 'MD5', ]; // config('wechat.payment.default.key')为商户的key $params['paySign'] = generate_sign($params, config('wechat.payment.default.key')); return $this->response->array($params); } return $this->errorNoValidation('下单失败'); } catch (\Exception $exception) { return $this->errorException($exception->getMessage()); } } /** * 退还押金 * @param DepositOrderRepository $depositOrderRepository * User: Mead */ public function refund(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository) { try { // 有未结账的订单是能不能退 if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) { return $this->errorNoValidation('您有未结算的订单,暂不能退款!'); } if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) { return $this->errorNoValidation('您有未完成的租车订单,请先处理'); } if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) { return $this->errorNoValidation('您有未支付的租车订单,请先处理'); } if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) { return $this->errorNoValidation('押金类型为免押金卡,不能退还押金'); } $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(); if (!$order) { return $this->errorNoValidation('你还没有缴纳押金'); } if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) { return $this->errorNoValidation('您已提交,请耐心等待审核'); } $user_id = $this->user->id; $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}"; if (Cache::has($cache_key)) { return $this->errorNoValidation('您提交的太频繁了,请一会再提交!'); } Cache::put($cache_key, 1, Carbon::now()->addSeconds(5)); $refund = DB::transaction(function () use ($order) { $refund = RefundLog::firstOrCreate([ 'deposit_id' => $order->id, 'user_id' => $this->user->id, ], [ 'no' => RefundLog::makeNo(), 'money' => $order->pay_money, 'type' => RefundLog::TYPE_USER, 'is_check_status' => RefundLog::CHECK_STATUS_OK, 'area_id' => $order->area_id, 'pay_type' => $order->pay_type, 'pay_money' => $order->pay_money, 'pay_status' => RefundLog::PAY_STATUS_NO ]); // 修改用户押金 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_NO ]); return $refund; }); $payment = app('wechat.payment'); // 微信支付 $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '退押金' ]); $refund->save(); if ($result['return_code'] === 'SUCCESS') { return $this->response->array([ 'is_refund_status' => true ]); } return $this->errorNoValidation('押金退还失败'); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } }