get('area_id'); $setting = $areaSettingRepository->byAreaId($area_id); // 判断区域是否需要缴纳押金 if ($setting['is_deposit'] == AreaSetting::DEPOSIT_NO) { return $this->errorNoValidation('此区域暂时无需缴纳押金'); } $user_id = $this->user->id; $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($user_id); if (!$result['status']) { if ($result['code'] === 'REFUNDNOTEXIST') { //发起退款 $payment = app('wechat.payment'); // 微信支付 $refund = $refundLogRepository->byUserIdUserRefundNoModel($user_id); $order = $depositOrderRepository->byIdGetModel($refund->deposit_id); $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '用户申请退回押金', 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify', ]); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { // 判断是否有免押金卡 $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time); if (Carbon::now()->gte($deposit_expire_time)) { // 免押金卡已经过期 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_NO, 'deposit_type' => User::DEPOSIT_TYPE_NO ]); } else { // 免押金卡未过期 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_OK, 'deposit_type' => User::DEPOSIT_CARD ]); } } else { $refund->result = $result['err_code_des']; $refund->save(); return $this->errorNoValidation($refund->result); } } else { 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)); $order = $depositOrderRepository->byUserIdGetUserDepositOrder($user_id); if ($order) { return $this->errorNoValidation('您已经缴纳押金!'); } $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'); // 微信支付 $auth = $this->user->auth; $result = $payment->order->unify([ 'body' => "用户缴纳押金-" . config('app.name', '未来bike'), 'out_trade_no' => $order->no, 'trade_type' => 'JSAPI', // 必须为JSAPI 'openid' => $auth['credential'], // 这里的openid为付款人的openid 'total_fee' => wechat_fee($order->pay_money), // 总价 'attach' => makeNoTag(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 OrderRepository $orderRepository // * @param RentOrderRepository $rentOrderRepository // * @param RefundLogRepository $refundLogRepository // * @param PunishmentOrderRepository $punishmentOrderRepository // * Author: Mead // */ // public function refundJob(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository) // { // try { // $user_id = $this->user->id; // // return $this->errorNoValidation('退款己达到小程序每日支付数据上限!为避免系统崩溃,请您次日再操作!'); // if (!$userRepository->byIdCheckStatusOk($user_id)) { // return $this->errorNoValidation('账号状态异常,暂不能退款!'); // } // // // 有未结账的订单是能不能退 // 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('您有未支付的租车订单,请先处理'); // } // // $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id); // if (!$punish) 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_WAIT)->exists()) { // return $this->response()->array([ // 'type' => 2 // ]); // } // // if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) { // $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id); // if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') { // return $this->errorNoValidation($result['msg']); // } // } // // // $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_WAIT, // ]); // // // 修改用户押金 // // return $refund; // // }); // // $this->dispatch(new DepositRefundJob($refund, Carbon::now()->addMinutes(5))); // // return $this->response()->array([ // 'type' => 1 // ]); // //// return $this->errorNoValidation($refund->result); // } catch (\Exception $exception) { // return $this->errorNoValidation($exception->getMessage()); // } // } /** * 退还押金 * @param DepositOrderRepository $depositOrderRepository * User: Mead */ public function refund(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository) { try { $user_id = $this->user->id; $day = date('Y-m-d'); $d = Cache::get('refund:nums:day:' . $day, 0); if ($d >= 150) return $this->errorNoValidation('退款己达到小程序每日支付数据上限!请次日操作吧'); if (!$userRepository->byIdCheckStatusOk($user_id)) { return $this->errorNoValidation('账号状态异常,暂不能退款!'); } // 有未结账的订单是能不能退 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('您有未支付的租车订单,请先处理'); } $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id); if (!$punish) 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()) { $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id); if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') { return $this->errorNoValidation($result['msg']); } } $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 ]); // 修改用户押金 if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) { $refund->pay_status = RefundLog::PAY_STATUS_NO; $refund->type = RefundLog::TYPE_USER_SPEED; $refund->save(); } return $refund; }); //超期押金 if (strtotime($order->pay_time) < strtotime(Carbon::now()->addYears(-1)->toDateTimeString())) { $user = $this->user; $auth = $user->auth; //超期退款 $payment = app('wechat.payment'); // 微信支付 $result = $payment->redpack->sendMiniprogramNormal([ 'mch_billno' => $refund->no, 'send_name' => '未来GO', 're_openid' => $auth['credential'], 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100 'wishing' => '退还超期押金', 'act_name' => '退还超期押金', 'remark' => '退还超期押金', ]); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'FAIL') { if ($result['err_code'] === 'SEND_FAILED') { $result = $payment->redpack->sendMiniprogramNormal([ 'mch_billno' => $refund->no . '-1', 'send_name' => '未来GO', 're_openid' => $auth['credential'], 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100 'wishing' => '退还超期押金', 'act_name' => '退还超期押金', 'remark' => '退还超期押金', ]); } } $refund->type = RefundLog::TYPE_RED_PACKET; $refund->save(); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(1))); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(5))); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(10))); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addHours(6))); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { $order->is_refund = DepositOrder::REFUND_OK; $order->save(); // $d += 1; // Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1)); } $ss = php2js($result); Log::error(" ********红包1************* no: {$refund->no} result: {$ss} "); } else { $payment = app('wechat.payment'); // 微信支付 $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '用户申请退回押金', 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify', ]); } if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { $d += 1; Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1)); // 判断是否有免押金卡 $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time); if (Carbon::now()->gte($deposit_expire_time)) { // 免押金卡已经过期 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_NO, 'deposit_type' => User::DEPOSIT_TYPE_NO ]); } else { // 免押金卡未过期 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_OK, 'deposit_type' => User::DEPOSIT_CARD ]); } return $this->response->array([ 'is_refund_status' => true ]); } else { $refund->result = $result['err_code_des']; } $refund->save(); return $this->errorNoValidation($refund->result); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } public function refundJob(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository) { try { $user_id = $this->user->id; $day = date('Y-m-d'); $d = Cache::get('refund:nums:day:' . $day, 0); if ($d >= 150) return $this->errorNoValidation('退款己达到小程序每日支付数据上限!请次日操作吧'); if (!$userRepository->byIdCheckStatusOk($user_id)) { return $this->errorNoValidation('账号状态异常,暂不能退款!'); } // 有未结账的订单是能不能退 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('您有未支付的租车订单,请先处理'); } $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id); if (!$punish) 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()) { $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id); if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') { return $this->errorNoValidation($result['msg']); } } $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 ]); // 修改用户押金 if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) { $refund->pay_status = RefundLog::PAY_STATUS_NO; $refund->type = RefundLog::TYPE_USER_SPEED; $refund->save(); } return $refund; }); //超期押金 if (strtotime($order->pay_time) < strtotime(Carbon::now()->addYears(-1)->toDateTimeString())) { $user = $this->user; $auth = $user->auth; //超期退款 $payment = app('wechat.payment'); // 微信支付 $result = $payment->redpack->sendMiniprogramNormal([ 'mch_billno' => $refund->no, 'send_name' => '未来GO', 're_openid' => $auth['credential'], 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100 'wishing' => '退还超期押金', 'act_name' => '退还超期押金', 'remark' => '退还超期押金', ]); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'FAIL') { if ($result['err_code'] === 'SEND_FAILED') { $result = $payment->redpack->sendMiniprogramNormal([ 'mch_billno' => $refund->no . '-1', 'send_name' => '未来GO', 're_openid' => $auth['credential'], 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100 'wishing' => '退还超期押金', 'act_name' => '退还超期押金', 'remark' => '退还超期押金', ]); } } $refund->type = RefundLog::TYPE_RED_PACKET; $refund->save(); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(1))); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(5))); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(10))); $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addHours(6))); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { $order->is_refund = DepositOrder::REFUND_OK; $order->save(); // $d += 1; // Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1)); } $ss = php2js($result); Log::error(" ********红包1************* no: {$refund->no} result: {$ss} "); } else { $payment = app('wechat.payment'); // 微信支付 $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '用户申请退回押金', 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify', ]); } if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { $d += 1; Cache::put('refund:nums:day:' . $day, $d, Carbon::now()->addDays(1)); // 判断是否有免押金卡 $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time); if (Carbon::now()->gte($deposit_expire_time)) { // 免押金卡已经过期 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_NO, 'deposit_type' => User::DEPOSIT_TYPE_NO ]); } else { // 免押金卡未过期 User::where('id', $this->user->id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_OK, 'deposit_type' => User::DEPOSIT_CARD ]); } return $this->response->array([ 'is_refund_status' => true ]); } else { $refund->result = $result['err_code_des']; } $refund->save(); return $this->errorNoValidation($refund->result); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } }