123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/8/19
- * Time: 2:11 PM
- */
- namespace App\Http\Controllers\V1;
- use App\Http\Requests\AreaIdRequest;
- use App\Jobs\CloseOrderJob;
- use App\Jobs\DepositRefundJob;
- use App\Models\AreaSetting;
- use App\Models\DepositOrder;
- use App\Models\RefundLog;
- use App\Models\User;
- use App\Repositories\DepositOrderRepository;
- use App\Repositories\OrderRepository;
- use App\Repositories\PunishmentOrderRepository;
- use App\Repositories\RefundLogRepository;
- use App\Repositories\RentOrderRepository;
- use App\Repositories\UserRepository;
- use Carbon\Carbon;
- use Dingo\Api\Http\Request;
- use function EasyWeChat\Kernel\Support\generate_sign;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- /**
- * 押金管理模块
- * Class DepositOrderController
- * @package App\Http\Controllers\V1
- */
- class DepositOrderController extends BaseController
- {
- /**
- * 支付押金
- * @param Request $request
- * User: Mead
- */
- public function store(AreaIdRequest $request, DepositOrderRepository $depositOrderRepository, RefundLogRepository $refundLogRepository)
- {
- try {
- $area_id = $request->get('area_id');
- $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;
- 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;
- 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;
- });
- $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') {
- // 判断是否有免押金卡
- $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());
- }
- }
- }
|