2023-06-20 04:15:14 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-06-20 09:29:02 +00:00
|
|
|
"errors"
|
2023-06-20 08:46:56 +00:00
|
|
|
"fusenapi/constants"
|
2023-06-20 04:15:14 +00:00
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-06-20 08:46:56 +00:00
|
|
|
"time"
|
2023-06-20 04:15:14 +00:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/home-user-auth/internal/svc"
|
|
|
|
"fusenapi/server/home-user-auth/internal/types"
|
|
|
|
|
2023-06-20 11:36:28 +00:00
|
|
|
"github.com/stripe/stripe-go/v74"
|
|
|
|
"github.com/stripe/stripe-go/v74/client"
|
|
|
|
|
2023-06-20 04:15:14 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2023-06-20 08:46:56 +00:00
|
|
|
"gorm.io/gorm"
|
2023-06-20 04:15:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2023-06-20 08:46:56 +00:00
|
|
|
|
2023-06-20 04:15:14 +00:00
|
|
|
if !userinfo.IsUser() {
|
|
|
|
return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误
|
|
|
|
}
|
|
|
|
|
|
|
|
//订单id
|
|
|
|
orderId := req.OrderId
|
2023-06-20 08:46:56 +00:00
|
|
|
if orderId < 1 {
|
2023-06-20 04:15:14 +00:00
|
|
|
return resp.SetStatus(basic.CodeRequestParamsErr)
|
|
|
|
}
|
|
|
|
|
2023-06-21 06:26:29 +00:00
|
|
|
m := l.svcCtx.AllModels.FsOrder
|
2023-06-20 08:46:56 +00:00
|
|
|
order, err := m.FindOne(l.ctx, userinfo.UserId, orderId)
|
2023-06-20 04:15:14 +00:00
|
|
|
if err != nil {
|
2023-06-20 09:29:02 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2023-06-20 08:46:56 +00:00
|
|
|
return resp.SetStatus(basic.CodeOrderNotFoundErr)
|
|
|
|
}
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeDbSqlErr)
|
2023-06-20 04:15:14 +00:00
|
|
|
}
|
2023-06-20 08:46:56 +00:00
|
|
|
|
|
|
|
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)
|
2023-06-20 04:15:14 +00:00
|
|
|
}
|
2023-06-20 08:46:56 +00:00
|
|
|
|
|
|
|
if *order.IsPayCompleted == 1 &&
|
|
|
|
time.Now().After(time.Unix(*order.Ctime, 0).Add(48*time.Hour)) {
|
|
|
|
return resp.SetStatus(basic.CodeOrderNotCancelledErr)
|
2023-06-20 04:15:14 +00:00
|
|
|
}
|
2023-06-20 08:46:56 +00:00
|
|
|
|
2023-06-20 09:29:02 +00:00
|
|
|
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().Unix()
|
|
|
|
)
|
|
|
|
|
|
|
|
refund := &gmodel.FsRefundReason{
|
|
|
|
IsRefund: &IsRefund,
|
|
|
|
RefundReasonId: &req.RefundReasonId,
|
|
|
|
RefundReason: &req.RefundReason,
|
|
|
|
OrderId: &order.Id,
|
|
|
|
CreatedAt: &CreatedAt,
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:36:28 +00:00
|
|
|
mFsRefundReason := gmodel.NewFsRefundReasonModel(l.svcCtx.MysqlConn)
|
|
|
|
err = mFsRefundReason.Create(l.ctx, refund)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeDbSqlErr)
|
|
|
|
}
|
2023-06-20 09:29:02 +00:00
|
|
|
|
|
|
|
// 退款
|
|
|
|
// 调用第三方接口发起退款
|
2023-06-20 11:36:28 +00:00
|
|
|
|
|
|
|
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]
|
2023-06-21 08:39:55 +00:00
|
|
|
payM := l.svcCtx.AllModels.FsPay
|
2023-06-20 09:29:02 +00:00
|
|
|
|
|
|
|
// 查询支付信息
|
2023-06-21 08:39:55 +00:00
|
|
|
pays, err := payM.GetOrderPayList(l.ctx, *order.Sn, 1, 0)
|
2023-06-20 09:29:02 +00:00
|
|
|
for _, pay := range pays {
|
2023-06-20 11:36:28 +00:00
|
|
|
sc.Refunds.New(&stripe.RefundParams{
|
2023-06-20 09:29:02 +00:00
|
|
|
PaymentIntent: pay.TradeNo,
|
|
|
|
})
|
|
|
|
}
|
2023-06-20 11:44:31 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return resp.SetStatus(basic.CodeApiErr)
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 09:29:02 +00:00
|
|
|
|
2023-06-21 08:39:55 +00:00
|
|
|
return resp.SetStatus(basic.CodePayCancelOk, uOrder)
|
|
|
|
|
2023-06-20 11:36:28 +00:00
|
|
|
// return ResponseError(500, "Cancellation failure")
|
2023-06-20 09:29:02 +00:00
|
|
|
|
2023-06-21 08:39:55 +00:00
|
|
|
// return resp.SetStatus(basic.CodePayCancelNotOk)
|
2023-06-20 04:15:14 +00:00
|
|
|
}
|
2023-06-20 08:46:56 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
// }
|