后台退款接口

This commit is contained in:
2024-09-23 16:42:13 +08:00
parent 7bfb626896
commit 0de85aebdf

View File

@@ -106,6 +106,100 @@ class FinanceController extends BaseAdminController
}
}
/**
* 管理员退款
*
* @return string[]
* @throws Exception
*/
public function actionAdminRefundAgree()
{
// TODO 退款记录生成,然后走退款流程
// 获取前端传递的POST数据
$post = \Yii::$app->request->post();
// 对必要的字段进行验证
$this->requestValidate($post, [
['id', 'required'],
['refund_type', 'required'],
['refund_type', 'required'],
]);
// 实例化ProductOrder模型
$orderModel = new ProductOrder();
// 根据POST数据中的id查询对应的订单信息
$orderInfo = $orderModel->find()->where(['id' => $post['id']])->asArray()->one();
// 开启数据库事务
$t = \Yii::$app->db->beginTransaction();
try {
// 实例化退款申请模型
$model = new ProductOrderRefund();
// 填充退款申请模型的基础信息
$model->order_id = $post['id'];
$model->user_id = \Yii::$app->user->id;
$model->reason = !empty($post['reason'])? $post['reason']:'管理员-'. \Yii::$app->user->id. '-发起退款';
// 如果POST数据中包含退款图片则填充退款图片字段
if (!empty($post['refund_images'])) {
$model->refund_images = $post['refund_images'];
}
// 填充其他退款申请字段
$model->remark = '平台申请退款';
$model->refund_no = $orderInfo['order_no'];
$model->refund_price = $orderInfo['total_pay_price'];
$model->refund_time = date('Y-m-d H:i:s');
// 保存退款申请模型
$model->save();
// 根据刚保存的退款申请ID查找退款申请记录确保其存在且未被删除
$ProductOrderRefund = ProductOrderRefund::find()->where([
'id' => $model->id,
'is_deleted' => 0,
])->with(['order'])->one();
// 如果找不到退款申请记录,则抛出异常
if (!$ProductOrderRefund) {
throw new Exception('退款申请记录不存在');
}
// 根据退款申请记录中的订单ID查找对应的订单确保订单存在
$ProductOrder = ProductOrder::find()->where(['id' => $ProductOrderRefund->order_id])->one();
if (!$ProductOrder) throw new Exception('产品订单不存在');
// 更新订单的退款状态为“已退款”
$ProductOrder->refund_status = 3;
$ProductOrder->saveOrFail();
// 更新退款申请记录的状态,标记为已退款
$ProductOrderRefund->is_refund = 1;
$ProductOrderRefund->status = 1;
$ProductOrderRefund->refund_time = date('Y-m-d H:i:s', time());
$ProductOrderRefund->saveOrFail();
// 执行退款操作
$form = new ProductRefundForm();
$form->refundMoney($ProductOrderRefund);
// 提交事务
$t->commit();
// 返回成功标识
return ['success'];
} catch (\Exception $e) {
// 如果发生异常,则回滚事务
$t->rollBack();
// 重新抛出异常,传递错误信息
throw new Exception($e->getMessage());
}
}
/**
* @doc-name 拒绝退款操作
* @doc-param int id 退款申请记录ID