ApiController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/9
  6. * Time: 7:39 PM.
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Http\Controllers\Controller;
  10. use App\Jobs\CheckRedOrderJob;
  11. use App\Models\DepositOrder;
  12. use App\Models\RefundLog;
  13. use App\Models\User;
  14. use App\Repositories\OrderRepository;
  15. use App\Repositories\PunishmentOrderRepository;
  16. use App\Repositories\RefundLogRepository;
  17. use App\Repositories\RentOrderRepository;
  18. use App\Repositories\UserRepository;
  19. use Carbon\Carbon;
  20. use Illuminate\Http\Request;
  21. use Illuminate\Http\Response;
  22. use Illuminate\Support\Facades\Cache;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Support\Facades\Log;
  25. /**
  26. * 对外接口模块
  27. * Class AreaController
  28. * @package App\Http\Controllers\V1
  29. */
  30. class ApiController extends Controller
  31. {
  32. /**
  33. * 清空缓存
  34. * @return mixed
  35. * User: Mead
  36. */
  37. public function clearCache()
  38. {
  39. try {
  40. Cache::flush();
  41. return [
  42. 'status' => true
  43. ];
  44. } catch (\Exception $exception) {
  45. return $this->errorNoValidation($exception->getMessage());
  46. }
  47. }
  48. /**
  49. * 退款超期api
  50. * @param Request $request
  51. * @return
  52. */
  53. public function refundOutDeposit(Request $request, UserRepository $userRepository, OrderRepository $orderRepository, PunishmentOrderRepository $punishmentOrderRepository, RefundLogRepository $refundLogRepository)
  54. {
  55. $response = [
  56. 'status' => 0,
  57. 'msg' => '操作失败'
  58. ];
  59. $id = $request->get('id');
  60. try {
  61. $order = DepositOrder::query()->where('id', $id)->first();
  62. if (!$order) {
  63. $response['msg'] = '找不到该订单';
  64. return $response;
  65. }
  66. $user_id = $order['user_id'];
  67. // $user_id = 7;
  68. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  69. $response['msg'] = '账号状态异常,暂不能退款!';
  70. return $response;
  71. }
  72. // 有未结账的订单是能不能退
  73. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($user_id)) {
  74. $response['msg'] = '您有未结算的订单,暂不能退款!';
  75. return $response;
  76. }
  77. $punish = $punishmentOrderRepository->checkNoPayModel($user_id);
  78. if (!$punish) {
  79. $response['msg'] = '您有罚单未支付,请先处理';
  80. return $response;
  81. }
  82. $user = User::query()->where('id', $user_id)->first();
  83. if ((int)$user->deposit_type === User::DEPOSIT_CARD) {
  84. $response['msg'] = '押金类型为免押金卡,不能退还押金';
  85. return $response;
  86. }
  87. if (!$order) {
  88. $response['msg'] = '你还没有缴纳押金';
  89. return $response;
  90. }
  91. if (RefundLog::query()->where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  92. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($user_id);
  93. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  94. $response['msg'] = $result['msg'];
  95. return $response;
  96. }
  97. }
  98. $refund = DB::transaction(function () use ($order, $user_id) {
  99. $refund = RefundLog::firstOrCreate([
  100. 'deposit_id' => $order->id,
  101. 'user_id' => $user_id,
  102. ], [
  103. 'no' => RefundLog::makeNo(),
  104. 'money' => $order->pay_money,
  105. 'type' => RefundLog::TYPE_USER,
  106. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  107. 'area_id' => $order->area_id,
  108. 'pay_type' => $order->pay_type,
  109. 'pay_money' => $order->pay_money,
  110. 'pay_status' => RefundLog::PAY_STATUS_NO
  111. ]);
  112. // 修改用户押金
  113. if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) {
  114. $refund->pay_status = RefundLog::PAY_STATUS_NO;
  115. $refund->type = RefundLog::TYPE_USER_SPEED;
  116. $refund->save();
  117. }
  118. return $refund;
  119. });
  120. //超期押金
  121. if (strtotime($order->pay_time) < strtotime(Carbon::now()->addYears(-1)->toDateTimeString())) {
  122. $auth = $user->auth;
  123. //超期退款
  124. $payment = app('wechat.payment'); // 微信支付
  125. $result = $payment->redpack->sendMiniprogramNormal([
  126. 'mch_billno' => $refund->no,
  127. 'send_name' => '未来GO',
  128. 're_openid' => $auth['credential'],
  129. 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
  130. 'wishing' => '退还超期押金',
  131. 'act_name' => '退还超期押金',
  132. 'remark' => '退还超期押金',
  133. ]);
  134. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'FAIL') {
  135. if ($result['err_code'] === 'SEND_FAILED') {
  136. $re = $payment->redpack->info($refund->no);
  137. log_record('查询', $re);
  138. if ($re['return_code'] === 'SUCCESS' && $re['result_code'] === 'SUCCESS') {
  139. switch ($re['status']) {
  140. //发放中
  141. case 'SENDING':
  142. $refund->remark = '超期押金退款红包发送中';
  143. break;
  144. //已发放待领取
  145. case 'SENT':
  146. $refund->remark = '超期押金退款红包已发放,请及时领取';
  147. break;
  148. //发放失败
  149. case 'FAILED':
  150. $refund->remark = '超期押金退款红包发放失败,请联系人工退还';
  151. $refund->pay_status = RefundLog::PAY_STATUS_ERROR;
  152. $refund->result = $re['err_code_des'];
  153. $refund->save();
  154. $result = $payment->redpack->sendMiniprogramNormal([
  155. 'mch_billno' => $refund->no . 'm',
  156. 'send_name' => '未来GO',
  157. 're_openid' => $auth['credential'],
  158. 'total_amount' => wechat_fee($order->pay_money), //单位为分,不小于100
  159. 'wishing' => '退还超期押金',
  160. 'act_name' => '退还超期押金',
  161. 'remark' => '退还超期押金',
  162. ]);
  163. break;
  164. //已领取
  165. case 'RECEIVED':
  166. $refund->pay_time = $re['hblist']['hbinfo']['rcv_time'];
  167. $refund->pay_status = RefundLog::PAY_STATUS_OK;
  168. $refund->save();
  169. $refund->refund_order_callback();
  170. //退款中 //已退款
  171. case 'RFUND_ING':
  172. case 'REFUND':
  173. $refund->remark = '超时未领取异常';
  174. $refund->pay_status = RefundLog::PAY_STATUS_ERROR;
  175. $refund->result = $re['err_code_des'];
  176. $refund->save();
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. log_record('发红包', $result);
  183. $refund->type = RefundLog::TYPE_RED_PACKET;
  184. $refund->result = $result['err_code_des'];
  185. $refund->save();
  186. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(1)));
  187. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(5)));
  188. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addMinutes(10)));
  189. $this->dispatch(new CheckRedOrderJob($refund->id, Carbon::now()->addHours(6)));
  190. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  191. $order->is_refund = DepositOrder::REFUND_OK;
  192. $order->save();
  193. }
  194. $ss = php2js($result);
  195. Log::error("
  196. ********红包1*************
  197. no: {$refund->no}
  198. result: {$ss}
  199. ");
  200. } else {
  201. $response['msg'] = '该订单不超期';
  202. return $response;
  203. }
  204. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  205. // 判断是否有免押金卡
  206. $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
  207. if (Carbon::now()->gte($deposit_expire_time)) {
  208. // 免押金卡已经过期
  209. User::where('id', $user_id)->update([
  210. 'deposit_money' => 0,
  211. 'is_deposit' => User::DEPOSIT_NO,
  212. 'deposit_type' => User::DEPOSIT_TYPE_NO
  213. ]);
  214. } else {
  215. // 免押金卡未过期
  216. User::where('id', $user_id)->update([
  217. 'deposit_money' => 0,
  218. 'is_deposit' => User::DEPOSIT_OK,
  219. 'deposit_type' => User::DEPOSIT_CARD
  220. ]);
  221. }
  222. $response['status'] = 1;
  223. $response['msg'] = $result['err_code_des'];
  224. return $response;
  225. } else {
  226. $refund->result = $result['err_code_des'];
  227. }
  228. $refund->save();
  229. $response['status'] = 0;
  230. $response['msg'] = $result['err_code_des'];
  231. return $response;
  232. } catch (\Exception $exception) {
  233. $response['status'] = 0;
  234. $response['msg'] = $exception->getMessage();
  235. return $response;
  236. }
  237. }
  238. }