From b1e56e7c2c1c5d86a2aeb045bdf85c7ad5e09f47 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Tue, 15 Sep 2020 20:08:25 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/v3/Implementations/FeiePrintService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Service/v3/Implementations/FeiePrintService.php b/app/Service/v3/Implementations/FeiePrintService.php index 91ef906..a248fdf 100644 --- a/app/Service/v3/Implementations/FeiePrintService.php +++ b/app/Service/v3/Implementations/FeiePrintService.php @@ -68,7 +68,7 @@ class FeiePrintService implements FeiePrintServiceInterface $content = $this->printFormat($data, 4, 14, 7, 7); $res = $this->printMsg('920527381', $content, 1); - return $content; + return $res; } /** From 8c47a1955ef80819f0f5f2ed8d622b92c0badf5a Mon Sep 17 00:00:00 2001 From: weigang Date: Wed, 16 Sep 2020 09:14:08 +0800 Subject: [PATCH 2/3] no message --- app/Service/v3/Implementations/StoreService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Service/v3/Implementations/StoreService.php b/app/Service/v3/Implementations/StoreService.php index ba84527..8389ae4 100644 --- a/app/Service/v3/Implementations/StoreService.php +++ b/app/Service/v3/Implementations/StoreService.php @@ -50,6 +50,6 @@ class StoreService implements StoreServiceInterface return Store::query() ->with('market') ->where('id',$storeId) - ->first()->toArray(); + ->first(); } } \ No newline at end of file From 29c0995ae9ecf4362b4f28cb065fe10a19236c4e Mon Sep 17 00:00:00 2001 From: weigang Date: Wed, 16 Sep 2020 10:08:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=9B=9E=E5=A4=8Dv1=E7=9A=84=20rpcservice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/JsonRpc/OrderOnlineService.php | 330 ++++++++++++++++++++ app/JsonRpc/OrderOnlineServiceInterface.php | 8 + app/JsonRpc/OrderService.php | 308 +++--------------- config/autoload/dependencies.php | 1 + 4 files changed, 374 insertions(+), 273 deletions(-) create mode 100644 app/JsonRpc/OrderOnlineService.php create mode 100644 app/JsonRpc/OrderOnlineServiceInterface.php diff --git a/app/JsonRpc/OrderOnlineService.php b/app/JsonRpc/OrderOnlineService.php new file mode 100644 index 0000000..82631e9 --- /dev/null +++ b/app/JsonRpc/OrderOnlineService.php @@ -0,0 +1,330 @@ +orderOnlineService->doComplete($global_order_id, $user_id); + $this->separateAccountsService->orderOnlineCompleted($global_order_id, $user_id); + + Db::commit(); + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; + } catch (\Exception $e) { + + Db::rollBack(); + $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]); + return [ + "status" => 200, + "code" => $e->getCode() ?? ErrorCode::ORDER_COMPLETE_FAIL, + "result" => [], + "message" => $e->getMessage() + ]; + } + + } + + /** + * 线上订单退款,整个订单退,这个是专门用于处理用户的申请退款的同意退款操作 + * @param $global_order_id + * @param $user_id + * @return array + */ + public function onlineRefund($global_order_id, $user_id) + { + try { + $this->orderOnlineService->doRefund($global_order_id, $user_id); + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; + } catch (\Exception $e) { + return [ + "status" => 200, + "code" => $e->getCode(), + "result" => [], + "message" => $e->getMessage() + ]; + } + + } + + /** + * 线上订单单笔退款,主要用于后台强行操作退单退款 + * 支持单商品、单店、整单 + * 按比例计算红包进行退款 + * 比如:两个子订单和子订单商品,分别是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 $order_child_id *主订单ID, + * @param $order_goods_id *订单商品ID + * @param $note + * @return array + */ + public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null) + { + + Db::beginTransaction(); + try { + + $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_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 主订单 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id]) + ->whereIn('state', OrderState::CAN_REFUND_DIRECT) + ->first(); + + if (is_null($orderMain)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', + 'params' => json_encode($params) + ]); + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + // 子订单 + $orderChild = null; + if ($order_child_id) { + $orderChild = Order::query()->with('store:user_id')->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first(); + } + + // 订单商品 + $orderGoods = null; + if ($order_goods_id) { + if (!$order_child_id || is_null($orderChild)) { + $this->log->event(LogLabel::ORDER_REFUND_LOG, [ + '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) + ]); + + // 退款成功 + if ( + !($refundResult['return_code'] == 'SUCCESS' + && isset($refundResult['result_code']) + && $refundResult['result_code'] == 'SUCCESS') + ) { + throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); + } + + $currentTime = time(); + // 处理订单状态 + $orderMain->state = OrderState::REFUNDED; + $orderMain->total_refund_note = $note; + $orderMain->refund_time = $currentTime; + $orderMain->save(); + + if ($refundType == 'sub') { + $orderChild->status = 3; + $orderChild->refund_note = $note; + $orderChild->refund_time = $currentTime; + $orderChild->save(); + } elseif ($refundType == 'goods') { + $orderGoods->status = 3; + $orderGoods->refund_note = $note; + $orderGoods->refund_time = $currentTime; + $orderGoods->save(); + } + + // 处理用户和商户流水 + if ($refundType == 'main') { # 整单退的话还得处理所有子订单的商户 + + $orderChildren = Order::query()->with('store:user_id')->where(['order_main_id' => $orderMain->global_order_id])->get(); + foreach ($orderChildren as $key => &$order) { + // 占订单金额的比例 + $rate = bcdiv($order->money, $totalAmount, 6); + // 计算优惠券所占金额 + $couponMoney = bcmul($orderMain->coupon_money, $rate, 6); + // 计算本次退款实际退款金额 + $refundStoreAmount = bcsub($order->money, $couponMoney, 2); + $this->financialRecordService->storeRefundDirect($order->store->user_id, $order->id, $refundStoreAmount); + } + + } elseif ($refundType == 'sub') { # 退子订单或者退单品的话,商户只有一个 + $this->financialRecordService->storeRefundDirect($orderChild->store->user_id, $orderChild->id, $refundAmount); + } + + $this->financialRecordService->userRefundDirect($orderMain->user_id, $orderMain->global_order_id, $refundAmount); + + Db::commit(); + return [ + "status" => 200, + "code" => 0, + "result" => [], + "message" => '处理成功' + ]; + + } catch (\Exception $e) { + Db::rollBack(); + return [ + "status" => 200, + "code" => $e->getCode(), + "result" => [], + "message" => '[退款失败]'.$e->getMessage() + ]; + } + + } +} \ No newline at end of file diff --git a/app/JsonRpc/OrderOnlineServiceInterface.php b/app/JsonRpc/OrderOnlineServiceInterface.php new file mode 100644 index 0000000..06b8632 --- /dev/null +++ b/app/JsonRpc/OrderOnlineServiceInterface.php @@ -0,0 +1,8 @@ +orderOnlineService->doComplete($global_order_id, $user_id); - $this->separateAccountsService->orderOnlineCompleted($global_order_id, $user_id); + $this->orderService->onlineCompleted($global_order_id); + $this->separateAccountsService->orderOnlineCompleted($global_order_id); Db::commit(); return [ "status" => 200, "code" => 0, "result" => [], - "message" => '处理成功' + "message" => '调用成功' ]; } catch (\Exception $e) { Db::rollBack(); - $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]); - return [ - "status" => 200, - "code" => $e->getCode() ?? ErrorCode::ORDER_COMPLETE_FAIL, - "result" => [], - "message" => $e->getMessage() - ]; - } - - } - - /** - * 线上订单退款,整个订单退,这个是专门用于处理用户的申请退款的同意退款操作 - * @param $global_order_id - * @param $user_id - * @return array - */ - public function onlineRefund($global_order_id, $user_id) - { - try { - $this->orderOnlineService->doRefund($global_order_id, $user_id); + $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['exception' => $e->getMessage()]); return [ "status" => 200, - "code" => 0, + "code" =>ErrorCode::SEPARATE_ACCOUNTS_ERROR, "result" => [], - "message" => '处理成功' - ]; - } catch (\Exception $e) { - return [ - "status" => 200, - "code" => $e->getCode(), - "result" => [], - "message" => $e->getMessage() + "message" => ErrorCode::getMessage(ErrorCode::SEPARATE_ACCOUNTS_ERROR) ]; } } /** - * 线上订单单笔退款,主要用于后台强行操作退单退款 - * 支持单商品、单店、整单 - * 按比例计算红包进行退款 - * 比如:两个子订单和子订单商品,分别是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 $order_child_id *主订单ID, - * @param $order_goods_id *订单商品ID - * @param $note - * @return array - */ - public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null) - { - - Db::beginTransaction(); - try { - - $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_encode($params) - ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } - - // 主订单 - $orderMain = OrderMain::query() - ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id]) - ->whereIn('state', OrderState::CAN_REFUND_DIRECT) - ->first(); - - if (is_null($orderMain)) { - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在', - 'params' => json_encode($params) - ]); - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } - - // 子订单 - $orderChild = null; - if ($order_child_id) { - $orderChild = Order::query()->with('store:user_id')->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first(); - } - - // 订单商品 - $orderGoods = null; - if ($order_goods_id) { - if (!$order_child_id || is_null($orderChild)) { - $this->log->event(LogLabel::ORDER_REFUND_LOG, [ - '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) - ]); - - // 退款成功 - if ( - !($refundResult['return_code'] == 'SUCCESS' - && isset($refundResult['result_code']) - && $refundResult['result_code'] == 'SUCCESS') - ) { - throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL); - } - - $currentTime = time(); - // 处理订单状态 - $orderMain->state = OrderState::REFUNDED; - $orderMain->total_refund_note = $note; - $orderMain->refund_time = $currentTime; - $orderMain->save(); - - if ($refundType == 'sub') { - $orderChild->status = 3; - $orderChild->refund_note = $note; - $orderChild->refund_time = $currentTime; - $orderChild->save(); - } elseif ($refundType == 'goods') { - $orderGoods->status = 3; - $orderGoods->refund_note = $note; - $orderGoods->refund_time = $currentTime; - $orderGoods->save(); - } - - // 处理用户和商户流水 - if ($refundType == 'main') { # 整单退的话还得处理所有子订单的商户 - - $orderChildren = Order::query()->with('store:user_id')->where(['order_main_id' => $orderMain->global_order_id])->get(); - foreach ($orderChildren as $key => &$order) { - // 占订单金额的比例 - $rate = bcdiv($order->money, $totalAmount, 6); - // 计算优惠券所占金额 - $couponMoney = bcmul($orderMain->coupon_money, $rate, 6); - // 计算本次退款实际退款金额 - $refundStoreAmount = bcsub($order->money, $couponMoney, 2); - $this->financialRecordService->storeRefundDirect($order->store->user_id, $order->id, $refundStoreAmount); - } - - } elseif ($refundType == 'sub') { # 退子订单或者退单品的话,商户只有一个 - $this->financialRecordService->storeRefundDirect($orderChild->store->user_id, $orderChild->id, $refundAmount); - } - - $this->financialRecordService->userRefundDirect($orderMain->user_id, $orderMain->global_order_id, $refundAmount); - - Db::commit(); - return [ - "status" => 200, - "code" => 0, - "result" => [], - "message" => '处理成功' - ]; - - } catch (\Exception $e) { - Db::rollBack(); - return [ - "status" => 200, - "code" => $e->getCode(), - "result" => [], - "message" => '[退款失败]'.$e->getMessage() - ]; - } - + * 线上订单退款 + * 申请退款 state = 8 + * 退款成功 state = 9 + */ + public function onlineRefund($global_order_id){ + $result = [ + "status" => 200, + "code" => ErrorCode::ORDER_FAILURE, + "result" => [], + "message" => '' + ]; + + $res = $this->orderService->onlineRefund($global_order_id); + if($res['code'] > 0){ + $result['result'] = $res; + $result['message'] = '退款失败'; + }else{ + $result['code'] = 0; + $result['result'] = $res; + $result['message'] = '退款成功'; + }; + + return $result; } } \ No newline at end of file diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index fb97855..cf36bd3 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -30,6 +30,7 @@ return [ \App\Service\UserRelationBindServiceInterface::class => \App\Service\UserCommunityBindService::class, \Hyperf\JsonRpc\JsonRpcTransporter::class => \Hyperf\JsonRpc\JsonRpcPoolTransporter::class, \App\JsonRpc\OrderServiceInterface::class => \App\JsonRpc\OrderService::class, + \App\JsonRpc\OrderOnlineServiceInterface::class => \App\JsonRpc\OrderOnlineService::class, \App\Service\FinancialRecordServiceInterface::class => \App\Service\FinancialRecordService::class, \App\Service\SeparateAccountsServiceInterface::class => \App\Service\SeparateAccountsService::class, \App\Service\ShopCarServiceInterface::class => \App\Service\ShopCarService::class,