7 changed files with 172 additions and 75 deletions
-
3api/config/main.php
-
20api/controllers/WxPaymentController.php
-
55api/logic/WxPaymentLogic.php
-
34backend/modules/shop/controllers/OrderController.php
-
71backend/modules/shop/logic/payment/WxPaymentManager.php
-
26backend/modules/shop/models/searchs/OrderSearch.php
-
38backend/modules/shop/views/order/refund.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()]; |
|||
} |
|||
} |
|||
} |
@ -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> |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue