From 23817a928b4f41c29d402a02b710ca98159b0e40 Mon Sep 17 00:00:00 2001 From: ww519441258 <310243791.com> Date: Tue, 17 Dec 2019 17:01:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=83=A8=E5=88=86=E9=80=80?= =?UTF-8?q?=E6=AC=BE=E5=8A=9F=E8=83=BD=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/config/main.php | 3 +- api/controllers/WxPaymentController.php | 20 +----- api/logic/WxPaymentLogic.php | 55 +------------- .../shop/controllers/OrderController.php | 34 ++++++++- .../shop/logic/payment/WxPaymentManager.php | 71 +++++++++++++++++++ .../shop/models/searchs/OrderSearch.php | 26 +++++++ backend/modules/shop/views/order/refund.php | 38 ++++++++++ 7 files changed, 172 insertions(+), 75 deletions(-) create mode 100644 backend/modules/shop/logic/payment/WxPaymentManager.php create mode 100644 backend/modules/shop/views/order/refund.php diff --git a/api/config/main.php b/api/config/main.php index 79b792b..eeea5cd 100644 --- a/api/config/main.php +++ b/api/config/main.php @@ -106,8 +106,7 @@ return [ 'extraPatterns' => [ 'GET web' => 'web', 'POST notify' => 'notify', - 'POST refund' => 'refund', - 'POST test' => 'test', + 'POST apply-refund' => 'apply-refund', ] ], ], diff --git a/api/controllers/WxPaymentController.php b/api/controllers/WxPaymentController.php index 543ac7f..cf9b42d 100644 --- a/api/controllers/WxPaymentController.php +++ b/api/controllers/WxPaymentController.php @@ -26,7 +26,7 @@ class WxPaymentController extends CommonController return ArrayHelper::merge(parent::behaviors(), [ 'authenticatior' => [ 'class' => HttpBearerAuth::className(), - 'except' => ['notify', 'test'], + 'except' => ['notify'], ] ]); } @@ -68,12 +68,6 @@ class WxPaymentController extends CommonController return $this->object->notify(); } - public function actionTest() - { - $data = Yii::$app->request->getBodyParam('notify');/*int 商品id*/ - return $data; - } - /** * @return bool * @throws BadRequestHttpException @@ -86,16 +80,4 @@ class WxPaymentController extends CommonController return $this->object->applyRefund(); } - /** - * @return mixed - * @throws BadRequestHttpException - * @throws \yii\db\Exception - * @throws \yii\web\NotFoundHttpException - * 退款 - */ - public function actionRefund() - { - return $this->object->refund(); - } - } \ No newline at end of file diff --git a/api/logic/WxPaymentLogic.php b/api/logic/WxPaymentLogic.php index 6bd07df..477f284 100644 --- a/api/logic/WxPaymentLogic.php +++ b/api/logic/WxPaymentLogic.php @@ -20,6 +20,7 @@ use yii\httpclient\Client; use yii\web\BadRequestHttpException; use yii\base\BaseObject; use yii\web\NotFoundHttpException; +use yii\web\ServerErrorHttpException; class WxPaymentLogic extends BaseObject { @@ -114,7 +115,7 @@ class WxPaymentLogic extends BaseObject } } - private function getApp() + protected function getApp() { $this->initObject(); $config = [ @@ -294,58 +295,6 @@ class WxPaymentLogic extends BaseObject return true; } - /** - * @return mixed - * @throws BadRequestHttpException - * @throws Exception - * @throws NotFoundHttpException - * 退款 - */ - public function refund() - { - $orderId = Yii::$app->request->getbodyParam('order_id'); - if (empty($orderId)) { - throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); - } - $paymentLog = PaymentLog::findOne(['order_id' => $orderId]); - if (empty($paymentLog)) { - throw new NotFoundHttpException('订单支付信息未找到'); - } - $refundLog = RefundLog::findOne(['order_id' => $orderId, 'status' => self::STATUS_REFUND_WAIT]); - if (!$refundLog) { - throw new NotFoundHttpException('订单退款信息未找到'); - } - - /*参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数*/ - $this->getApp(); - $config = ['refund_desc' => '退款']; - $result = $this->app->refund->byTransactionId( - $paymentLog->wx_refund_id, - $refundLog->wx_refund_id, - round($paymentLog->payment_amount * 100), - round($refundLog->refund_amount * 100), - $config - ); - Yii::info($result, 'refund_log'); - - if ($result['return_code'] == 'FAIL' || $result['return_msg'] != 'OK' || $result['result_code'] == 'FAIL') { - throw new BadRequestHttpException($result['return_msg']); - } - if ($result['err_code_des']) { - throw new BadRequestHttpException($result['err_code_des']); - } - $paymentLog->status = $refundLog->refund_amount < $paymentLog->payment_amount ? self::STATUS_REFUND_SUCCESS : self::STATUS_REFUND_PORTION; - if (!$paymentLog->save()) { - throw new Exception(Helper::errorMessageStr($paymentLog->errors)); - } - $refundLog->status = $refundLog->refund_amount < $paymentLog->payment_amount ? self::STATUS_REFUND_SUCCESS : self::STATUS_REFUND_PORTION; - $refundLog->finished_at = time(); - if (!$refundLog->save()) { - throw new Exception(Helper::errorMessageStr($refundLog->errors)); - } - return $result; - } - /** * @param $data diff --git a/backend/modules/shop/controllers/OrderController.php b/backend/modules/shop/controllers/OrderController.php index 69d1b45..ece17fc 100755 --- a/backend/modules/shop/controllers/OrderController.php +++ b/backend/modules/shop/controllers/OrderController.php @@ -2,8 +2,11 @@ namespace backend\modules\shop\controllers; +use api\logic\WxPaymentLogic; +use backend\models\shop\logic\payment\WxPaymentManager; use backend\modules\shop\logic\delivery\DeliveryManager; use backend\modules\shop\models\ars\Delivery; +use backend\modules\shop\models\ars\RefundLog; use Yii; use backend\modules\shop\models\ars\Order; use backend\modules\shop\models\searchs\OrderSearch; @@ -153,7 +156,6 @@ class OrderController extends Controller * @param $id * @return string|\yii\web\Response * @throws NotFoundHttpException - * @throws \yii\db\Exception * 订单发货 */ public function actionDelivery($id) @@ -179,4 +181,34 @@ class OrderController extends Controller ]); } + /** + * @param $id + * @return string|\yii\web\Response + * @throws \yii\base\InvalidConfigException + * @var $wxPayment + */ + public function actionRefund($id) + { + $model = RefundLog::findOne([ + 'order_id' => $id, + 'status' => WxPaymentLogic::STATUS_REFUND_WAIT + ]); + + if (Yii::$app->request->post('RefundLog')) { + $wxPayment = Yii::createObject([ + 'class' => 'backend\modules\shop\logic\payment\WxPaymentManager' + ]); + $res = $wxPayment->refund($model, Yii::$app->user->id); + if ($res['status']) { + return $this->redirect(['index']); + } else { + Yii::$app->session->setFlash('error', $res['info']); + } + } + + return $this->render('refund', [ + 'model' => $model + ]); + } + } diff --git a/backend/modules/shop/logic/payment/WxPaymentManager.php b/backend/modules/shop/logic/payment/WxPaymentManager.php new file mode 100644 index 0000000..96e05c2 --- /dev/null +++ b/backend/modules/shop/logic/payment/WxPaymentManager.php @@ -0,0 +1,71 @@ + $refundLog->order_id]); + if (empty($paymentLog)) { + throw new NotFoundHttpException('订单支付信息未找到'); + } + $refundLog = RefundLog::findOne(['order_id' => $orderId, 'status' => self::STATUS_REFUND_WAIT]); + if (!$refundLog) { + throw new NotFoundHttpException('订单退款信息未找到'); + } + + /*参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数*/ + $this->getApp(); + $config = ['refund_desc' => '退款']; + $result = $this->app->refund->byTransactionId( + $paymentLog->wx_refund_id, + $refundLog->wx_refund_id, + round($paymentLog->payment_amount * 100), + round($refundLog->refund_amount * 100), + $config + ); + Yii::info($result, 'refund_log'); + + if ($result['return_code'] == 'FAIL' || $result['return_msg'] != 'OK' || $result['result_code'] == 'FAIL') { + throw new BadRequestHttpException($result['return_msg']); + } + if ($result['err_code_des']) { + throw new BadRequestHttpException($result['err_code_des']); + } + $tra = Yii::$app->db->beginTransaction(); + try { + $paymentLog->status = $refundLog->refund_amount < $paymentLog->payment_amount ? self::STATUS_REFUND_SUCCESS : self::STATUS_REFUND_PORTION; + if (!$paymentLog->save()) { + throw new Exception(Helper::errorMessageStr($paymentLog->errors)); + } + $refundLog->operator_id = $operatorId; + $refundLog->status = $refundLog->refund_amount < $paymentLog->payment_amount ? self::STATUS_REFUND_SUCCESS : self::STATUS_REFUND_PORTION; + $refundLog->finished_at = time(); + if (!$refundLog->save()) { + throw new Exception(Helper::errorMessageStr($refundLog->errors)); + } + $tra->commit(); + return ['status' => 'true']; + } catch (Exception $e) { + $tra->rollBack(); + return ['status' => false, 'info' => $e->getMessage()]; + } + } +} \ No newline at end of file diff --git a/backend/modules/shop/models/searchs/OrderSearch.php b/backend/modules/shop/models/searchs/OrderSearch.php index 0cd28ed..c813618 100755 --- a/backend/modules/shop/models/searchs/OrderSearch.php +++ b/backend/modules/shop/models/searchs/OrderSearch.php @@ -162,6 +162,32 @@ class OrderSearch extends Order 'rule' => 'or' ] ], + [ + 'name' => 'delivery', + 'icon' => 'box', + 'title' => '发货', + 'hide' => [ + 'attributes' => [ + 'status', + 'status', + 'status', + 'status', + 'status', + 'status', + 'status', + ], + 'values' => [ + Order::STATUS_UNCONFIRMED, + Order::STATUS_NONPAYMENT, + Order::STATUS_CANCEL, + Order::STATUS_PAYMENT_TO_BE_CONFIRMED, + Order::STATUS_REFUND, + Order::STATUS_SHIPMENT_ALL, + Order::STATUS_FINISH + ], + 'rule' => 'or' + ] + ], [ 'name' => 'update', 'icon' => 'pencil', diff --git a/backend/modules/shop/views/order/refund.php b/backend/modules/shop/views/order/refund.php new file mode 100644 index 0000000..1a1988d --- /dev/null +++ b/backend/modules/shop/views/order/refund.php @@ -0,0 +1,38 @@ +title = '退款订单:' . $model->order_id; +$this->params['breadcrumbs'][] = ['label' => '订单列表', 'url' => ['index']]; +?> + +
+ + + field($model, 'wx_refund_id')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + + field($model, 'reason')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + + field($model, 'order_amount')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + + field($model, 'refund_amount')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + + field($model, 'refunded_amount')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + + field($model, 'refund_account')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + + field($model, 'type')->textInput(['maxlength' => true, 'readonly' => 'true']) ?> + +
+ 'btn btn-success']) ?> + 'btn btn-info']) ?> +
+ + + +
+