Browse Source

refactor: 部分退款功能重构

wechat_public_accounts
ww519441258 5 years ago
parent
commit
23817a928b
  1. 3
      api/config/main.php
  2. 20
      api/controllers/WxPaymentController.php
  3. 55
      api/logic/WxPaymentLogic.php
  4. 34
      backend/modules/shop/controllers/OrderController.php
  5. 71
      backend/modules/shop/logic/payment/WxPaymentManager.php
  6. 26
      backend/modules/shop/models/searchs/OrderSearch.php
  7. 38
      backend/modules/shop/views/order/refund.php

3
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',
]
],
],

20
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();
}
}

55
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

34
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
]);
}
}

71
backend/modules/shop/logic/payment/WxPaymentManager.php

@ -0,0 +1,71 @@
<?php
namespace backend\modules\shop\logic\payment;
use api\logic\Helper;
use api\logic\WxPaymentLogic;
use backend\modules\shop\models\ars\PaymentLog;
use backend\modules\shop\models\ars\RefundLog;
use yii\db\Exception;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use Yii;
class WxPaymentManager extends WxPaymentLogic
{
/**
* @param RefundLog $refundLog
* @param int $operatorId
* @return array|mixed
* @throws BadRequestHttpException
* @throws NotFoundHttpException
*/
public function refund($refundLog, $operatorId)
{
$paymentLog = PaymentLog::findOne(['order_id' => $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()];
}
}
}

26
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',

38
backend/modules/shop/views/order/refund.php

@ -0,0 +1,38 @@
<?php
use yii\helpers\Html;
use kartik\form\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\shop\models\ars\RefundLog */
$this->title = '退款订单:' . $model->order_id;
$this->params['breadcrumbs'][] = ['label' => '订单列表', 'url' => ['index']];
?>
<div class="refund-log-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'wx_refund_id')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<?= $form->field($model, 'reason')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<?= $form->field($model, 'order_amount')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<?= $form->field($model, 'refund_amount')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<?= $form->field($model, 'refunded_amount')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<?= $form->field($model, 'refund_account')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<?= $form->field($model, 'type')->textInput(['maxlength' => true, 'readonly' => 'true']) ?>
<div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
<?= Html::a('返回', ['index'], ['class' => 'btn btn-info']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Loading…
Cancel
Save