From fc0680609ef84df9cab430afa6e07f46db5cc732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Thu, 4 Jul 2024 15:33:44 +0800 Subject: [PATCH] =?UTF-8?q?plugin=20=E4=BA=91=E9=97=AA=E4=BB=98=EF=BC=8C?= =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/union_pay/internal/po/notify.go | 11 ++--- plugins/union_pay/internal/po/order.go | 2 +- plugins/union_pay/internal/po/po.go | 2 +- plugins/union_pay/internal/po/query.go | 31 +++++++++----- plugins/union_pay/internal/transform.go | 37 ++++++++++++++--- plugins/union_pay/internal/union_pay.go | 27 +++++++++--- plugins/union_pay/internal/union_pay_test.go | 4 +- plugins/union_pay/internal/vo/opera_st.go | 42 +++++++++++++++++++ plugins/union_pay/internal/vo/status.go | 43 -------------------- plugins/zltx/internal/transform.go | 4 +- plugins/zltx/internal/zltx.go | 4 +- 11 files changed, 129 insertions(+), 78 deletions(-) create mode 100644 plugins/union_pay/internal/vo/opera_st.go delete mode 100644 plugins/union_pay/internal/vo/status.go diff --git a/plugins/union_pay/internal/po/notify.go b/plugins/union_pay/internal/po/notify.go index 8535670..63d7ee0 100644 --- a/plugins/union_pay/internal/po/notify.go +++ b/plugins/union_pay/internal/po/notify.go @@ -1,11 +1,8 @@ package po -import "plugins/union_pay/internal/vo" - type Notify struct { - MerchantId int `json:"merchantId" validate:"required"` - OutTradeNo string `json:"outTradeNo" validate:"required"` - RechargeAccount string `json:"rechargeAccount" validate:"required"` - Status vo.OrderStatus `json:"status" validate:"required"` - Sign string `json:"sign" validate:"required"` + MerchantId int `json:"merchantId" validate:"required"` + OutTradeNo string `json:"outTradeNo" validate:"required"` + RechargeAccount string `json:"rechargeAccount" validate:"required"` + Sign string `json:"sign" validate:"required"` } diff --git a/plugins/union_pay/internal/po/order.go b/plugins/union_pay/internal/po/order.go index 5ef21d3..dead844 100644 --- a/plugins/union_pay/internal/po/order.go +++ b/plugins/union_pay/internal/po/order.go @@ -34,5 +34,5 @@ type OrderResp struct { } func (o *OrderResp) GetMsg() string { - return fmt.Sprintf("Msg:%s,SubMsg:%s,自定处理msg:%s", o.Msg, o.SubMsg, o.SubCode.GetMsg()) + return fmt.Sprintf("Msg:[%s],SubMsg:[%s],自定处理msg:[%s]", o.Msg, o.SubMsg, o.SubCode.GetMsg()) } diff --git a/plugins/union_pay/internal/po/po.go b/plugins/union_pay/internal/po/po.go index 7cff32b..98b9e5f 100644 --- a/plugins/union_pay/internal/po/po.go +++ b/plugins/union_pay/internal/po/po.go @@ -46,7 +46,7 @@ func (req *QueryReq) Validate() error { } func (req *QueryReq) GetReId() string { - return "" + return req.TraceId } func (req *QueryReq) ToJson() []byte { diff --git a/plugins/union_pay/internal/po/query.go b/plugins/union_pay/internal/po/query.go index 7240475..f7be60c 100644 --- a/plugins/union_pay/internal/po/query.go +++ b/plugins/union_pay/internal/po/query.go @@ -1,18 +1,29 @@ package po -import "plugins/union_pay/internal/vo" +import ( + "fmt" + "plugins/union_pay/internal/vo" +) type QueryReq struct { - MerchantId string `validate:"required"` - OutTradeNo string `validate:"required"` - TimeStamp int64 `validate:"required"` - Version string `validate:"required"` - Sign string + ChNlId string `validate:"required" json:"chnlId"` // 渠道方代码-必填 + Cmd string `validate:"required" json:"cmd"` + OrigQid string `validate:"required" json:"origQid"` + OrigDate string `validate:"required" json:"origDate"` + TraceId string `validate:"required" json:"traceId"` } type QueryResp struct { - Code vo.Code `json:"code"` - Status vo.OrderStatus `json:"status"` - Message string `json:"message"` - OutTradeNo string `json:"outTradeNo"` + Code vo.Code `json:"code"` + Msg string `json:"msg"` + SubCode vo.SubCode `json:"subCode"` + SubMsg string `json:"subMsg"` + OperaSt vo.OperaSt `json:"operaSt"` + CouponCd string `json:"couponCd"` + CouponBeginTs string `json:"couponBeginTs"` + CouponEndTs string `json:"couponEndTs"` +} + +func (o *QueryResp) GetMsg() string { + return fmt.Sprintf("Msg:[%s],SubMsg:[%s],自定处理msg:[%s]", o.Msg, o.SubMsg, o.SubCode.GetMsg()) } diff --git a/plugins/union_pay/internal/transform.go b/plugins/union_pay/internal/transform.go index 5a03d08..8ed00d0 100644 --- a/plugins/union_pay/internal/transform.go +++ b/plugins/union_pay/internal/transform.go @@ -19,7 +19,7 @@ type ConfigExtra struct { RsaNpk string `json:"rsa_npk"` // 回调公钥 } -func transformConfig(config *proto.Config) (*Config, error) { +func transConfig(config *proto.Config) (*Config, error) { var extra ConfigExtra err := json.Unmarshal(config.Extra, &extra) if err != nil { @@ -31,11 +31,11 @@ func transformConfig(config *proto.Config) (*Config, error) { }, nil } -func rechargeReq(in *proto.OrderRequest, chNlId string) *po.OrderReq { - type Extra struct { +func orderReq(in *proto.OrderRequest, chNlId string) *po.OrderReq { + type OrderExtra struct { OrderDt string `json:"orderDt"` } - var e Extra + var e OrderExtra _ = json.Unmarshal(in.Order.Extra, &e) return &po.OrderReq{ @@ -53,7 +53,7 @@ func rechargeReq(in *proto.OrderRequest, chNlId string) *po.OrderReq { } } -func rechargeResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse { +func orderResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse { data, _ := json.Marshal(resp) result := &proto.Result{ OrderNo: request.Order.OrderNo, @@ -69,3 +69,30 @@ func rechargeResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderRe } return &proto.OrderResponse{Result: result} } + +func queryReq(in *proto.QueryRequest, chNlId string) *po.QueryReq { + type OrderExtra struct { + OrigDate string `json:"origDate"` + } + var e OrderExtra + _ = json.Unmarshal(in.Order.Extra, &e) + return &po.QueryReq{ + ChNlId: chNlId, + Cmd: "couponAcqQuery", + OrigQid: in.Order.OrderNo, + OrigDate: e.OrigDate, + TraceId: in.Order.OrderNo, + } +} + +func queryResp(request *proto.QueryRequest, resp po.QueryResp) *proto.QueryResponse { + data, _ := json.Marshal(resp) + result := &proto.Result{ + OrderNo: request.Order.OrderNo, + TradeNo: resp.CouponCd, + Status: resp.OperaSt.GetOrderStatus(), + Message: resp.GetMsg(), + Data: data, + } + return &proto.QueryResponse{Result: result} +} diff --git a/plugins/union_pay/internal/union_pay.go b/plugins/union_pay/internal/union_pay.go index aa75b52..49fbdce 100644 --- a/plugins/union_pay/internal/union_pay.go +++ b/plugins/union_pay/internal/union_pay.go @@ -29,12 +29,14 @@ const ( type UnionPayService struct{} func (p *UnionPayService) Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error) { - conf, err := transformConfig(request.Config) + conf, err := transConfig(request.Config) if err != nil { return nil, err } - uv := rechargeReq(request, conf.Extra.ChNlId) - + uv := orderReq(request, conf.Extra.ChNlId) + if err = uv.Validate(); err != nil { + return nil, err + } var response po.OrderResp url := fmt.Sprintf("%s%s", request.Config.BaseUri, orderMethod) err = requests.URL(url).Headers(headers(conf, uv, orderBizMethod)).BodyJSON(uv).ToJSON(&response).Fetch(ctx) @@ -42,11 +44,26 @@ func (p *UnionPayService) Order(ctx context.Context, request *proto.OrderRequest return nil, fmt.Errorf("请求异常,msg:" + err.Error()) } - return rechargeResp(request, response), nil + return orderResp(request, response), nil } func (p *UnionPayService) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) { - return nil, nil + conf, err := transConfig(request.Config) + if err != nil { + return nil, err + } + uv := queryReq(request, conf.Extra.ChNlId) + if err = uv.Validate(); err != nil { + return nil, err + } + var response po.QueryResp + url := fmt.Sprintf("%s%s", request.Config.BaseUri, queryMethod) + err = requests.URL(url).Headers(headers(conf, uv, queryBizMethod)).BodyJSON(uv).ToJSON(&response).Fetch(ctx) + if err != nil { + return nil, fmt.Errorf("请求异常,msg:" + err.Error()) + } + + return queryResp(request, response), nil } func (p *UnionPayService) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { diff --git a/plugins/union_pay/internal/union_pay_test.go b/plugins/union_pay/internal/union_pay_test.go index 308fe42..ce377ca 100644 --- a/plugins/union_pay/internal/union_pay_test.go +++ b/plugins/union_pay/internal/union_pay_test.go @@ -53,10 +53,10 @@ func TestQuery(t *testing.T) { request := &proto.QueryRequest{ Config: config, Order: &proto.QueryRequest_Order{ - OrderNo: "test_zltx_4", + OrderNo: "240403164049635931", TradeNo: "", Account: "", - Extra: nil, + Extra: []byte(`{"origDate":"20240704"}`), }, } t.Run("TestQuery", func(t *testing.T) { diff --git a/plugins/union_pay/internal/vo/opera_st.go b/plugins/union_pay/internal/vo/opera_st.go new file mode 100644 index 0000000..b758ee1 --- /dev/null +++ b/plugins/union_pay/internal/vo/opera_st.go @@ -0,0 +1,42 @@ +package vo + +import "codeup.aliyun.com/6552e56cc3b2728a4557fc18/plugin/proto" + +// OperaSt 原操作状态;10表示原操作未被受理;00表示原操作成功;01表示原操作结果未明;其他表示原操作失败 +type OperaSt string + +const ( + OperaStSuccess OperaSt = "00" + OperaStNotAccepted OperaSt = "10" + OperaStResultIsNotClear OperaSt = "01" +) + +var operaStMap = map[OperaSt]string{ + OperaStSuccess: "原操作成功", + OperaStNotAccepted: "原操作未被受理", + OperaStResultIsNotClear: "原操作结果未明", +} + +var queryOrderStatusMap = map[OperaSt]proto.Status{ + OperaStSuccess: proto.Status_SUCCESS, + OperaStResultIsNotClear: proto.Status_ING, + OperaStNotAccepted: proto.Status_INVALID, +} + +func (o OperaSt) GetText() string { + msg, ok := operaStMap[o] + if !ok { + return "原操作失败" + } + return msg +} + +func (o OperaSt) GetOrderStatus() proto.Status { + if o == "" { + return proto.Status_INVALID + } + if resultStatus, ok := queryOrderStatusMap[o]; ok { + return resultStatus + } + return proto.Status_FAIL +} diff --git a/plugins/union_pay/internal/vo/status.go b/plugins/union_pay/internal/vo/status.go deleted file mode 100644 index 2d1a8be..0000000 --- a/plugins/union_pay/internal/vo/status.go +++ /dev/null @@ -1,43 +0,0 @@ -package vo - -import ( - "codeup.aliyun.com/6552e56cc3b2728a4557fc18/plugin/proto" -) - -type OrderStatus string - -const ( - OrderSuccess OrderStatus = "01" // 充值成功 - OrderPending OrderStatus = "02" // 充值处理中 - OrderFail OrderStatus = "03" // 充值失败 - OrderAbnormal OrderStatus = "04" // 充值异常,处理中 -) - -var orderStatusMap = map[OrderStatus]proto.Status{ - OrderSuccess: proto.Status_SUCCESS, - OrderPending: proto.Status_ING, - OrderFail: proto.Status_FAIL, -} - -func (o OrderStatus) IsSuccess() bool { - return o == OrderSuccess -} - -func (o OrderStatus) IsPending() bool { - return o == OrderPending -} - -func (o OrderStatus) IsFail() bool { - return o == OrderFail -} - -func (o OrderStatus) IsAbnormal() bool { - return o == OrderAbnormal -} - -func (o OrderStatus) GetOrderStatus() proto.Status { - if resultStatus, ok := orderStatusMap[o]; ok { - return resultStatus - } - return proto.Status_ING -} diff --git a/plugins/zltx/internal/transform.go b/plugins/zltx/internal/transform.go index f126f37..5275bcd 100644 --- a/plugins/zltx/internal/transform.go +++ b/plugins/zltx/internal/transform.go @@ -7,7 +7,7 @@ import ( "time" ) -func rechargeReq(in *proto.OrderRequest) *po.OrderReq { +func orderReq(in *proto.OrderRequest) *po.OrderReq { return &po.OrderReq{ MerchantId: in.Config.AppId, OutTradeNo: in.Order.OrderNo, @@ -21,7 +21,7 @@ func rechargeReq(in *proto.OrderRequest) *po.OrderReq { } } -func rechargeResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse { +func orderResp(request *proto.OrderRequest, resp po.OrderResp) *proto.OrderResponse { data, _ := json.Marshal(resp) return &proto.OrderResponse{ Result: &proto.Result{ diff --git a/plugins/zltx/internal/zltx.go b/plugins/zltx/internal/zltx.go index 8f0c8ee..a5b64c3 100644 --- a/plugins/zltx/internal/zltx.go +++ b/plugins/zltx/internal/zltx.go @@ -26,7 +26,7 @@ const ( type ZltxService struct{} func (p *ZltxService) Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error) { - uv, err := req(rechargeReq(request), request.Config.AppKey) + uv, err := req(orderReq(request), request.Config.AppKey) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func (p *ZltxService) Order(ctx context.Context, request *proto.OrderRequest) (* return nil, fmt.Errorf("请求错误,msg:" + response.Message) } - return rechargeResp(request, response), nil + return orderResp(request, response), nil } func (p *ZltxService) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {