animeic 1 жил өмнө
parent
commit
aa7dc36f1d

+ 32 - 0
pay/bus/IPay.go

@@ -0,0 +1,32 @@
+package bus
+
+import (
+	"context"
+	"pay/db/model"
+
+	"infish.cn/comm/pay"
+)
+
+const (
+	ALIPAY    = 0
+	WECHATPAY = 1
+	SINGLE    = 0
+	MULTI     = 1
+)
+
+type PayInf interface {
+	Pay(context.Context, *pay.OrderMsg) (interface{}, error)
+	Close(context.Context, *pay.OrderMsg) (bool, error)
+	ReFundPay(ctx context.Context, refund *pay.ReFund) (interface{}, error)
+	ReFundQuery(ctx context.Context, refund *model.ReFund) (interface{}, error)
+}
+
+func PayMod(payType int) PayInf {
+	if payType == ALIPAY {
+		return NewSingleAliPay()
+	} else if payType == WECHATPAY {
+		return NewSingleWechatPay()
+
+	}
+	return nil
+}

+ 0 - 0
pay/bus/singleAliPay.go → pay/bus/aliPay.go


+ 92 - 17
pay/bus/pay.go

@@ -2,31 +2,106 @@ package bus
 
 import (
 	"context"
+	"errors"
+	"fmt"
 	"pay/db/model"
+	"pay/log"
+	"pay/utils"
 
+	"github.com/nats-io/nats.go"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+	"infish.cn/comm"
 	"infish.cn/comm/pay"
 )
 
-const (
-	ALIPAY    = 0
-	WECHATPAY = 1
-	SINGLE    = 0
-	MULTI     = 1
-)
+type SinglePay struct{}
+
+// 创建二维码
+func (s *SinglePay) CreateQr() *comm.NatsMsgReplyer {
+	utils.ConsoleFormat(pay.PaySingleQrApi)
+
+	return &comm.NatsMsgReplyer{
+		Subject: pay.PaySingleQrApi,
+		Entity:  func() interface{} { return &pay.OrderMsg{} },
+		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
+			m := entity.(*pay.OrderMsg)
+			pay := PayMod(int(*m.PayMod))
+			ctx := context.Background()
+			res, err := pay.Pay(ctx, m)
+			return res, err
+		},
+	}
+}
 
-type PayInf interface {
-	Pay(context.Context, *pay.OrderMsg) (interface{}, error)
-	Close(context.Context, *pay.OrderMsg) (bool, error)
-	ReFundPay(ctx context.Context, refund *pay.ReFund) (interface{}, error)
-	ReFundQuery(ctx context.Context, refund *model.ReFund) (interface{}, error)
+// 关闭订单应答
+func (s *SinglePay) CloseOrder() *comm.NatsMsgReplyer {
+	utils.ConsoleFormat(pay.PaySingleCloseOrderApi)
+
+	return &comm.NatsMsgReplyer{
+		Subject: pay.PaySingleCloseOrderApi,
+		Entity:  func() interface{} { return &pay.OrderMsg{} },
+		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
+			m := entity.(*pay.OrderMsg)
+			pay := PayMod(int(*m.PayMod))
+			ctx := context.Background()
+			flag, err := pay.Close(ctx, m)
+			return flag, err
+		},
+	}
+}
+
+// 退款
+func (s *SinglePay) Refund() *comm.NatsMsgReplyer {
+	utils.ConsoleFormat(pay.PaySingleRefundApi)
+	refundId := primitive.ObjectID{}
+	return &comm.NatsMsgReplyer{
+		Subject: pay.PaySingleRefundApi,
+		Entity:  func() interface{} { return &refundId },
+		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
+			refundId := entity.(*primitive.ObjectID)
+			cxt := context.Background()
+			refund, err := getPayRefund(cxt, map[string]interface{}{"_id": refundId})
+			if err != nil {
+				return false, errors.New("该退款申请信息错误")
+			}
+			// ???适配缺失退款理由
+			if refund.Reason == "" {
+				refund.Reason = "默认退款"
+			}
+			payi := PayMod(int(*refund.PayMod))
+			ctx := context.Background()
+			flag, err := payi.ReFundPay(ctx, refund)
+			// 根据退款结果更新退款状态
+			var status int32 = 0
+			if flag.(bool) {
+				status = int32(pay.REFUND_SUCCESS)
+			} else {
+				// 退款异常
+				status = int32(pay.REFUND_ABNORMAL)
+			}
+			_, resErr := updateRefund(ctx, &pay.ReFund{Status: &status})
+			if resErr != nil {
+				log.Error(err)
+				fmt.Println("更新退款状态失败")
+			}
+			return flag, err
+		},
+	}
 }
 
