123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/8/9
- * Time: 7:39 PM.
- */
- namespace App\Http\Controllers\V1;
- use App\Http\Controllers\Controller;
- use App\Jobs\CheckRedOrderJob;
- use App\Models\DepositOrder;
- use App\Models\RefundLog;
- use App\Models\User;
- use App\Repositories\OrderRepository;
- use App\Repositories\PunishmentOrderRepository;
- use App\Repositories\RefundLogRepository;
- use App\Repositories\RentOrderRepository;
- use App\Repositories\UserRepository;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 对外接口模块
- * Class AreaController
- * @package App\Http\Controllers\V1
- */
- class ApiController extends Controller
- {
- /**
- * 清空缓存
- * @return mixed
- * User: Mead
- */
- public function clearCache()
- {
- try {
- Cache::flush();
- return [
- 'status' => true
- ];
- } catch (\Exception $exception) {
- return $this->errorNoValidation($exception->getMessage());
- }
- }
- /**
- * 退款超期api
- * @param Request $request
- * @return
- */
- public function refundOutDeposit(Request $request, UserRepository $userRepository, OrderRepository $orderRepository, PunishmentOrderRepository $punishmentOrderRepository, RefundLogRepository $refundLogRepository)
- {
- $response = [
- 'status' => 0,
- 'msg' => '操作失败'
- ];
- $id = $request->get('id');
- try {
- $order = DepositOrder::query()->where('id', $id)->first();
- if (!$order) {
- $response['msg'] = '找不到该订单';
- return $response;
- }
- $user_id = $order['user_id'];
- // $user_id = 7;
- if (!$userRepository->byIdCheckStatusOk($user_id)) {
- $response['msg'] = '账号状态异常,暂不能退款!';
- return $response;
- }
- // 有未结账的订单是能不能退
- if ($orderRepository->byUserIdCheckIsExistNoPayOrder($user_id)) {
- $response['msg'] = '您有未结算的订单,暂不能退款!';
- return $response;
- }
- $punish = $punishmentOrderRepository->checkNoPayModel($user_id);
- if (!$punish) {
- $response['msg'] = '您有罚单未支付,请先处理';
- return $response;
- }
- $user = User::query()->where('id', $user_id)->first();
- if ((int)$user->deposit_type === User::DEPOSIT_CARD) {
- $response['msg'] = '押金类型为免押金卡,不能退还押金';
- return $response;
- }
- if (!$order) {
- $response['msg'] = '你还没有缴纳押金';
- return $response;
- }
- if (RefundLog::query()->where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
- $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($user_id);
- if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
- $response['msg'] = $result['msg'];
- return $response;
- }
- }
- $refund = DB::transaction(function () use ($order, $user_id) {
- $refund = RefundLog::firstOrCreate([
- 'deposit_id' => $order->id,
- 'user_id' => $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())) {
- $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') {
- $re = $payment->redpack->info($refund->no);
- log_record('查询', $re);
- if ($re['return_code'] === 'SUCCESS' && $re['result_code'] === 'SUCCESS') {
- switch ($re['status']) {
- //发放中
- case 'SENDING':
- $refund->remark = '超期押金退款红包发送中';
- break;
- //已发放待领取
- case 'SENT':
- $refund->remark = '超期押金退款红包已发放,请及时领取';
- break;
- //发放失败
- case 'FAILED':
- $refund->remark = '超期押金退款红包发放失败,请联系人工退还';
- $refund->pay_status = RefundLog::PAY_STATUS_ERROR;
- $refund->result = $re['err_code_des'];
- $refund->save();
- $result = $payment->redpack->sendMiniprogramNormal([
- 'mch_billno' => $refund->no . 'm',
- 'send_name' => '未来GO',
- 're_openid' => $auth['credential'],
- 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
- 'wishing' => '退还超期押金',
- 'act_name' => '退还超期押金',
- 'remark' => '退还超期押金',
- ]);
- break;
- //已领取
- case 'RECEIVED':
- $refund->pay_time = $re['hblist']['hbinfo']['rcv_time'];
- $refund->pay_status = RefundLog::PAY_STATUS_OK;
- $refund->save();
- $refund->refund_order_callback();
- //退款中 //已退款
- case 'RFUND_ING':
- case 'REFUND':
- $refund->remark = '超时未领取异常';
- $refund->pay_status = RefundLog::PAY_STATUS_ERROR;
- $refund->result = $re['err_code_des'];
- $refund->save();
- break;
- }
- }
- }
- }
- log_record('发红包', $result);
- $refund->type = RefundLog::TYPE_RED_PACKET;
- $refund->result = $result['err_code_des'];
- $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();
- }
- $ss = php2js($result);
- Log::error("
- ********红包1*************
- no: {$refund->no}
- result: {$ss}
- ");
- } else {
- $response['msg'] = '该订单不超期';
- return $response;
- }
- if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
- // 判断是否有免押金卡
- $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
- if (Carbon::now()->gte($deposit_expire_time)) {
- // 免押金卡已经过期
- User::where('id', $user_id)->update([
- 'deposit_money' => 0,
- 'is_deposit' => User::DEPOSIT_NO,
- 'deposit_type' => User::DEPOSIT_TYPE_NO
- ]);
- } else {
- // 免押金卡未过期
- User::where('id', $user_id)->update([
- 'deposit_money' => 0,
- 'is_deposit' => User::DEPOSIT_OK,
- 'deposit_type' => User::DEPOSIT_CARD
- ]);
- }
- $response['status'] = 1;
- $response['msg'] = $result['err_code_des'];
- return $response;
- } else {
- $refund->result = $result['err_code_des'];
- }
- $refund->save();
- $response['status'] = 0;
- $response['msg'] = $result['err_code_des'];
- return $response;
- } catch (\Exception $exception) {
- $response['status'] = 0;
- $response['msg'] = $exception->getMessage();
- return $response;
- }
- }
- }
|