退款
This commit is contained in:
@@ -20,3 +20,10 @@ OAuth:
|
||||
|
||||
Stripe:
|
||||
SK: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y"
|
||||
|
||||
PayConfig:
|
||||
Stripe:
|
||||
Key: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y"
|
||||
EndpointSecret: "whsec_f5f9a121d43af3789db7459352f08cf523eb9e0fbf3381f91ba6c97c324c174d"
|
||||
SuccessURL: "http://www.baidu.com"
|
||||
CancelURL: "http://www.baidu.com"
|
||||
|
||||
@@ -28,4 +28,13 @@ type Config struct {
|
||||
Stripe struct {
|
||||
SK string
|
||||
}
|
||||
|
||||
PayConfig struct {
|
||||
Stripe struct {
|
||||
EndpointSecret string
|
||||
Key string
|
||||
CancelURL string
|
||||
SuccessURL string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ import (
|
||||
"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"
|
||||
handlerUtils "fusenapi/utils/handler"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
@@ -50,11 +52,76 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us
|
||||
}
|
||||
|
||||
// 判断订单状态
|
||||
if *orderInfo.Status == int64(constants.STATUS_NEW_NOT_PAY) {
|
||||
|
||||
} else {
|
||||
var notCancelStatusMap = make(map[constants.Order]struct{}, 3)
|
||||
notCancelStatusMap[constants.STATUS_NEW_NOT_PAY] = struct{}{}
|
||||
notCancelStatusMap[constants.STATUS_NEW_PART_PAY] = struct{}{}
|
||||
notCancelStatusMap[constants.STATUS_NEW_PAY_COMPLETED] = struct{}{}
|
||||
_, ok := notCancelStatusMap[constants.Order(*orderInfo.Status)]
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "the order status not cancle")
|
||||
}
|
||||
|
||||
var cancelTime int64 = time.Now().Unix() - (*orderInfo.Ctime + int64(constants.CANCLE_ORDER_EXPIRE))
|
||||
// 第一次支付成功后48小时后不能进行取消操作
|
||||
if orderInfo.IsPayCompleted != nil && cancelTime > 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "The current order cannot be cancelled")
|
||||
}
|
||||
|
||||
// 修改订单--取消状态和取消原因
|
||||
*orderInfo.Status = int64(constants.STATUS_NEW_CANCEL)
|
||||
*orderInfo.IsCancel = 1
|
||||
orderInfo.RefundReasonId = &req.RefundReasonId
|
||||
orderInfo.RefundReason = &req.RefundReason
|
||||
|
||||
var nowTime = time.Now().Unix()
|
||||
var payList []handlerUtils.PayInfo
|
||||
// 事务处理
|
||||
err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) {
|
||||
// 修改订单信息
|
||||
orderModelTS := gmodel.NewFsOrderModel(connGorm)
|
||||
err = orderModelTS.Update(ctx, orderInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 新增退款记录
|
||||
var isRefund int64 = 0
|
||||
refundReasonModelTS := gmodel.NewFsRefundReasonModel(connGorm)
|
||||
refundReasonModelTS.CreateOrUpdate(ctx, &gmodel.FsRefundReason{
|
||||
IsRefund: &isRefund,
|
||||
RefundReasonId: &req.RefundReasonId,
|
||||
RefundReason: &req.RefundReason,
|
||||
OrderId: &orderInfo.Id,
|
||||
CreatedAt: &nowTime,
|
||||
})
|
||||
// 退款申请
|
||||
// 退款申请--查询支付信息
|
||||
fsPayModelTS := gmodel.NewFsPayModel(connGorm)
|
||||
rbFsPay := fsPayModelTS.RowSelectBuilder(nil).Where("order_number = ?", orderInfo.Sn).Where("pay_status =?", constants.PAYSTATUS_SUCCESS).Where("is_refund =?", 0)
|
||||
payInfoList, err := fsPayModelTS.FindAll(ctx, rbFsPay, nil, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, payInfo := range payInfoList {
|
||||
var key string
|
||||
if *payInfo.PaymentMethod == int64(constants.PAYMETHOD_STRIPE) {
|
||||
key = l.svcCtx.Config.PayConfig.Stripe.Key
|
||||
}
|
||||
payList = append(payList, handlerUtils.PayInfo{
|
||||
TradeNo: *payInfo.TradeNo,
|
||||
PaymentMethod: *payInfo.PaymentMethod,
|
||||
Key: key,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
// 退款申请--调取第三方接口发起退款
|
||||
handlerUtils.PayRefundHandler(&handlerUtils.PayRefundHandlerReq{
|
||||
PayInfoList: payList,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeOrderCancelledNotOk, "the order cancle failed")
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user