DepositOrderController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/19
  6. * Time: 2:11 PM
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Http\Requests\AreaIdRequest;
  10. use App\Jobs\CloseOrderJob;
  11. use App\Jobs\DepositRefundJob;
  12. use App\Models\AreaSetting;
  13. use App\Models\Auth;
  14. use App\Models\CouponsUserBag;
  15. use App\Models\DepositOrder;
  16. use App\Models\RefundLog;
  17. use App\Models\User;
  18. use App\Repositories\DepositOrderRepository;
  19. use App\Repositories\OrderRepository;
  20. use App\Repositories\PunishmentOrderRepository;
  21. use App\Repositories\RefundLogRepository;
  22. use App\Repositories\RentOrderRepository;
  23. use App\Repositories\UserRepository;
  24. use Carbon\Carbon;
  25. use Dingo\Api\Http\Request;
  26. use EasyWeChat\Factory;
  27. use Illuminate\Support\Facades\Log;
  28. use xin\helper\Secure;
  29. use function EasyWeChat\Kernel\Support\generate_sign;
  30. use Illuminate\Support\Facades\Cache;
  31. use Illuminate\Support\Facades\DB;
  32. use App\Library\Alipay\Alipay;
  33. /**
  34. * 押金管理模块
  35. * Class DepositOrderController
  36. * @package App\Http\Controllers\V1
  37. */
  38. class DepositOrderController extends BaseController
  39. {
  40. /**
  41. * 支付押金
  42. * @param Request $request
  43. * User: Mead
  44. */
  45. public function store(AreaIdRequest $request, DepositOrderRepository $depositOrderRepository, RefundLogRepository $refundLogRepository)
  46. {
  47. try {
  48. $area_id = $request->get('area_id');
  49. $payment_type = (int)($request->get('pay_type', self::PATMENT_TYPE_MINIAPP));//支付方式
  50. if (!in_array($payment_type, array_keys(self::$paymentTypeMaps))) return $this->errorNoValidation('支付方式不对');
  51. $is_deposit = AreaSetting::where('area_id', $area_id)->value('is_deposit');
  52. $user_id = $this->user->id;
  53. if ($user_id != 3) {
  54. if (!(int)$is_deposit) {
  55. return $this->errorNoValidation('该区域免押金骑行');
  56. }
  57. }
  58. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk_alipay_wxapp($user_id); //查询退款
  59. if (!$result['status']) {
  60. if ($result['code'] === 'REFUNDNOTEXIST') { //交易退款查询失败
  61. //发起退款
  62. $refund = $refundLogRepository->byUserIdUserRefundNoModel($user_id);
  63. $order = $depositOrderRepository->byIdGetModel($refund->deposit_id);
  64. switch ($refund['pay_type']) {
  65. case RefundLog::PAY_TYPE_WECHAT: //微信
  66. $payment = Factory::payment(wechat_pay_config(self::$MERCHANT)); // 微信支付
  67. $coderesult = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  68. // 可在此处传入其他参数,详细参数见微信支付文档
  69. 'refund_desc' => '用户申请退回押金',
  70. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify/' . self::$MERCHANT['id'],
  71. ]);
  72. break;
  73. case RefundLog::PAY_TYPE_ALIPAYMINI: //支付宝
  74. $no = $order->no;
  75. $money = $order->pay_money;
  76. $data = alipay_mini_config(self::$MERCHANT)->payment()->common()->refund($no, $money);
  77. if ($data->fundChange == 'Y') {
  78. $coderesult['return_code'] = 'SUCCESS';
  79. $coderesult['result_code'] = 'SUCCESS';
  80. //支付宝没有退款异步回调 这里写下
  81. $order->is_refund = DepositOrder::REFUND_OK;
  82. $order->save();
  83. $order->refund_order_callback(); //押金退款订单回调
  84. } else {
  85. $coderesult['return_code'] = 'ERROE';
  86. $coderesult['result_code'] = 'ERROE';
  87. $coderesult['err_code_des'] = "调用失败,原因:可能已经退款 请确认";
  88. }
  89. break;
  90. case RefundLog::PAY_TYPE_ALIPAYMINI_CREDIT:
  91. $money = $order->pay_money;
  92. $alipayobj1 = new Alipay();
  93. $resultdata = $alipayobj1->zijidongjieJiedong($result['data']); // 预授权解冻
  94. if ($resultdata->status == 'SUCCESS' && $resultdata->amount == $money) {
  95. $coderesult['return_code'] = 'SUCCESS';
  96. $coderesult['result_code'] = 'SUCCESS';
  97. //支付宝没有退款异步回调 这里写下
  98. $order->is_refund = DepositOrder::REFUND_OK;
  99. $order->save();
  100. $order->refund_order_callback(); //押金退款订单回调
  101. } else {
  102. $coderesult['return_code'] = 'ERROE';
  103. $coderesult['result_code'] = 'ERROE';
  104. $coderesult['err_code_des'] = json_encode($resultdata);
  105. }
  106. break;
  107. default:
  108. return $this->errorNoValidation('退款类型错误');
  109. }
  110. if ($coderesult['return_code'] === 'SUCCESS' && $coderesult['result_code'] === 'SUCCESS') {
  111. // 判断是否有免押金卡
  112. $re = CouponsUserBag::query()->where('user_id', $user_id)
  113. ->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)
  114. ->where('valid_end_time', '<=', Carbon::now()->toDateTimeString())
  115. ->where('status', CouponsUserBag::STATUS_OK)->exists();
  116. if ($re) {
  117. //是否有免押金劵
  118. User::where('id', $user_id)->update([
  119. 'deposit_money' => 0,
  120. 'is_deposit' => User::DEPOSIT_OK,
  121. 'deposit_type' => User::DEPOSIT_COUPON
  122. ]);
  123. } else {
  124. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  125. if (Carbon::now()->gte($deposit_expire_time)) {
  126. // 免押金卡已经过期
  127. User::where('id', $user_id)->update([
  128. 'deposit_money' => 0,
  129. 'is_deposit' => User::DEPOSIT_NO,
  130. 'deposit_type' => User::DEPOSIT_TYPE_NO
  131. ]);
  132. } else {
  133. // 免押金卡未过期
  134. User::where('id', $user_id)->update([
  135. 'deposit_money' => 0,
  136. 'is_deposit' => User::DEPOSIT_OK,
  137. 'deposit_type' => User::DEPOSIT_CARD
  138. ]);
  139. }
  140. }
  141. } else {
  142. $refund->result = $coderesult['err_code_des'];
  143. $refund->save();
  144. return $this->errorNoValidation($refund->result);
  145. }
  146. } else {
  147. return $this->errorNoValidation($result['msg']);
  148. }
  149. }
  150. $cache_key = "DEPOSIT_ORDER_{$user_id}";
  151. if (Cache::has($cache_key)) {
  152. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  153. }
  154. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  155. $order = $depositOrderRepository->byUserIdGetUserDepositOrder($user_id);
  156. if ($order) {
  157. return $this->errorNoValidation('您已经缴纳押金!');
  158. }
  159. $money = AreaSetting::where('area_id', $area_id)->value('deposit');
  160. // 开启一个数据库事务
  161. switch (self::$SOURCE_TYPE) {
  162. case self::SOURCE_TYPE_WECHAT:
  163. $source_type = DepositOrder::PAY_TYPE_WECHAT;
  164. break;
  165. case self::SOURCE_TYPE_ALIPAY:
  166. if ($payment_type == self::PATMENT_TYPE_MINIAPP) {
  167. $source_type = DepositOrder::PAY_TYPE_ALIPAYMINI;
  168. } elseif ($payment_type == self::PATMENT_TYPE_ALIPAY_CARD) {
  169. $source_type = DepositOrder::PAY_TYPE_ALIPAYMINI_CREDIT;
  170. }
  171. break;
  172. }
  173. $data = [
  174. 'money' => $money,
  175. 'user_id' => $user_id,
  176. 'no' => DepositOrder::makeNo(self::$ORDER_TAG),
  177. 'rno' => DepositOrder::makeNo(self::$ORDER_TAG),
  178. 'area_id' => $area_id,
  179. 'pay_status' => DepositOrder::PAY_STATUS_NO,
  180. 'pay_money' => $money,
  181. 'pay_type' => $source_type,
  182. 'merchant_id' => self::$MERCHANT_ID
  183. ];
  184. $order = DepositOrder::create($data);
  185. if (!$order) {
  186. return $this->errorNoValidation('订单错误');
  187. }
  188. $this->dispatch(new CloseOrderJob($order, Carbon::now()->addMinutes(30)));
  189. $auth = $this->user->auth;
  190. $userauth = userAuthinfo(self::$MERCHANT, $user_id);
  191. if ($userauth['code'] != 1) {
  192. return $this->errorNoValidation($userauth['msg']);
  193. }
  194. switch (self::$SOURCE_TYPE) {
  195. case self::SOURCE_TYPE_WECHAT: //DepositOrder::PAY_TYPE_WECHAT
  196. // 微信支付
  197. $payment = Factory::payment(wechat_pay_config(self::$MERCHANT));
  198. $result = $payment->order->unify([
  199. 'body' => "用户缴纳押金-" . self::$MERCHANT['wxapp_name'],
  200. 'out_trade_no' => $order->no,
  201. 'trade_type' => 'JSAPI', // 必须为JSAPI
  202. 'openid' => $userauth['data']['credential'], // 这里的openid为付款人的openid
  203. 'total_fee' => wechat_fee($order->pay_money), // 总价
  204. 'attach' => makeNoTag(DepositOrder::NO_TAG)
  205. ]);
  206. // 如果成功生成统一下单的订单,那么进行二次签名
  207. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  208. // 二次签名的参数必须与下面相同
  209. $params = [
  210. 'appId' => $auth['identifier'],
  211. 'timeStamp' => time(),
  212. 'nonceStr' => $result['nonce_str'],
  213. 'package' => 'prepay_id=' . $result['prepay_id'],
  214. 'signType' => 'MD5',
  215. ];
  216. $params['paySign'] = generate_sign($params, self::$MERCHANT['pay_key']);
  217. return $this->response->array($params);
  218. }
  219. break;
  220. case self::SOURCE_TYPE_ALIPAY: //DepositOrder::PAY_TYPE_ALIPAYMINI
  221. if ($payment_type == self::PATMENT_TYPE_MINIAPP) {
  222. //支付宝支付
  223. $money = $order->pay_money;
  224. $result = alipay_mini_pay(self::$MERCHANT, "用户缴纳押金-" . self::$MERCHANT['wxapp_name'], $order->no, $money, $userauth['data']['credential']);
  225. if ($result['code'] != 1) {
  226. return $this->errorNoValidation($result['msg']);
  227. }
  228. $response['tradeNo'] = $result['data']->tradeNo; //获取支付宝订单号
  229. return $this->response->array($response);
  230. } elseif ($payment_type == self::PATMENT_TYPE_ALIPAY_CARD) {
  231. //支付宝预授权资金冻结免押认证
  232. $merchant = self::$MERCHANT;
  233. $alipayobj = new Alipay();
  234. $money = $order->pay_money;
  235. $data = [
  236. //公共参数
  237. 'appId' => $merchant['alipaymini_appId'],
  238. 'rsaPrivateKey' => $merchant['alipaymini_merchantPrivateKey'],
  239. // 'appCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'],
  240. // 'alipayCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'],
  241. // 'rootCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'],
  242. 'appCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'],
  243. 'alipayCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayCertPath'],
  244. 'rootCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath'],
  245. //异步回调
  246. 'notify_url' => config('app.url') . '/api/alipay_yushouquan/' . $merchant['id'],
  247. //请求参数
  248. 'out_order_no' => $order->no, //商户授权资金订单号 ,
  249. 'out_request_no' => $order->rno,//商户本次资金操作的请求流水号,用于标示请求流水的唯一性
  250. 'order_title' => '预授权用户骑行押金',
  251. 'amount' => $money,
  252. 'product_code' => 'PRE_AUTH_ONLINE',
  253. 'payee_user_id' => $merchant['alipaymini_pid'],
  254. ];
  255. $result = $alipayobj->zijidongjie_cret($data); // 证书公钥
  256. $response['tradeNo'] = $result; //获取支付宝订单号
  257. return $this->response->array($response);
  258. }
  259. break;
  260. default:
  261. return $this->errorNoValidation('没有有效的支付方式');
  262. }
  263. return $this->errorNoValidation('下单失败');
  264. } catch (\Exception $exception) {
  265. Log::error($exception);
  266. return $this->errorException($exception->getMessage());
  267. }
  268. }
  269. /**
  270. * 退押金 队列
  271. * @param OrderRepository $orderRepository
  272. * @param RentOrderRepository $rentOrderRepository
  273. * @param RefundLogRepository $refundLogRepository
  274. * @param PunishmentOrderRepository $punishmentOrderRepository
  275. * Author: Mead
  276. */
  277. public function refundJob(Request $request, OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  278. {
  279. try {
  280. $user_id = $this->user->id;
  281. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  282. return $this->errorNoValidation('账号状态异常,暂不能退款!');
  283. }
  284. // 有未结账的订单是能不能退
  285. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  286. return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  287. }
  288. if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  289. return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  290. }
  291. if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  292. return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  293. }
  294. $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  295. if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  296. if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  297. return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  298. }
  299. $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();
  300. if (!$order) {
  301. return $this->errorNoValidation('你还没有缴纳押金');
  302. }
  303. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_WAIT)->exists()) {
  304. return $this->response()->array([
  305. 'type' => 2
  306. ]);
  307. }
  308. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->exists()) {
  309. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  310. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  311. return $this->errorNoValidation($result['msg']);
  312. }
  313. }
  314. $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  315. if (Cache::has($cache_key)) {
  316. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  317. }
  318. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  319. $refund = DB::transaction(function () use ($order) {
  320. $refund = RefundLog::firstOrCreate([
  321. 'deposit_id' => $order->id,
  322. 'user_id' => $this->user->id,
  323. ], [
  324. 'no' => RefundLog::makeNo(self::$ORDER_TAG),
  325. 'money' => $order->pay_money,
  326. 'type' => RefundLog::TYPE_USER,
  327. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  328. 'area_id' => $order->area_id,
  329. 'pay_type' => $order->pay_type,
  330. 'pay_money' => $order->pay_money,
  331. 'pay_status' => RefundLog::PAY_STATUS_WAIT,
  332. 'merchant_id' => self::$MERCHANT_ID
  333. ]);
  334. // 修改用户押金
  335. return $refund;
  336. });
  337. // $this->dispatch(new DepositRefundJob($refund, Carbon::now()->addMinutes(5)));
  338. $this->dispatch(new DepositRefundJob($refund, Carbon::now()->addMinutes(1)));
  339. return $this->response()->array([
  340. 'type' => 1
  341. ]);
  342. // return $this->errorNoValidation($refund->result);
  343. } catch (\Exception $exception) {
  344. return $this->exception($exception);
  345. }
  346. }
  347. /**
  348. * 退还押金 马上
  349. * @param DepositOrderRepository $depositOrderRepository
  350. * User: Mead
  351. */
  352. public function refund(Request $request, OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, RefundLogRepository $refundLogRepository, PunishmentOrderRepository $punishmentOrderRepository, UserRepository $userRepository)
  353. {
  354. try {
  355. $user_id = $this->user->id;
  356. if (!$userRepository->byIdCheckStatusOk($user_id)) {
  357. return $this->errorNoValidation('账号状态异常,暂不能退款!');
  358. }
  359. // 有未结账的订单是能不能退
  360. if ($orderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  361. return $this->errorNoValidation('您有未结算的订单,暂不能退款!');
  362. }
  363. if ($rentOrderRepository->byUserIdCheckIsExistRideOrder($this->user->id)) {
  364. return $this->errorNoValidation('您有未完成的租车订单,请先处理');
  365. }
  366. if ($rentOrderRepository->byUserIdCheckIsExistNoPayOrder($this->user->id)) {
  367. return $this->errorNoValidation('您有未支付的租车订单,请先处理');
  368. }
  369. $punish = $punishmentOrderRepository->checkNoPayModel($this->user->id);
  370. if (!$punish) return $this->errorNoValidation('您有罚单未支付,请先处理');
  371. if ((int)$this->user->deposit_type === User::DEPOSIT_CARD) {
  372. return $this->errorNoValidation('押金类型为免押金卡,不能退还押金');
  373. }
  374. $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();
  375. if (!$order) {
  376. return $this->errorNoValidation('你还没有缴纳押金');
  377. }
  378. if (RefundLog::where('deposit_id', $order->id)->where('pay_status', RefundLog::PAY_STATUS_NO)->where('pay_type', DepositOrder::PAY_TYPE_WECHAT)->exists()) {
  379. $result = $refundLogRepository->byUserIdCheckUserIsRefundOk($this->user->id);
  380. if (!$result['status'] && $result['code'] !== 'REFUNDNOTEXIST') {
  381. return $this->errorNoValidation($result['msg']);
  382. }
  383. }
  384. $cache_key = "REFUND_DEPOSIT_ORDER_{$user_id}";
  385. if (Cache::has($cache_key)) {
  386. return $this->errorNoValidation('您提交的太频繁了,请一会再提交!');
  387. }
  388. Cache::put($cache_key, 1, Carbon::now()->addSeconds(5));
  389. $refund = DB::transaction(function () use ($order) {
  390. $refund = RefundLog::firstOrCreate([
  391. 'deposit_id' => $order->id,
  392. 'user_id' => $this->user->id,
  393. ], [
  394. 'no' => RefundLog::makeNo(self::$ORDER_TAG),
  395. 'money' => $order->pay_money,
  396. 'type' => RefundLog::TYPE_USER,
  397. 'is_check_status' => RefundLog::CHECK_STATUS_OK,
  398. 'area_id' => $order->area_id,
  399. 'pay_type' => $order->pay_type,
  400. 'pay_money' => $order->pay_money,
  401. 'pay_status' => RefundLog::PAY_STATUS_NO,
  402. 'merchant_id' => self::$MERCHANT_ID
  403. ]);
  404. // 修改用户押金
  405. if ((int)$refund->pay_status === RefundLog::PAY_STATUS_WAIT) {
  406. $refund->pay_status = RefundLog::PAY_STATUS_NO;
  407. $refund->type = RefundLog::TYPE_USER_SPEED;
  408. $refund->save();
  409. }
  410. return $refund;
  411. });
  412. switch ($order->pay_type) {
  413. case DepositOrder::PAY_TYPE_WECHAT: //用的是支付押金时候的支付方式
  414. $payment = Factory::payment(wechat_pay_config(self::$MERCHANT)); // 微信支付
  415. $result = $payment->refund->byOutTradeNumber($order->no, $refund->no, wechat_fee($order->pay_money), wechat_fee($refund->pay_money), [
  416. // 可在此处传入其他参数,详细参数见微信支付文档
  417. 'refund_desc' => '用户申请退回押金',
  418. 'notify_url' => config('app.url') . '/api/payments/wechat-refund-notify/' . self::$MERCHANT['id'],
  419. ]);
  420. break;
  421. case DepositOrder::PAY_TYPE_ALIPAYMINI: //用的是支付押金时候的支付方式 原路退回
  422. $no = $order->no;
  423. $money = $order->pay_money;
  424. $data = alipay_mini_config(self::$MERCHANT)->payment()->common()->optional("out_request_no", $refund->no)->refund($no, $money);
  425. if ($data->fundChange == 'Y') {
  426. $result['return_code'] = 'SUCCESS';
  427. $result['result_code'] = 'SUCCESS';
  428. //支付宝没有退款异步回调 这里写下
  429. $order->is_refund = DepositOrder::REFUND_OK;
  430. $order->save();
  431. $order->refund_order_callback(); //押金退款订单回调
  432. } else {
  433. $result['return_code'] = 'ERROE';
  434. $result['result_code'] = 'ERROE';
  435. $result['err_code_des'] = "调用失败,原因:可能已经退款 请确认";
  436. }
  437. break;
  438. case DepositOrder::PAY_TYPE_ALIPAYMINI_CREDIT:
  439. //支付宝押金预授权解冻
  440. $money = $order->pay_money;
  441. $merchant = self::$MERCHANT;
  442. $alipayobj = new Alipay();
  443. $data = AlipayYushouquanJiedong($merchant, $order->no, $order->rno);
  444. $resultdata = $alipayobj->AlipayQueryYushouquan($data); // 预授权查询
  445. if(!empty($resultdata->code) && $resultdata->code==10000 && $resultdata->status=='SUCCESS') {
  446. if ($resultdata->rest_amount == '0.00') {
  447. return $this->errorNoValidation('已经解冻完所有金额!!');
  448. }
  449. }else{
  450. return $this->errorNoValidation($resultdata->sub_msg);
  451. }
  452. $data = AlipayYushouquanJiedongQingqiu($data, $resultdata->auth_no, $refund->no, $resultdata->rest_amount);
  453. $resultdata = $alipayobj->zijidongjieJiedong($data); // 预授权解冻
  454. if ($resultdata === false) {
  455. return $this->errorNoValidation('预授权解冻失败,请联系管理员');
  456. }
  457. if ($resultdata->status == 'SUCCESS' && $resultdata->amount == $money) {
  458. $result['return_code'] = 'SUCCESS';
  459. $result['result_code'] = 'SUCCESS';
  460. $order->is_refund = DepositOrder::REFUND_OK;
  461. $order->save();
  462. $order->refund_order_callback(); //押金退款订单回调
  463. } else {
  464. $result['return_code'] = 'ERROE';
  465. $result['result_code'] = 'ERROE';
  466. $result['err_code_des'] = json_encode($resultdata);
  467. }
  468. break;
  469. default:
  470. return $this->errorNoValidation('退款方式错误');
  471. }
  472. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  473. // 判断是否有免押金卡
  474. $re = CouponsUserBag::query()->where('user_id', $user_id)
  475. ->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)
  476. ->where('valid_end_time', '<=', Carbon::now()->toDateTimeString())
  477. ->where('status', CouponsUserBag::STATUS_OK)->exists();
  478. if ($re) {
  479. //是否有免押金劵
  480. User::where('id', $user_id)->update([
  481. 'deposit_money' => 0,
  482. 'is_deposit' => User::DEPOSIT_OK,
  483. 'deposit_type' => User::DEPOSIT_COUPON
  484. ]);
  485. } else {
  486. $deposit_expire_time = Carbon::parse($this->user->deposit_expire_time);
  487. if (Carbon::now()->gte($deposit_expire_time)) {
  488. // 免押金卡已经过期
  489. User::where('id', $this->user->id)->update([
  490. 'deposit_money' => 0,
  491. 'is_deposit' => User::DEPOSIT_NO,
  492. 'deposit_type' => User::DEPOSIT_TYPE_NO
  493. ]);
  494. } else {
  495. // 免押金卡未过期
  496. User::where('id', $this->user->id)->update([
  497. 'deposit_money' => 0,
  498. 'is_deposit' => User::DEPOSIT_OK,
  499. 'deposit_type' => User::DEPOSIT_CARD
  500. ]);
  501. }
  502. }
  503. return $this->response->array([
  504. 'is_refund_status' => true
  505. ]);
  506. } else {
  507. $refund->result = $result['err_code_des'];
  508. }
  509. $refund->save();
  510. return $this->errorNoValidation($refund->result);
  511. } catch (\Exception $exception) {
  512. return $this->exception($exception);
  513. }
  514. }
  515. }