package logic import ( "errors" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "time" "context" "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" "github.com/stripe/stripe-go/v74" "github.com/stripe/stripe-go/v74/client" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type UserOderDeleteLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUserOderDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOderDeleteLogic { return &UserOderDeleteLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null if !userinfo.IsUser() { return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 } //订单id orderId := req.OrderId if orderId < 1 { return resp.SetStatus(basic.CodeRequestParamsErr) } m := l.svcCtx.AllModels.FsOrder order, err := m.FindOne(l.ctx, userinfo.UserId, orderId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatus(basic.CodeOrderNotFoundErr) } logx.Error(err) return resp.SetStatus(basic.CodeDbSqlErr) } if auth.CheckValueRange[constants.Order]( constants.Order(*order.Status), constants.STATUS_NEW_NOT_PAY, constants.STATUS_NEW_PAY_COMPLETED, constants.STATUS_NEW_PART_PAY, ) { return resp.SetStatus(basic.CodeOrderNotCancelledErr) } if *order.IsPayCompleted == 1 && time.Now().After(time.Unix(*order.Ctime, 0).Add(48*time.Hour)) { return resp.SetStatus(basic.CodeOrderNotCancelledErr) } uOrder := &gmodel.FsOrder{ Id: orderId, } var ( isCancel int64 = 1 ustatus int64 = int64(constants.STATUS_NEW_CANCEL) ) // 修改取消状态和取消原因 uOrder.Status = &ustatus uOrder.IsCancel = &isCancel uOrder.RefundReasonId = &req.RefundReasonId uOrder.RefundReason = &req.RefundReason err = m.Update(l.ctx, uOrder) if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeDbUpdateErr) } var ( IsRefund int64 = 0 CreatedAt = time.Now().UTC().Unix() ) refund := &gmodel.FsRefundReason{ IsRefund: &IsRefund, RefundReasonId: &req.RefundReasonId, RefundReason: &req.RefundReason, OrderId: &order.Id, CreatedAt: &CreatedAt, } mFsRefundReason := gmodel.NewFsRefundReasonModel(l.svcCtx.MysqlConn) err = mFsRefundReason.Create(l.ctx, refund) if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeDbSqlErr) } // 退款 // 调用第三方接口发起退款 config := &stripe.BackendConfig{ MaxNetworkRetries: stripe.Int64(0), // Zero retries } sc := &client.API{} sc.Init(l.svcCtx.Config.Stripe.SK, &stripe.Backends{ API: stripe.GetBackendWithConfig(stripe.APIBackend, config), Uploads: stripe.GetBackendWithConfig(stripe.UploadsBackend, config), }) // ['order_number' => $order->sn, 'is_refund' => 0, 'pay_status' => 1] payM := l.svcCtx.AllModels.FsPay // 查询支付信息 pays, err := payM.GetOrderPayList(l.ctx, *order.Sn, 1, 0) for _, pay := range pays { sc.Refunds.New(&stripe.RefundParams{ PaymentIntent: pay.TradeNo, }) } if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatus(basic.CodeApiErr) } } return resp.SetStatus(basic.CodePayCancelOk, uOrder) // return ResponseError(500, "Cancellation failure") // return resp.SetStatus(basic.CodePayCancelNotOk) } // func (l *OrderLogic) CancelOrder(req *types.RequestCancelOrder, userinfo *auth.UserInfo) (resp *basic.Response) { // // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // // userinfo 传入值时, 一定不为null // if !userinfo.IsUser() { // return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 // } // //订单id // id := req.ReqParam.Get("id") // if id == "" { // return resp.SetStatus(basic.CodeParamErr) // } // //验证当前订单是否可以取消 // order, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).GetOne(l.ctx, map[string]interface{}{"user_id": userinfo.UserId, "id": id}) // if err != nil { // return resp.SetStatus(basic.CodeDbQueryErr) // } // if order == nil { // return resp.SetStatus(basic.CodeNotFound) // } // //那些状态下是不能关闭的? (订单完成,退款完成,关闭订单) // if order.Status == gconst.OrderStatusComplete || // order.Status == gconst.OrderStatusRefund || // order.Status == gconst.OrderStatusClose { // return resp.SetStatus(basic.CodeCannotOperate) // } // //订单状态修改 // order.Status = gconst.OrderStatusDelete // order.IsDeleted = 1 // result, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).UpdateOne(l.ctx, order) // if err != nil { // return resp.SetStatus(basic.CodeDbUpdateErr) // } // return resp.SetStatus(basic.CodeOK, result) // }