From b05a3ea9116726673db338c781cd92f79cddff01 Mon Sep 17 00:00:00 2001 From: weigang Date: Thu, 10 Sep 2020 17:40:17 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E8=AE=A2=E5=8D=95ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 9 +++++---- app/Model/v3/OrderMain.php | 4 ++++ app/Model/v3/ShoppingCart.php | 3 ++- app/Service/v3/Implementations/OrderOnlineService.php | 8 +------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index f13b9cb..989833f 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -100,12 +100,13 @@ class OrderService implements OrderServiceInterface /** * 线上订单单笔退款 * 支持单商品、单店、整单 - * @param $global_order_id 全局总订单ID - * @param $child_order_id 主订单ID, - * @param $order_goods_id 订单商品ID + * @param $user_id *用户ID + * @param $global_order_id *全局总订单ID + * @param $child_order_id *主订单ID, + * @param $order_goods_id *订单商品ID * @param $note */ - public function onlineSingleRefund($global_order_id, $child_order_id, $order_goods_id, $note) + public function onlineSingleRefund($user_id, $global_order_id, $child_order_id, $order_goods_id, $note) { } diff --git a/app/Model/v3/OrderMain.php b/app/Model/v3/OrderMain.php index 9d4f06b..854043a 100644 --- a/app/Model/v3/OrderMain.php +++ b/app/Model/v3/OrderMain.php @@ -56,6 +56,10 @@ class OrderMain extends Model 'shipping_type_text', ]; + protected $casts = [ + 'global_order_id' => 'string' + ]; + public function getCreatedAtTextAttribute() { return date('Y-m-d H:i:s', $this->attributes['created_at']); diff --git a/app/Model/v3/ShoppingCart.php b/app/Model/v3/ShoppingCart.php index b07b814..e1d246c 100644 --- a/app/Model/v3/ShoppingCart.php +++ b/app/Model/v3/ShoppingCart.php @@ -3,10 +3,11 @@ namespace App\Model\v3; use App\Model\Model; +use Hyperf\Database\Model\SoftDeletes; class ShoppingCart extends Model { - + use SoftDeletes; protected $table = 'lanzu_shopping_cart'; protected $fillable = [ diff --git a/app/Service/v3/Implementations/OrderOnlineService.php b/app/Service/v3/Implementations/OrderOnlineService.php index b4f0e42..bbce864 100644 --- a/app/Service/v3/Implementations/OrderOnlineService.php +++ b/app/Service/v3/Implementations/OrderOnlineService.php @@ -244,13 +244,7 @@ class OrderOnlineService implements OrderOnlineServiceInterface // 校验订单总金额 if ($totalAmount != $totalMoney) { - throw new ErrorCodeException(ErrorCode::ORDER_TOTAL_AMOUNT_ERROR, json_encode([ - '计算的总订单金额:' => $totalAmount, - '前端的订单总金额:' => $totalMoney, - 'Delivery:' => $deliveryAmount, - 'Service:' => $serviceMoney, - 'Coupon:' => $couponMoney, - ])); + throw new ErrorCodeException(ErrorCode::ORDER_TOTAL_AMOUNT_ERROR); } $dataMain = [ From 3bd4068a48bac0f9ca270f884a1524e0d95513af Mon Sep 17 00:00:00 2001 From: weigang Date: Thu, 10 Sep 2020 20:36:30 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 39 ++++++++++++++++++- app/Model/v3/Category.php | 18 ++++----- app/Model/v3/OrderMain.php | 2 +- app/Request/v3/SearchGoodsRequest.php | 2 +- .../v3/Implementations/CategoryService.php | 15 ++++++- 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 989833f..60de8b1 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -6,6 +6,8 @@ use App\Commons\Log; use App\Constants\v3\ErrorCode; use App\Constants\v3\LogLabel; use App\Exception\ErrorCodeException; +use App\Model\v3\Order; +use App\Model\v3\OrderMain; use App\Service\v3\Interfaces\OrderOnlineServiceInterface; use App\Service\v3\Interfaces\SeparateAccountsServiceInterface; use Hyperf\DbConnection\Db; @@ -98,16 +100,49 @@ class OrderService implements OrderServiceInterface } /** - * 线上订单单笔退款 + * 线上订单单笔退款,主要用于后台强行操作退单退款 * 支持单商品、单店、整单 + * 按比例计算红包进行退款 + * 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券 + * 退2元商品时,退款金额为 + * 红包 :(2/(98+2))*10 = 0.2 + * 退款:2-0.2=1.8元 * @param $user_id *用户ID * @param $global_order_id *全局总订单ID * @param $child_order_id *主订单ID, * @param $order_goods_id *订单商品ID * @param $note */ - public function onlineSingleRefund($user_id, $global_order_id, $child_order_id, $order_goods_id, $note) + public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id=null, $order_goods_id=null) { + if (!$user_id || !$global_order_id || !$note) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对', + 'params' => json([$global_order_id, $user_id, $note]) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 主订单 + $orderMain = OrderMain::query()->where(['global_order_id' => $global_order_id])->first(); + + // 子订单 + if ($child_order_id) { + $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->first(); + } + + // 单商品退 + if ($order_goods_id) { + if (!$child_order_id) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对[单品]', + 'params' => json([$global_order_id, $user_id, $note, $child_order_id]) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + + } } } \ No newline at end of file diff --git a/app/Model/v3/Category.php b/app/Model/v3/Category.php index 7c3eaa6..1dda811 100644 --- a/app/Model/v3/Category.php +++ b/app/Model/v3/Category.php @@ -21,9 +21,7 @@ class Category extends Model protected $table = 'lanzu_category'; protected $appends = [ - 'goods_types', - 'cover_url', - 'goods_category_ids', + 'cover_url' ]; protected function boot(): void @@ -34,18 +32,18 @@ class Category extends Model }); } - public function getGoodsTypesAttribute() + public function getCoverUrlAttribute() { - return self::query()->where(['parent_id' => $this->attributes['id']])->orderBy('sort', 'desc')->get()->toArray(); + return $this->attachmentService->switchImgToAliOss($this->attributes['cover_img']); } - public function getGoodsCategoryIdsAttribute() + public function goodsTypes() { - return GoodsCategory::query()->where(['category_id' => $this->attributes['id']])->orderBy('sort', 'desc')->pluck('id'); + return $this->hasMany(self::class, 'parent_id', 'id')->with('goodsCategory'); } - - public function getCoverUrlAttribute() + + public function goodsCategory() { - return $this->attachmentService->switchImgToAliOss($this->attributes['cover_img']); + return $this->hasMany(GoodsCategory::class, 'category_id', 'id'); } } \ No newline at end of file diff --git a/app/Model/v3/OrderMain.php b/app/Model/v3/OrderMain.php index 854043a..01134a4 100644 --- a/app/Model/v3/OrderMain.php +++ b/app/Model/v3/OrderMain.php @@ -103,7 +103,7 @@ class OrderMain extends Model Order::class, 'order_main_id', 'order_id', - 'id', + 'global_order_id', 'id' ); } diff --git a/app/Request/v3/SearchGoodsRequest.php b/app/Request/v3/SearchGoodsRequest.php index 7b85add..31ec5bd 100644 --- a/app/Request/v3/SearchGoodsRequest.php +++ b/app/Request/v3/SearchGoodsRequest.php @@ -16,7 +16,7 @@ class SearchGoodsRequest extends BaseFormRequest { return [ 'market_id' => 'required|nonempty|integer', - 'type_id' => 'nonempty|integer', + 'type_id' => 'nonempty', 'store_id' => 'nonempty|integer', 'keyword' => 'nonempty', 'order_by' => 'nonempty|in:default,sales,price', diff --git a/app/Service/v3/Implementations/CategoryService.php b/app/Service/v3/Implementations/CategoryService.php index 6be32bd..039e532 100644 --- a/app/Service/v3/Implementations/CategoryService.php +++ b/app/Service/v3/Implementations/CategoryService.php @@ -32,7 +32,20 @@ class CategoryService implements CategoryServiceInterface public function all() { - return Category::query()->where(['parent_id' => 0])->get()->toArray(); + $categories = Category::query() + ->with(['goodsTypes']) + ->where(['parent_id' => 0])->get()->toArray(); + + foreach ($categories as $key => &$category) { + foreach ($category['goods_types'] as $key2 => &$item) { + $item['goods_category_ids'] = ''; + if (isset($item['goods_category']) && $item['goods_category']) { + $item['goods_category_ids'] = implode(',', array_values(array_column($item['goods_category'], 'id'))); + } + } + } + + return $categories; } public function allForStore($storeId) From 6e1bd1d3504c25f50d36d304608d5395cc14925e Mon Sep 17 00:00:00 2001 From: weigang Date: Thu, 10 Sep 2020 21:05:35 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BA=8C=E7=BA=A7=E4=B8=89=E7=BA=A7?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E6=90=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/v3/OrderState.php | 8 ++++++++ app/JsonRpc/OrderService.php | 14 +++++++++++++- app/Request/v3/SearchGoodsRequest.php | 1 + app/Service/v3/Implementations/SearchService.php | 5 +++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/Constants/v3/OrderState.php b/app/Constants/v3/OrderState.php index 718dffc..af1ff7c 100644 --- a/app/Constants/v3/OrderState.php +++ b/app/Constants/v3/OrderState.php @@ -75,6 +75,14 @@ class OrderState extends AbstractConstants */ const REFUND = [self::REFUNDING, self::REFUNDED, self::REFUND_REFUSE]; + /** + * @Message("可删除") + */ const CAN_DEL = [self::COMPLETED, self::EVALUATED, self::CANCELED, self::REFUNDED, self::REFUND_REFUSE]; + /** + * @Message("可强行退款") + */ + const CAN_REFUND_DIRECT = [self::COMPLETED, self::EVALUATED, self::REFUND_REFUSE]; + } \ No newline at end of file diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 60de8b1..4ea5298 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -5,6 +5,7 @@ namespace App\JsonRpc; use App\Commons\Log; use App\Constants\v3\ErrorCode; use App\Constants\v3\LogLabel; +use App\Constants\v3\OrderState; use App\Exception\ErrorCodeException; use App\Model\v3\Order; use App\Model\v3\OrderMain; @@ -124,7 +125,18 @@ class OrderService implements OrderServiceInterface } // 主订单 - $orderMain = OrderMain::query()->where(['global_order_id' => $global_order_id])->first(); + $orderMain = OrderMain::query() + ->where(['global_order_id' => $global_order_id]) + ->whereIn('state', OrderState::CAN_REFUND_DIRECT) + ->first(); + + if (empty($orderMain)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', + 'params' => json([$global_order_id, $user_id, $note]) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } // 子订单 if ($child_order_id) { diff --git a/app/Request/v3/SearchGoodsRequest.php b/app/Request/v3/SearchGoodsRequest.php index 31ec5bd..46ec354 100644 --- a/app/Request/v3/SearchGoodsRequest.php +++ b/app/Request/v3/SearchGoodsRequest.php @@ -17,6 +17,7 @@ class SearchGoodsRequest extends BaseFormRequest return [ 'market_id' => 'required|nonempty|integer', 'type_id' => 'nonempty', + 'goods_category_ids' => 'nonempty', 'store_id' => 'nonempty|integer', 'keyword' => 'nonempty', 'order_by' => 'nonempty|in:default,sales,price', diff --git a/app/Service/v3/Implementations/SearchService.php b/app/Service/v3/Implementations/SearchService.php index e1f5c84..6b6a0b1 100644 --- a/app/Service/v3/Implementations/SearchService.php +++ b/app/Service/v3/Implementations/SearchService.php @@ -23,6 +23,11 @@ class SearchService implements \App\Service\v3\Interfaces\SearchServiceInterface if (isset($params['type_id']) && $params['type_id']) { $typeIds = explode(',', $params['type_id']); + $builder->whereIn('category_id', $typeIds); + } + + if (isset($params['goods_category_ids']) && $params['goods_category_ids']) { + $typeIds = explode(',', $params['goods_category_ids']); $builder->whereIn('goods_category_id', $typeIds); } From af6607f17525c143df7cf3930060c498d65db0d0 Mon Sep 17 00:00:00 2001 From: lanzu_qinsheng <334039090@qq.com> Date: Fri, 11 Sep 2020 08:59:06 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E8=B0=83=E8=AF=95=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E9=80=80=E6=AC=BE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 1 - app/Service/v3/Implementations/OrderOnlineService.php | 1 - app/Service/v3/Implementations/PaymentService.php | 11 ++++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index c2d8f43..e10ffc3 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -77,7 +77,6 @@ class OrderService implements OrderServiceInterface Db::beginTransaction(); try { - $this->orderOnlineService->doRefund($global_order_id, $user_id); Db::commit(); diff --git a/app/Service/v3/Implementations/OrderOnlineService.php b/app/Service/v3/Implementations/OrderOnlineService.php index bbce864..0e65411 100644 --- a/app/Service/v3/Implementations/OrderOnlineService.php +++ b/app/Service/v3/Implementations/OrderOnlineService.php @@ -551,7 +551,6 @@ class OrderOnlineService implements OrderOnlineServiceInterface { $orderMain = $this->check($globalOrderId, $userId, OrderState::REFUNDING); - // 微信退款 if ($orderMain->pay_type == Payment::WECHAT) { return $this->paymentService->undo($orderMain->global_order_id, $userId); diff --git a/app/Service/v3/Implementations/PaymentService.php b/app/Service/v3/Implementations/PaymentService.php index 0f92373..d447fa8 100644 --- a/app/Service/v3/Implementations/PaymentService.php +++ b/app/Service/v3/Implementations/PaymentService.php @@ -45,7 +45,6 @@ class PaymentService implements PaymentServiceInterface ->where(['state' => OrderState::UNPAID, 'global_order_id' => $globalOrderId, 'user_id' => $userId]) ->where('created_at', '>=', (time()-900)) ->first(); - if (empty($orderMain)) { throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE, '[支付订单号]'.$globalOrderId); } @@ -112,10 +111,8 @@ class PaymentService implements PaymentServiceInterface // 已支付的,未退款的,使用微信支付的订单 $orderMain = OrderMain::query() ->whereIn('state', [OrderState::PAID, OrderState::DELIVERY, OrderState::COMPLETED, OrderState::EVALUATED, OrderState::REFUNDING]) - ->where(['global_order_id' => $globalOrderId, 'user_id' => $userId, 'pay_type' => Payment::WECHAT]) - ->whereRaw('refund_time is null') + ->where(['global_order_id' => $globalOrderId, 'user_id' => $userId, 'pay_type' => Payment::WECHAT,'refund_time'=>0]) ->first(); - if (empty($orderMain)) { throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE, '[支付订单号]'.$globalOrderId); } @@ -130,7 +127,11 @@ class PaymentService implements PaymentServiceInterface 'notify_url' => config('wechat.notify_url.refund'), ] ); - + if ($result['return_code']=="SUCCESS"&&isset($result['result_code'])&&$result['result_code']=="SUCCESS"){ + return true; + }else{ + throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL, '[退款失败]'.$result['err_code_des']); + } } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_PAYMENT_LOG, ['payment_do_exception_msg' => $e->getMessage()]); throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL, '[退款失败]'.$e->getMessage()); From e7402c02a3250a6e87731320a3d4be75bfd2ab95 Mon Sep 17 00:00:00 2001 From: lanzu_qinsheng <334039090@qq.com> Date: Fri, 11 Sep 2020 09:49:56 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E9=80=80=E6=AC=BE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 29 ++++++++++++------- .../v3/Implementations/PaymentService.php | 6 +--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 0e662a1..1f49932 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -81,15 +81,24 @@ class OrderService implements OrderServiceInterface Db::beginTransaction(); try { - $this->orderOnlineService->doRefund($global_order_id, $user_id); + $result = $this->orderOnlineService->doRefund($global_order_id, $user_id); Db::commit(); - return [ - "status" => 200, - "code" => 0, - "result" => [], - "message" => '处理成功' - ]; + if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") { + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; + } else { + return [ + "status" => 200, + "code" => -1, + "result" => [], + "message" => $result['err_code_des'] + ]; + } } catch (\Exception $e) { Db::rollBack(); @@ -109,11 +118,11 @@ class OrderService implements OrderServiceInterface * 退款:2-0.2=1.8元 * @param $user_id *用户ID * @param $global_order_id *全局总订单ID - * @param $child_order_id *主订单ID, - * @param $order_goods_id *订单商品ID + * @param $child_order_id *主订单ID, + * @param $order_goods_id *订单商品ID * @param $note */ - public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id=null, $order_goods_id=null) + public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id = null, $order_goods_id = null) { if (!$user_id || !$global_order_id || !$note) { $this->log->event(LogLabel::ORDER_REFUND_LOG, [ diff --git a/app/Service/v3/Implementations/PaymentService.php b/app/Service/v3/Implementations/PaymentService.php index d447fa8..e979635 100644 --- a/app/Service/v3/Implementations/PaymentService.php +++ b/app/Service/v3/Implementations/PaymentService.php @@ -127,11 +127,7 @@ class PaymentService implements PaymentServiceInterface 'notify_url' => config('wechat.notify_url.refund'), ] ); - if ($result['return_code']=="SUCCESS"&&isset($result['result_code'])&&$result['result_code']=="SUCCESS"){ - return true; - }else{ - throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL, '[退款失败]'.$result['err_code_des']); - } + return $result; } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_PAYMENT_LOG, ['payment_do_exception_msg' => $e->getMessage()]); throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL, '[退款失败]'.$e->getMessage()); From 9a825f4bb382e55683d4b08fe6349cf032c8e301 Mon Sep 17 00:00:00 2001 From: lanzu_qinsheng <334039090@qq.com> Date: Fri, 11 Sep 2020 11:18:09 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=AA=91=E8=A1=8C?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/LocationService.php | 32 ++++++++++++++++++++++++ app/JsonRpc/LocationServiceInterface.php | 10 ++++++++ config/autoload/dependencies.php | 1 + 3 files changed, 43 insertions(+) create mode 100644 app/JsonRpc/LocationService.php create mode 100644 app/JsonRpc/LocationServiceInterface.php diff --git a/app/JsonRpc/LocationService.php b/app/JsonRpc/LocationService.php new file mode 100644 index 0000000..bb9358f --- /dev/null +++ b/app/JsonRpc/LocationService.php @@ -0,0 +1,32 @@ +locationService->getDistanceByTencent($lng1, $lat1, $lng2, $lat2); + } +} \ No newline at end of file diff --git a/app/JsonRpc/LocationServiceInterface.php b/app/JsonRpc/LocationServiceInterface.php new file mode 100644 index 0000000..cf076bb --- /dev/null +++ b/app/JsonRpc/LocationServiceInterface.php @@ -0,0 +1,10 @@ + \App\Service\v3\Implementations\SmsAliService::class, \App\Service\v3\Interfaces\AttachmentServiceInterface::class => \App\Service\v3\Implementations\AttachmentService::class, \App\JsonRpc\PrintServiceInterface::class => \App\JsonRpc\FeieService::class, + \App\JsonRpc\LocationServiceInterface::class => \App\JsonRpc\LocationService::class, ]; From fee62e08180e7574786e1d36822252aa5a74fcce Mon Sep 17 00:00:00 2001 From: weigang Date: Fri, 11 Sep 2020 11:53:59 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=8B=86=E5=88=86=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E7=8A=B6=E6=80=813=E4=B8=BA=E5=B7=B2=E6=8E=A5=E5=8D=95/?= =?UTF-8?q?=E5=BE=85=E9=80=81=E8=BE=BE=EF=BC=8C=E4=BE=9D=E6=8D=AE=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E6=9C=89=E9=AA=91=E6=89=8BID=EF=BC=88=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=AE=89=E6=8E=92=E4=BA=86=E9=AA=91=E6=89=8B=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderService.php | 119 +++++++++++++++++++++++++++++++---- app/Model/v3/OrderMain.php | 6 ++ 2 files changed, 112 insertions(+), 13 deletions(-) diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 0e662a1..1a1b47b 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -8,10 +8,14 @@ use App\Constants\v3\LogLabel; use App\Constants\v3\OrderState; use App\Exception\ErrorCodeException; use App\Model\v3\Order; +use App\Model\v3\OrderGoods; use App\Model\v3\OrderMain; use App\Service\v3\Interfaces\OrderOnlineServiceInterface; use App\Service\v3\Interfaces\SeparateAccountsServiceInterface; +use EasyWeChat\Factory; +use EasyWeChat\Kernel\Exceptions\InvalidConfigException; use Hyperf\DbConnection\Db; +use Hyperf\Guzzle\CoroutineHandler; use Hyperf\RpcServer\Annotation\RpcService; use Hyperf\Di\Annotation\Inject; use function AlibabaCloud\Client\json; @@ -107,53 +111,142 @@ class OrderService implements OrderServiceInterface * 退2元商品时,退款金额为 * 红包 :(2/(98+2))*10 = 0.2 * 退款:2-0.2=1.8元 - * @param $user_id *用户ID + * @param $user_id *用户ID * @param $global_order_id *全局总订单ID - * @param $child_order_id *主订单ID, + * @param $order_child_id *主订单ID, * @param $order_goods_id *订单商品ID * @param $note + * @throws InvalidConfigException */ - public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id=null, $order_goods_id=null) + public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null) { + + $params = [ + 'user_id' => $user_id, + 'note' => $note, + 'global_order_id' => $global_order_id, + 'order_child_id' => $order_child_id, + 'order_goods_id' => $order_goods_id, + ]; + if (!$user_id || !$global_order_id || !$note) { $this->log->event(LogLabel::ORDER_REFUND_LOG, [ 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对', - 'params' => json([$global_order_id, $user_id, $note]) + 'params' => json_encode($params) ]); throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); } // 主订单 $orderMain = OrderMain::query() - ->where(['global_order_id' => $global_order_id]) + ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id]) ->whereIn('state', OrderState::CAN_REFUND_DIRECT) ->first(); - if (empty($orderMain)) { + if (is_null($orderMain)) { $this->log->event(LogLabel::ORDER_REFUND_LOG, [ 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', - 'params' => json([$global_order_id, $user_id, $note]) + 'params' => json_encode($params) ]); throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); } // 子订单 - if ($child_order_id) { - $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->first(); + $orderChild = null; + if ($order_child_id) { + $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first(); } - // 单商品退 + // 订单商品 + $orderGoods = null; if ($order_goods_id) { - if (!$child_order_id) { + if (!$order_child_id || is_null($orderChild)) { $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对[单品]', - 'params' => json([$global_order_id, $user_id, $note, $child_order_id]) + 'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]', + 'params' => json_encode($params) ]); throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); } + $orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first(); + + if (is_null($orderGoods)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + } + + $totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费 + $preRefundAmount = 0; // 预退款金额 + $refundAmount = 0; // 实际退款金额 + $refundType = 'Main'; // 退款类型, Main整单 Sub子单 Goods单品 + if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品 + $preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2); + $refundType = 'Goods'; + } elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单 + $preRefundAmount = $orderChild->money; + $refundType = 'Sub'; + } else { // 3. 再则如果存在主订单说明退主订单 + $preRefundAmount = $orderMain->total_money; + $refundType = 'Main'; + } + + // 占订单金额的比例 + $rate = bcdiv($preRefundAmount, $totalAmount, 6); + // 计算优惠券所占金额 + $couponMoney = bcmul($orderMain->coupon_money, $rate, 6); + // 计算本次退款实际退款金额 + $refundAmount = bcsub($preRefundAmount, $couponMoney, 2); + + if ($refundAmount <= 0) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + // 开始退款 + $config = config('wxpay'); + $app = Factory::payment($config); + $app['guzzle_handler'] = CoroutineHandler::class; + $result = $app->refund->byOutTradeNumber( + $orderMain->global_order_id, + $orderMain->global_order_id, + bcmul($orderMain->money, 100, 0), + bcmul($refundAmount, 100, 0), + [ + 'refund_desc' => '订单协商退款['.$refundType.']', + 'notify_url' => config('wechat.notify_url.refund_single'), + ] + ); + + if ($result['return_code'] == 'FAIL') { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'].'[微信退款失败]', + 'params' => json_encode($result) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); } + if ($result['result_code'] == 'FAIL') { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'].'[微信退款失败]'.$result['err_code'], + 'params' => json_encode($result) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 退款申请成功,查询退款状态 + $refundResult = $app->refund->queryByRefundId($result['refund_id']); + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]', + 'params' => json_encode($refundResult) + ]); + } } \ No newline at end of file diff --git a/app/Model/v3/OrderMain.php b/app/Model/v3/OrderMain.php index 01134a4..c3abfc8 100644 --- a/app/Model/v3/OrderMain.php +++ b/app/Model/v3/OrderMain.php @@ -72,6 +72,12 @@ class OrderMain extends Model public function getStateTextAttribute() { + if ($this->attributes['state'] == 3) { + if (!$this->attributes['horseman_id']) { + return '已接单'; + } + } + return OrderState::getMessage($this->attributes['state']); }