OrderService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Services\Pay;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\ApiException;
  5. use App\Models\OrderModel;
  6. use App\Services\Service;
  7. class OrderService extends Service
  8. {
  9. /**
  10. * 企业付款到零钱
  11. * @param int $uid
  12. * @param int $pay_fee 单位:分
  13. * @param string $desc 描述
  14. * @return bool
  15. * @throws \ApiException
  16. */
  17. public function payments(int $uid, int $pay_fee, string $desc = "口袋高校提现")
  18. {
  19. $pay_url = config('api.pay_url') . "/payments/minitransfer";
  20. $params = [
  21. 'uid' => $uid,
  22. 'amount' => $pay_fee,
  23. 'app_order_id' => "share_payments_" . uniqid(),
  24. 'desc' => $desc
  25. ];
  26. $result = \Curl::to($pay_url)->withData($params)->asJsonResponse()->post();
  27. if (isset($result->code) && $result->code == 200) {
  28. return true;
  29. } else {
  30. throw new \ApiException($result->message, $result->code);
  31. }
  32. }
  33. /**
  34. * 订单退款
  35. * @param int $order_id 订单ID
  36. * @param int $refund_fee 退款金额
  37. * @param string $refund_desc 退款描述
  38. * @param bool $all
  39. * @return bool
  40. * @throws AlertException
  41. * @throws ApiException
  42. */
  43. public function refund(int $order_id, int $refund_fee, $refund_desc = "", $all = false)
  44. {
  45. $order = OrderModel::find($order_id);
  46. if (collect($order)->isEmpty()) {
  47. throw new ApiException('支付订单错误或不存在', 404);
  48. }
  49. if ($all && $order->create_at < 1533114000) {
  50. throw new AlertException("过期订单,请联系小向导微信:fpdxkf", 103);
  51. } else {
  52. $pay_url = config('api.pay_url') . "/payments/order/{$order->out_trade_no}/refund";
  53. $params = [
  54. 'refund_fee' => $refund_fee,
  55. 'refund_desc' => $refund_desc
  56. ];
  57. }
  58. $result = \Curl::to($pay_url)->withData($params)->asJsonResponse()->post();
  59. if (isset($result->code) && $result->code == 200) {
  60. // 使用优惠券逻辑
  61. if ($order->coupon_id) {
  62. $couponService = new CouponService();
  63. $couponService->setUid($order->uid)
  64. ->setCouponId($order->coupon_id)
  65. ->unUse();
  66. }
  67. return true;
  68. } else {
  69. throw new AlertException(isset($result->message) ? $result->message : "订单退款异常", 500);
  70. }
  71. }
  72. /**
  73. * 订单取消支付
  74. * @param int $order_id
  75. * @return bool
  76. * @throws AlertException
  77. * @throws ApiException
  78. */
  79. public function closeOrder(int $order_id)
  80. {
  81. $order = OrderModel::find($order_id);
  82. if (!$order) {
  83. throw new ApiException("订单不存在", 404001);
  84. }
  85. $pay_url = config('api.pay_url') . "/payments/order/{$order->out_trade_no}/close";
  86. $result = \Curl::to($pay_url)->asJsonResponse()->put();
  87. if (isset($result->code) && $result->code == 200) {
  88. // 置为关闭状态
  89. $order->order_state = 3;
  90. $order->save();
  91. // 是否使用优惠券
  92. if ($order->coupon_id) {
  93. $couponService = new CouponService();
  94. $couponService->setUid($order->uid)
  95. ->setCouponId($order->coupon_id)
  96. ->unUse();
  97. }
  98. return true;
  99. } else {
  100. throw new ApiException(
  101. isset($result->message) ? $result->message : "取消订单异常[{$order_id}]",
  102. 500
  103. );
  104. }
  105. }
  106. }