diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index 137d5ee..b853328 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -44,11 +44,16 @@ func Refund(c *gin.Context) { c.Set("OutTradeNo", req.OutTradeNo) appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) - refund, errCode := thirdpay.ThirdPayRefund(c.Request.Context(), req, appCheckInfo, c.ClientIP()) + check, errCode := thirdpay.ThirdPayRefundCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP()) if errCode != errorcode.Success { controllers.ApiRes(c, nil, errCode) return } + refund, errCode := thirdpay.ThirdPayRefund(check) + if errCode != errorcode.Success { + controllers.ApiRes(c, nil, errCode, refund.ThirdMsg) + return + } data := thirdpay.NewOrdersResp(refund.Order) var res front.ApiResponse res.Order = data diff --git a/app/http/controllers/front/payment_controller.go b/app/http/controllers/front/payment_controller.go index 05a0a0c..3c27df8 100644 --- a/app/http/controllers/front/payment_controller.go +++ b/app/http/controllers/front/payment_controller.go @@ -13,6 +13,7 @@ import ( "github.com/go-pay/gopay/alipay" "github.com/go-pay/gopay/wechat/v3" "github.com/qit-team/snow-core/log/logger" + "io/ioutil" "strconv" "net/http" @@ -135,3 +136,56 @@ func AliCallback(c *gin.Context) { c.String(http.StatusOK, "%s", "success") return } + +func BrokerWechatUrl(c *gin.Context) { + url := c.Query("url") + if url == "" { + c.String(400, "url is empty") + return + } + + method := "GET" + client := &http.Client{} + req, err := http.NewRequest(method, url, nil) + if err != nil { + fmt.Println(err) + return + } + req.Header.Add("Referer", "https://cardmallpay.85938.cn") + req.Header.Add("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + req.Header.Add("Accept", "*/*") + req.Header.Add("Host", "wx.tenpay.com") + req.Header.Add("Connection", "keep-alive") + + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + return + } + defer res.Body.Close() + + // 读取响应体 + body, err := ioutil.ReadAll(res.Body) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + // 复制响应头部(可选,根据需要选择需要的头部) + for name, values := range res.Header { + // 注意:某些头部(如Content-Length)在转发时可能需要重新计算 + for _, value := range values { + c.Writer.Header().Add(name, value) + } + } + + // 设置状态码 + c.Writer.WriteHeader(res.StatusCode) + + // 写入响应体 + _, err = c.Writer.Write(body) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } +} diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 71562ea..810cd2d 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -62,6 +62,8 @@ func RegisterRoute(router *gin.Engine) { pay.POST("/query", front.QueryOrder) //查询订单 pay.POST("/refund", front.Refund) } + // 测试微信支付唤起 + v1.GET("/brokerWechatUrl", front.BrokerWechatUrl) } router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/app/services/thirdpay/pay.go b/app/services/thirdpay/pay.go index 1de7650..382968c 100644 --- a/app/services/thirdpay/pay.go +++ b/app/services/thirdpay/pay.go @@ -46,21 +46,28 @@ func ThirdPayInfoCheck(ctx context.Context, payReq *front.PayReqs, appCheck *ser return } -func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck *services.AppCheck, ip string) (refund *thirdpay.Pay, errorCode int) { +func ThirdPayRefundCheck(ctx context.Context, refundReq *front.RefundReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck, errorCode int) { var req types.Reqs copier.Copy(&req, refundReq) - check := thirdpay.NewPayCheck(&ctx, &req, appCheck, ip) + check = thirdpay.NewPayCheck(&ctx, &req, appCheck, ip) // 校验表单 check.CheckPayInfo() - if check.CheckCode != errorcode.Success { - return nil, check.CheckCode + return check, check.CheckCode +} + +func ThirdPayRefund(check *thirdpay.PayCheck) (refund *thirdpay.Pay, errorCode int) { + + refund = thirdpay.NewPayWithPayCheck(check) + if refund.CheckRefundOrder() { + refund.CreateOrder(common.ORDER_TYPE_REFUND) + if refund.PayCode != errorcode.Success { + return refund, refund.PayCode + } } - check.CheckOrderRefund() - if check.CheckCode != errorcode.Success { - return nil, check.CheckCode - } - refund, errorCode = ThirdRefund(check) - return + + refund.Refund() + return refund, refund.PayCode + } func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { @@ -76,16 +83,3 @@ func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { } return } - -func ThirdRefund(check *thirdpay.PayCheck) (refund *thirdpay.Pay, errCode int) { - refund = thirdpay.NewPayWithPayCheck(check) - if refund.CheckRefundOrder() { - refund.CreateOrder(common.ORDER_TYPE_REFUND) - if refund.PayCode != errorcode.Success { - return refund, refund.PayCode - } - } - - refund.Refund() - return refund, refund.PayCode -}