-func PayMod(payType int) PayInf {
-	if payType == ALIPAY {
-		return NewSingleAliPay()
-	} else if payType == WECHATPAY {
-		return NewSingleWechatPay()
+// 退款查询
+func (s *SinglePay) RefundQuery() *comm.NatsMsgReplyer {
+	utils.ConsoleFormat(pay.PaySingleRefundQueryApi)
 
+	return &comm.NatsMsgReplyer{
+		Subject: pay.PaySingleRefundQueryApi,
+		Entity:  func() interface{} { return &model.ReFund{} },
+		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
+			m := entity.(*model.ReFund)
+			pay := PayMod(int(*m.PayMode))
+			ctx := context.Background()
+			status, err := pay.ReFundQuery(ctx, m)
+			return status, err
+		},
 	}
-	return nil
 }

+ 33 - 0
pay/bus/refund.go

@@ -5,9 +5,11 @@ import (
 	"pay/db"
 	"pay/db/repo"
 	"pay/utils"
+	"time"
 
 	"github.com/nats-io/nats.go"
 	"go.mongodb.org/mongo-driver/bson/primitive"
+	"go.mongodb.org/mongo-driver/mongo"
 	"infish.cn/comm"
 	"infish.cn/comm/pay"
 )
@@ -36,3 +38,34 @@ func createRefund(ctx context.Context, refund *pay.ReFund) (id primitive.ObjectI
 	id, err = primitive.ObjectIDFromHex(_id)
 	return
 }
+
+// query := map[string]interface{}{"_id":"xxx"}
+func PayRefundDetail() *comm.NatsMsgReplyer {
+	qmap := make(map[string]interface{})
+	utils.ConsoleFormat(pay.PayRefundDetailApi)
+	return &comm.NatsMsgReplyer{
+		Subject: pay.PayRefundDetailApi,
+		Entity:  func() interface{} { return &qmap },
+		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
+			query := entity.(*map[string]interface{})
+			ctx := context.Background()
+			res, err := getPayRefund(ctx, *query)
+			return res, err
+		},
+	}
+}
+
+func getPayRefund(ctx context.Context, query map[string]interface{}) (refund *pay.ReFund, err error) {
+	_, err = repo.RepoSeachDoc(&repo.RepoSession{Ctx: ctx, Client: db.GMongoDb}, &repo.DocSearchOptions{
+		CollectName: repo.CollectionRefund,
+		Query:       query,
+	}, &refund)
+	return
+}
+
+func updateRefund(ctx context.Context, refund *pay.ReFund) (result *mongo.UpdateResult, err error) {
+	refund.UpdateTime = time.Now()
+	result, err = repo.RepoUpdateSetDoc(&repo.RepoSession{Ctx: ctx, Client: db.GMongoDb}, repo.CollectionRefund, refund.Id.Hex(), &refund)
+
+	return
+}

+ 0 - 85
pay/bus/singlePay.go

@@ -1,85 +0,0 @@
-package bus
-
-import (
-	"context"
-	"pay/db/model"
-	"pay/utils"
-
-	"github.com/nats-io/nats.go"
-	"infish.cn/comm"
-	"infish.cn/comm/pay"
-)
-
-type SinglePay struct{}
-
-// 创建二维码
-func (s *SinglePay) CreateQr() *comm.NatsMsgReplyer {
-	utils.ConsoleFormat(pay.PaySingleQrApi)
-
-	return &comm.NatsMsgReplyer{
-		Subject: pay.PaySingleQrApi,
-		Entity:  func() interface{} { return &pay.OrderMsg{} },
-		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
-			m := entity.(*pay.OrderMsg)
-			pay := PayMod(int(*m.PayMod))
-			ctx := context.Background()
-			res, err := pay.Pay(ctx, m)
-			return res, err
-		},
-	}
-}
-
-// 关闭订单应答
-func (s *SinglePay) CloseOrder() *comm.NatsMsgReplyer {
-	utils.ConsoleFormat(pay.PaySingleCloseOrderApi)
-
-	return &comm.NatsMsgReplyer{
-		Subject: pay.PaySingleCloseOrderApi,
-		Entity:  func() interface{} { return &pay.OrderMsg{} },
-		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
-			m := entity.(*pay.OrderMsg)
-			pay := PayMod(int(*m.PayMod))
-			ctx := context.Background()
-			flag, err := pay.Close(ctx, m)
-			return flag, err
-		},
-	}
-}
-
-// 退款
-func (s *SinglePay) Refund() *comm.NatsMsgReplyer {
-	utils.ConsoleFormat(pay.PaySingleRefundApi)
-	refund := pay.ReFund{}
-	return &comm.NatsMsgReplyer{
-		Subject: pay.PaySingleRefundApi,
-		Entity:  func() interface{} { return &refund },
-		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
-			refund := entity.(*pay.ReFund)
-			// ???适配缺失退款理由
-			if refund.Reason == "" {
-				refund.Reason = "默认退款"
-			}
-			pay := PayMod(int(*refund.PayMod))
-			ctx := context.Background()
-			flag, err := pay.ReFundPay(ctx, refund)
-			return flag, err
-		},
-	}
-}
-
-// 退款查询
-func (s *SinglePay) RefundQuery() *comm.NatsMsgReplyer {
-	utils.ConsoleFormat(pay.PaySingleRefundQueryApi)
-
-	return &comm.NatsMsgReplyer{
-		Subject: pay.PaySingleRefundQueryApi,
-		Entity:  func() interface{} { return &model.ReFund{} },
-		Cb2: func(_ *nats.Msg, entity interface{}) (interface{}, error) {
-			m := entity.(*model.ReFund)
-			pay := PayMod(int(*m.PayMode))
-			ctx := context.Background()
-			status, err := pay.ReFundQuery(ctx, m)
-			return status, err
-		},
-	}
-}

+ 0 - 0
pay/bus/singleWechatPay.go → pay/bus/wechatPay.go