From f34bf3670e71a587d8b77f3b829168f9de86b4c4 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Mon, 27 May 2024 16:48:16 +0800 Subject: [PATCH] yop --- api/proto/vas/proto/pay.go | 44 +++++++------ api/proto/vas/proto/vas.go | 14 +++-- app/mix/conf/cfg.go | 1 + app/mix/controller/init.go | 1 + app/mix/controller/yeepay_callback.go | 20 ++++++ app/mix/service/logic/vas.go | 85 ++++++++++++++++++++------ app/mix/service/logic/vas_zone.go | 14 +++-- app/mix/service/service.go | 3 + etc/mix/mix-local.yaml | 2 +- library/payclients/yeepaycli/client.go | 63 +++++++++++++++++-- library/payclients/yeepaycli/x.json | 16 +++++ 11 files changed, 210 insertions(+), 53 deletions(-) create mode 100644 app/mix/controller/yeepay_callback.go create mode 100644 library/payclients/yeepaycli/x.json diff --git a/api/proto/vas/proto/pay.go b/api/proto/vas/proto/pay.go index 4fb2ac20..28d81770 100644 --- a/api/proto/vas/proto/pay.go +++ b/api/proto/vas/proto/pay.go @@ -26,12 +26,14 @@ type GetMembershipProductListData struct { // 创建订单 const ( - PayTypeOp = "op" // op直冲 - PayTypeAlipay = "alipay" // 支付宝 - PayTypeAlipayH5 = "alipay_h5" // 支付宝 h5 - PayTypeWxpayNative = "wxpay_native" // 微信 native - PayTypeWxpayJsapi = "wxpay_jsapi" // 微信 jsapi - PayTypeWxpayH5 = "wxpay_h5" // 微信支付 h5 + PayTypeOp = "op" // op直冲 + PayTypeAlipay = "alipay" // 支付宝 + PayTypeAlipayH5 = "alipay_h5" // 支付宝 h5 + PayTypeWxpayNative = "wxpay_native" // 微信 native + PayTypeWxpayJsapi = "wxpay_jsapi" // 微信 jsapi + PayTypeWxpayH5 = "wxpay_h5" // 微信支付 h5 + PayTypeYeepayAlipayH5 = "yeepay_alipay_h5" // 易宝 微信h5支付 + PayTypeYeepayWxpayH5 = "yeepay_wxpay_h5" // 易宝 微信h5支付 ) const ( @@ -63,12 +65,14 @@ type CreateOrderReq struct { } type CreateOrderData struct { - OrderId string `json:"order_id"` // 订单id - AlipayParamStr string `json:"alipay_param_str"` // 支付宝 app支付参数 - AlipayH5ParamStr string `json:"alipay_h5_param_str"` // 支付宝 h5支付参数 - WxpayNativeParamStr string `json:"wxpay_native_param_str"` // 微信支付 native支付参数 - WxpayJsapiParamObj wxpaycli.JsapiPayResp `json:"wxpay_jsapi_param_obj"` // 微信支付 jsapi支付参数 - WxpayH5ParamStr string `json:"wxpay_h5_param_str"` // 微信支付 h5支付参数 + OrderId string `json:"order_id"` // 订单id + AlipayParamStr string `json:"alipay_param_str"` // 支付宝 app支付参数 + AlipayH5ParamStr string `json:"alipay_h5_param_str"` // 支付宝 h5支付参数 + WxpayNativeParamStr string `json:"wxpay_native_param_str"` // 微信支付 native支付参数 + WxpayJsapiParamObj wxpaycli.JsapiPayResp `json:"wxpay_jsapi_param_obj"` // 微信支付 jsapi支付参数 + WxpayH5ParamStr string `json:"wxpay_h5_param_str"` // 微信支付 h5支付参数 + YeepayAlipayH5ParamStr string `json:"yeepay_alipay_h5_param_str"` // 易宝支付 支付宝-h5支付参数 + YeepayWxpayH5ParamStr string `json:"yeepay_wxpay_h5_param_str"` // 易宝支付 微信支付-h5支付参数 } // 预解锁联系方式 @@ -147,13 +151,15 @@ type H5DirectUnlockWechatReq struct { } type H5DirectUnlockWechatData struct { - CoinEnough int32 `json:"coin_enough"` // 0:不够(用下面的支付宝参数),1:够 - OrderId string `json:"order_id"` // 订单id - AlipayParamStr string `json:"alipay_param_str"` // 支付宝 app支付参数 - AlipayH5ParamStr string `json:"alipay_h5_param_str"` // 支付宝 h5支付参数 - WxpayNativeParamStr string `json:"wxpay_native_param_str"` // 微信支付 native支付参数 - WxpayJsapiParamObj wxpaycli.JsapiPayResp `json:"wxpay_jsapi_param_obj"` // 微信支付 native - WxpayH5ParamStr string `json:"wxpay_h5_param_str"` // 微信支付 h5支付参数 + CoinEnough int32 `json:"coin_enough"` // 0:不够(用下面的支付宝参数),1:够 + OrderId string `json:"order_id"` // 订单id + AlipayParamStr string `json:"alipay_param_str"` // 支付宝 app支付参数 + AlipayH5ParamStr string `json:"alipay_h5_param_str"` // 支付宝 h5支付参数 + WxpayNativeParamStr string `json:"wxpay_native_param_str"` // 微信支付 native支付参数 + WxpayJsapiParamObj wxpaycli.JsapiPayResp `json:"wxpay_jsapi_param_obj"` // 微信支付 native + WxpayH5ParamStr string `json:"wxpay_h5_param_str"` // 微信支付 h5支付参数 + YeepayAlipayH5ParamStr string `json:"yeepay_alipay_h5_param_str"` // 易宝支付 支付宝-h5支付参数 + YeepayWxpayH5ParamStr string `json:"yeepay_wxpay_h5_param_str"` // 易宝支付 微信支付-h5支付参数 } // 支付宝回调参数 diff --git a/api/proto/vas/proto/vas.go b/api/proto/vas/proto/vas.go index f282d539..4333ed68 100644 --- a/api/proto/vas/proto/vas.go +++ b/api/proto/vas/proto/vas.go @@ -162,12 +162,14 @@ type ZoneCreateOrderReq struct { } type ZoneCreateOrderData struct { - OrderId string `json:"order_id"` // 订单id - AlipayParamStr string `json:"alipay_param_str"` // 支付宝 app支付参数 - AlipayH5ParamStr string `json:"alipay_h5_param_str"` // 支付宝 h5支付参数 - WxpayNativeParamStr string `json:"wxpay_native_param_str"` // 微信支付 native支付参数 - WxpayJsapiParamObj wxpaycli.JsapiPayResp `json:"wxpay_jsapi_param_obj"` // 微信支付 jsapi支付参数 - WxpayH5ParamStr string `json:"wxpay_h5_param_str"` // 微信支付 h5支付参数 + OrderId string `json:"order_id"` // 订单id + AlipayParamStr string `json:"alipay_param_str"` // 支付宝 app支付参数 + AlipayH5ParamStr string `json:"alipay_h5_param_str"` // 支付宝 h5支付参数 + WxpayNativeParamStr string `json:"wxpay_native_param_str"` // 微信支付 native支付参数 + WxpayJsapiParamObj wxpaycli.JsapiPayResp `json:"wxpay_jsapi_param_obj"` // 微信支付 jsapi支付参数 + WxpayH5ParamStr string `json:"wxpay_h5_param_str"` // 微信支付 h5支付参数 + YeepayAlipayH5ParamStr string `json:"yeepay_alipay_h5_param_str"` // 易宝支付 支付宝-h5支付参数 + YeepayWxpayH5ParamStr string `json:"yeepay_wxpay_h5_param_str"` // 易宝支付 微信支付-h5支付参数 } type ZoneCreateOrderResp struct { diff --git a/app/mix/conf/cfg.go b/app/mix/conf/cfg.go index 0d6f77da..fb0ef756 100644 --- a/app/mix/conf/cfg.go +++ b/app/mix/conf/cfg.go @@ -22,4 +22,5 @@ type ConfigSt struct { DingTalkRobot *configcenter.DingTalkRobotConfig `json:"ding_talk_robot" yaml:"ding_talk_robot"` // 钉钉机器人 VideoModeration *configcenter.VideoModerationConfig `json:"video_moderation" yaml:"video_moderation"` // 视频审核 ElasticSearch *configcenter.ElasticSearchConfig `json:"elastic_search" yaml:"elastic_search"` // es + Yeepay *configcenter.YeepayClientConfig `json:"yeepay" yaml:"yeepay"` } diff --git a/app/mix/controller/init.go b/app/mix/controller/init.go index 78cf5aef..d1dcfa13 100644 --- a/app/mix/controller/init.go +++ b/app/mix/controller/init.go @@ -293,6 +293,7 @@ func Init(r *gin.Engine) { extVasPayGroup := r.Group("/ext/vas") extVasPayGroup.POST("alipay_callback", AlipayCallback) extVasPayGroup.POST("wxpay_callback", WxpayCallback) + extVasPayGroup.POST("yeepay_callback", YeepayCallback) opVasPayGroup := r.Group("/op/vas", PrepareOp()) opVasPayGroup.POST("create_order", middleware.JSONParamValidator(vasproto.OpCreateOrderReq{}), OpCreateOrder) diff --git a/app/mix/controller/yeepay_callback.go b/app/mix/controller/yeepay_callback.go new file mode 100644 index 00000000..c3453af7 --- /dev/null +++ b/app/mix/controller/yeepay_callback.go @@ -0,0 +1,20 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "service/bizcommon/util" + "service/library/logger" + "service/library/payclients/yeepaycli" +) + +func YeepayCallback(ctx *gin.Context) { + //req, _ := ctx.GetRawData() + //logger.Info("_AlipayCallback req: %v", string(req)) + err := yeepaycli.GetDefaultYeepayClient().ParseNotify(ctx.Request) + if err != nil { + logger.Error("ParseNotify fail, req: %v, err: %v", util.ToJson(""), err) + return + } + + //ctx.String(200, "success") +} diff --git a/app/mix/service/logic/vas.go b/app/mix/service/logic/vas.go index be1b7d49..697b44ae 100644 --- a/app/mix/service/logic/vas.go +++ b/app/mix/service/logic/vas.go @@ -21,6 +21,7 @@ import ( "service/library/logger" "service/library/payclients/alipaycli" "service/library/payclients/wxpaycli" + "service/library/payclients/yeepaycli" "time" "github.com/go-pay/gopay/alipay" @@ -129,11 +130,13 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data orderId = idgenerator.GenOrderId() // 订单id ) var ( - alipayParamStr string - alipayH5ParamStr string - wxpayNativeParamStr string - wxpayJsapiParamObj wxpaycli.JsapiPayResp - wxpayH5ParamStr string + alipayParamStr string + alipayH5ParamStr string + wxpayNativeParamStr string + wxpayJsapiParamObj wxpaycli.JsapiPayResp + wxpayH5ParamStr string + yeepayAlipayH5ParamStr string + yeepayWxpayH5ParamStr string ) defer func() { @@ -296,6 +299,50 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data return } req.Oid3 = wxpayCli.AppId + case vasproto.PayTypeYeepayAlipayH5: + var aggPayResp *yeepaycli.AggPayResp + yeepayCli := yeepaycli.GetDefaultYeepayClient() + aggPayParam := &yeepaycli.AggPayParam{ + Description: product.Subject, + OutTradeNo: orderId, + TotalAmount: product.RealPrice, + PayWay: yeepaycli.YeepayWayH5, + PayChannel: yeepaycli.YeepayChannelAlipay, + Ip: ctx.ClientIP(), + } + // 给个默认ip + if len(aggPayParam.Ip) <= 0 { + aggPayParam.Ip = "112.124.18.6" + } + aggPayResp, err = yeepayCli.AggPay(ctx, aggPayParam) + if err != nil { + logger.Error("YeepayAlipayH5 fail, req: %v, aggPayParam: %v, err: %v", util.ToJson(req), util.ToJson(aggPayParam), err) + return + } + yeepayAlipayH5ParamStr = aggPayResp.PrePayTn + req.Oid3 = yeepayCli.AppId + case vasproto.PayTypeYeepayWxpayH5: + var aggPayResp *yeepaycli.AggPayResp + yeepayCli := yeepaycli.GetDefaultYeepayClient() + aggPayParam := &yeepaycli.AggPayParam{ + Description: product.Subject, + OutTradeNo: orderId, + TotalAmount: product.RealPrice, + PayWay: yeepaycli.YeepayWayH5, + PayChannel: yeepaycli.YeepayChannelWxpay, + Ip: ctx.ClientIP(), + } + // 给个默认ip + if len(aggPayParam.Ip) <= 0 { + aggPayParam.Ip = "112.124.18.6" + } + aggPayResp, err = yeepayCli.AggPay(ctx, aggPayParam) + if err != nil { + logger.Error("YeepayWxpayH5 fail, req: %v, aggPayParam: %v, err: %v", util.ToJson(req), util.ToJson(aggPayParam), err) + return + } + yeepayWxpayH5ParamStr = aggPayResp.PrePayTn + req.Oid3 = yeepayCli.AppId } // 添加订单 @@ -334,12 +381,14 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data } data = &vasproto.CreateOrderData{ - OrderId: orderId, - AlipayParamStr: alipayParamStr, - AlipayH5ParamStr: alipayH5ParamStr, - WxpayNativeParamStr: wxpayNativeParamStr, - WxpayJsapiParamObj: wxpayJsapiParamObj, - WxpayH5ParamStr: wxpayH5ParamStr, + OrderId: orderId, + AlipayParamStr: alipayParamStr, + AlipayH5ParamStr: alipayH5ParamStr, + WxpayNativeParamStr: wxpayNativeParamStr, + WxpayJsapiParamObj: wxpayJsapiParamObj, + WxpayH5ParamStr: wxpayH5ParamStr, + YeepayAlipayH5ParamStr: yeepayAlipayH5ParamStr, + YeepayWxpayH5ParamStr: yeepayWxpayH5ParamStr, } return } @@ -1113,12 +1162,14 @@ func (v *Vas) H5DirectUnlockWechat(ctx *gin.Context, req *vasproto.H5DirectUnloc return } data = &vasproto.H5DirectUnlockWechatData{ - OrderId: cData.OrderId, - AlipayParamStr: cData.AlipayParamStr, - AlipayH5ParamStr: cData.AlipayH5ParamStr, - WxpayNativeParamStr: cData.WxpayNativeParamStr, - WxpayJsapiParamObj: cData.WxpayJsapiParamObj, - WxpayH5ParamStr: cData.WxpayH5ParamStr, + OrderId: cData.OrderId, + AlipayParamStr: cData.AlipayParamStr, + AlipayH5ParamStr: cData.AlipayH5ParamStr, + WxpayNativeParamStr: cData.WxpayNativeParamStr, + WxpayJsapiParamObj: cData.WxpayJsapiParamObj, + WxpayH5ParamStr: cData.WxpayH5ParamStr, + YeepayAlipayH5ParamStr: cData.YeepayAlipayH5ParamStr, + YeepayWxpayH5ParamStr: cData.YeepayWxpayH5ParamStr, } return } diff --git a/app/mix/service/logic/vas_zone.go b/app/mix/service/logic/vas_zone.go index 1cc584c5..7d78eaf3 100644 --- a/app/mix/service/logic/vas_zone.go +++ b/app/mix/service/logic/vas_zone.go @@ -259,12 +259,14 @@ func (v *Vas) ZoneCreateOrder(ctx *gin.Context, req *vasproto.ZoneCreateOrderReq return nil, err } data := &vasproto.ZoneCreateOrderData{ - OrderId: createOrderData.OrderId, - AlipayParamStr: createOrderData.AlipayParamStr, - AlipayH5ParamStr: createOrderData.AlipayH5ParamStr, - WxpayNativeParamStr: createOrderData.WxpayNativeParamStr, - WxpayJsapiParamObj: createOrderData.WxpayJsapiParamObj, - WxpayH5ParamStr: createOrderData.WxpayH5ParamStr, + OrderId: createOrderData.OrderId, + AlipayParamStr: createOrderData.AlipayParamStr, + AlipayH5ParamStr: createOrderData.AlipayH5ParamStr, + WxpayNativeParamStr: createOrderData.WxpayNativeParamStr, + WxpayJsapiParamObj: createOrderData.WxpayJsapiParamObj, + WxpayH5ParamStr: createOrderData.WxpayH5ParamStr, + YeepayAlipayH5ParamStr: createOrderData.YeepayAlipayH5ParamStr, + YeepayWxpayH5ParamStr: createOrderData.YeepayWxpayH5ParamStr, } return data, nil } diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 01a5afad..7ecae0e5 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -58,6 +58,7 @@ import ( "service/library/mycrypto" "service/library/payclients/alipaycli" "service/library/payclients/wxpaycli" + "service/library/payclients/yeepaycli" "service/library/redis" "strings" "time" @@ -170,6 +171,8 @@ func (s *Service) Init(c any) (err error) { return } + yeepaycli.Init(cfg.Yeepay) + _DefaultToken = logic.NewToken(store, cfg.Crypto) _DefaultVeriCode = logic.NewVeriCode(store, cfg.Dysmsapi) _DefaultVeriCodeSendTimes = logic.NewVeriCodeSendTimes(store) diff --git a/etc/mix/mix-local.yaml b/etc/mix/mix-local.yaml index 5c90d6f5..cc24acfb 100644 --- a/etc/mix/mix-local.yaml +++ b/etc/mix/mix-local.yaml @@ -105,4 +105,4 @@ xxl_job: executor_ip: "127.0.0.1" executor_port: "9801" registry_key: "golang-jobs-executor" - log_path: "/Users/erwin/log/wishpal-ironfan/" \ No newline at end of file + log_path: "/Users/erwin/log/wishpal-ironfan/" diff --git a/library/payclients/yeepaycli/client.go b/library/payclients/yeepaycli/client.go index 1668f174..00f11f8f 100644 --- a/library/payclients/yeepaycli/client.go +++ b/library/payclients/yeepaycli/client.go @@ -2,10 +2,11 @@ package yeepaycli import ( "context" + "encoding/json" "github.com/yop-platform/yop-go-sdk/yop/client" "github.com/yop-platform/yop-go-sdk/yop/constants" "github.com/yop-platform/yop-go-sdk/yop/request" - "github.com/yop-platform/yop-go-sdk/yop/response" + yoputils "github.com/yop-platform/yop-go-sdk/yop/utils" "net/http" "service/bizcommon/util" "service/library/configcenter" @@ -13,6 +14,15 @@ import ( "time" ) +const ( + YeepayWayH5 = "H5_PAY" // h5支付 +) + +const ( + YeepayChannelWxpay = "WECHAT" // 易宝支付微信 + YeepayChannelAlipay = "ALIPAY" // 易宝支付支付宝 +) + const ( DefaultOrderTimeoutSeconds = 900 // 默认订单超时时间,单位: s ) @@ -50,8 +60,40 @@ func Init(cfg *configcenter.YeepayClientConfig) { } // 验签 -func (c *YeepayClient) ParseNotify() { +type YeepayNotify struct { + ChannelOrderId string `json:"channelOrderId"` + OrderId string `json:"orderId"` + BankOrderId string `json:"bankOrderId"` + PaySuccessDate string `json:"paySuccessDate"` + Channel string `json:"channel"` + PayWay string `json:"payWay"` + UniqueOrderNo string `json:"uniqueOrderNo"` + OrderAmount string `json:"orderAmount"` + PayAmount string `json:"payAmount"` + PayerInfo string `json:"payerInfo"` + RealPayAmount string `json:"realPayAmount"` + ParentMerchantNo string `json:"parentMerchantNo"` + MerchantNo string `json:"merchantNo"` + Status string `json:"status"` +} +func (c *YeepayClient) ParseNotify(req *http.Request) error { + if err := req.ParseForm(); err != nil { + logger.Error("Yeepay ParseForm fail, err: %v", err) + return err + } + var ( + response = req.Form.Get("response") + appKey = req.Form.Get("customerIdentification") + ) + logger.Info("YeepayCallback notify, response: %v, appKey: %v", response, appKey) + content, err := yoputils.DecryptCallback(request.YOP_PLATFORM_PUBLIC_KEY, c.PrivateKey, response) + if err != nil { + logger.Error("Yeepay DecryptCallback fail, form: %v, err: %v", req.Form.Encode(), err) + return err + } + logger.Info("Yeepay ParseNotify, %v", util.ToJson(content)) + return nil } // 易宝支付 聚合支付 @@ -65,7 +107,16 @@ type AggPayParam struct { Ip string } -func (c *YeepayClient) AggPay(ctx context.Context, param *AggPayParam) (resp *response.YopResponse, err error) { +type AggPayResp struct { + Code string `json:"code"` + Message string `json:"message"` + OrderId string `json:"orderId"` + UniqueOrderNo string `json:"uniqueOrderNo"` + BankOrderId string `json:"bankOrderId"` + PrePayTn string `json:"prePayTn"` +} + +func (c *YeepayClient) AggPay(ctx context.Context, param *AggPayParam) (resp *AggPayResp, err error) { req := request.NewYopRequest(constants.POST_HTTP_METHOD, "/rest/v1.0/aggpay/tutelage/pre-pay") req.AppId = c.AppId req.IsvPriKey = request.IsvPriKey{Value: c.PrivateKey, CertType: request.RSA2048} @@ -81,10 +132,14 @@ func (c *YeepayClient) AggPay(ctx context.Context, param *AggPayParam) (resp *re req.AddParam("scene", "ONLINE") req.AddParam("userIp", param.Ip) - resp, err = c.client.Request(req) + respMeta, err := c.client.Request(req) if err != nil { + logger.Error("yeepay AggPay fail, param: %v, respMeta: %v, err: %v", util.ToJson(req.Params), util.ToJson(respMeta), err) return } + resp = new(AggPayResp) + b, _ := json.Marshal(respMeta.Result) + _ = json.Unmarshal(b, resp) logger.Info("yeepay AggPay param: %v, resp: %v", util.ToJson(req.Params), util.ToJson(resp)) return } diff --git a/library/payclients/yeepaycli/x.json b/library/payclients/yeepaycli/x.json new file mode 100644 index 00000000..c160d4b3 --- /dev/null +++ b/library/payclients/yeepaycli/x.json @@ -0,0 +1,16 @@ +{ + "channelOrderId": "332022021622001422971445123456", + "orderId": "DQD620CUD57Q8A3123456", + "bankOrderId": "ST5207953516123456", + "paySuccessDate": "2022-02-16 20:26:05", + "channel": "ALIPAY", + "payWay": "MINI_PROGRAM", + "uniqueOrderNo": "1013202202160000003222123456", + "orderAmount": "2.00", + "payAmount": "2.00", + "payerInfo": "{\"bankCardNo\":\"\",\"bankId\":\"ALIPAY\",\"buyerLogonId\":\"136****1013\",\"mobilePhoneNo\":\"\",\"userID\":\"2088502619123456\"}", + "realPayAmount": "2.00", + "parentMerchantNo": "10085123456", + "merchantNo": "10085123456", + "status": "SUCCESS" +} \ No newline at end of file