diff --git a/api/h5/index.go b/api/h5/index.go index 80d8534..0204935 100644 --- a/api/h5/index.go +++ b/api/h5/index.go @@ -313,6 +313,22 @@ func GatewayPay(c *gin.Context) { return } +func UnionPay(c *gin.Context) { + c.Writer.Header().Set("content-type", "text/html") + token := c.DefaultQuery("token", "") + if token == "" { + c.Writer.WriteString("token empty") + return + } + body, err := service.PaymentService.UnionPay(token) + if err != nil { + c.Writer.WriteString(err.Error()) + return + } + c.Writer.WriteString(body) + return +} + func BankCardUnbind(c *gin.Context) (result.Data, error) { err := service.UserService.BankCardUnbind(user(c)) if err != nil { diff --git a/route/h5.go b/route/h5.go index fd4a372..a367a81 100644 --- a/route/h5.go +++ b/route/h5.go @@ -18,6 +18,7 @@ func h5RouteInit(r *gin.Engine) { h5Group.Match([]string{"POST", "OPTIONS"}, "/binding-pay-notify", h5.BindingPayNotify) h5Group.Match([]string{"GET", "OPTIONS"}, "/test-gateway-pay", h5.GatewayPay) h5Group.Match([]string{"GET", "OPTIONS"}, "/gateway-pay", h5.GatewayPay) + h5Group.Match([]string{"GET", "OPTIONS"}, "/union-pay", h5.UnionPay) h5Group.Match([]string{"POST", "GET", "OPTIONS"}, "/gateway-pay-ret", h5.GatewayPayRet) authGroup := h5Group.Group("") diff --git a/service/payment_service.go b/service/payment_service.go index ee56e40..9301c6f 100644 --- a/service/payment_service.go +++ b/service/payment_service.go @@ -53,6 +53,8 @@ func (s paymentService) Payment(userID int, paymentRequest request.PaymentReques data, err = s.bindingPayment(paymentRequest, p) } else if paymentRequest.PayType == "gateway" { data, err = s.getGatewayPayUrl(p) + } else if paymentRequest.PayType == "union" { + data, err = s.getUnionPayUrl(p) } else { err = errors.NewBusinessError("payType参数错误") } @@ -439,3 +441,35 @@ func (s paymentService) getGatewayPayUrl(pm *model.Payment) (result.Data, error) data := result.Data{"url": "http://api.wrtcjt.com/h5/gateway-pay?token=" + token, "orderId": ""} return data, nil } + +func (s paymentService) getUnionPayUrl(pm *model.Payment) (result.Data, error) { + token := utils.Md5(pm.PaymentNo + strconv.Itoa(pm.UserID) + "_union") + pm.Token = token + err := global.DB.Save(pm).Error + if err != nil { + return nil, err + } + data := result.Data{"url": "http://api.wrtcjt.com/h5/union-pay?token=" + token, "orderId": ""} + return data, nil +} + +func (s paymentService) UnionPay(token string) (string, error) { + pm := &model.Payment{} + err := global.DB.Where("token", token).Where("status", 0).First(&pm).Error + + if err != nil { + return "", err + } + p := param.UnionOrderParam{} + p.ReqSn = pm.PaymentNo + p.TrxAmt = strconv.FormatFloat(pm.Amount*100, 'f', 0, 64) + p.LimitPay = "no_credit" + p.RetUrl = "http://api.wrtcjt.com/h5/gateway-pay-ret" + p.NotifyUrl = "http://api.wrtcjt.com/h5/binding-pay-notify" + p.Body = "充值" + p.Charset = "UTF-8" + p.PayType = "VSP511" + + str := tlpay.TLPay.UnionPay(p) + return str, nil +} diff --git a/utils/tlpay/api.go b/utils/tlpay/api.go index c7b65aa..0dc5b1f 100644 --- a/utils/tlpay/api.go +++ b/utils/tlpay/api.go @@ -237,10 +237,21 @@ func (api *tlPay) GatewayPay(p param.GatewayPayParam) string { data = api.SetCommonParams(data, false) sign, _ := api.sign(data) data.Set("sign", sign) - return api.BuildForm(data) + payConfig := global.Config.TlPay + formUrl := payConfig.BaseUrl + "/apiweb/gateway/pay" + return api.BuildForm(formUrl, data) +} + +func (api *tlPay) UnionPay(p param.UnionOrderParam) string { + data, _ := utils.StructToURLValues(p) + data = api.SetCommonParams(data, false) + sign, _ := api.sign(data) + data.Set("sign", sign) + formUrl := "https://syb.allinpay.com/apiweb/h5unionpay/unionorder" + return api.BuildForm(formUrl, data) } -func (api *tlPay) BuildForm(data url.Values) string { +func (api *tlPay) BuildForm(formUrl string, data url.Values) string { htmlBegin := ` @@ -249,7 +260,7 @@ func (api *tlPay) BuildForm(data url.Values) string { @@ -259,18 +270,16 @@ func (api *tlPay) BuildForm(data url.Values) string { ` ` - return htmlBegin + api.buildFormHtml(data) + htmlEnd + return htmlBegin + api.buildFormHtml(formUrl, data) + htmlEnd } -func (api *tlPay) buildFormHtml(data url.Values) string { - payConfig := global.Config.TlPay - formUrl := payConfig.BaseUrl + "/apiweb/gateway/pay" +func (api *tlPay) buildFormHtml(formUrl string, data url.Values) string { formHtml := "
" for key, value := range data { formHtml += "" } - //formHtml += "" + formHtml += "" formHtml += "
" return formHtml } diff --git a/utils/tlpay/param/union_order_param.go b/utils/tlpay/param/union_order_param.go new file mode 100644 index 0000000..059481f --- /dev/null +++ b/utils/tlpay/param/union_order_param.go @@ -0,0 +1,35 @@ +package param + +type UnionOrderParam struct { + CusID string `json:"cusid" form:"cusid"` + AppID string `json:"appid" form:"appid"` + ReqTime string `json:"reqtime" form:"reqtime"` + ReqIp string `json:"reqip" form:"reqip"` + Version string `json:"version" form:"version"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + + ReqSn string `json:"reqsn" form:"reqsn"` + PayType string `json:"paytype" form:"paytype"` + TrxAmt string `json:"trxamt" form:"trxamt"` + FqNum string `json:"fqnum" form:"fqnum"` + Charset string `json:"charset" form:"charset"` + RetUrl string `json:"returl" form:"returl"` + NotifyUrl string `json:"notify_url" form:"notify_url"` + Body string `json:"body" form:"body"` + Remark string `json:"remark" form:"remark"` + ValidTime string `json:"validtime" form:"validtime"` + ExpireTime string `json:"expiretime" form:"expiretime"` + LimitPay string `json:"limit_pay" form:"limit_pay"` + GoodsTag string `json:"goods_tag" form:"goods_tag"` + BenefitDetail string `json:"benefitdetail" form:"benefitdetail"` + AsInfo string `json:"asinfo" form:"asinfo"` + SubBranch string `json:"subbranch" form:"subbranch"` + ExtendParams string `json:"extendparams" form:"extendparams"` + TrueName string `json:"truename" form:"truename"` + IdNo string `json:"idno" form:"idno"` + IsHide string `json:"ishide" form:"ishide"` + UnPid string `json:"unpid" form:"unpid"` + TermInfo string `json:"terminfo" form:"terminfo"` +}