Browse Source

生成支付链接

kobe6258 2 days ago
parent
commit
2a2826343a

+ 14 - 5
controllers/payment/payment_controller.go

@@ -24,9 +24,8 @@ type PayRequest struct {
 	ProductOrderNo string `json:"productOrderNo"`
 }
 type PayResponse struct {
-	ProductOrderNo string `json:"productOrderNo"`
-	TradeOrderNo   string `json:"tradeOrderNo"`
-	PaymentToken   string `json:"paymentToken"`
+	PaymentUrl   string `json:"paymentUrl"`
+	PaymentToken string `json:"paymentToken"`
 }
 
 // PayOrder  支付订单
@@ -97,14 +96,24 @@ func (pc *PaymentController) PayOrder() {
 			if err != nil {
 				pc.FailedResult("支付失败,正式用户信息不存在", result)
 			}
-			var payOrderDTO facade.PayOrderDTO
+			var payOrderDTO orderDomian.PayOrderDTO
 			payOrderDTO, err = facade.PayOrder(officialUser.ID, userInfo.Id, productOrder)
 			if err != nil {
 				pc.FailedResult("支付订单创建失败", result)
 				err = exception.NewWithException(exception.CreatePaymentOrderFailed, err.Error())
 				return
 			}
-			pc.SuccessResult("支付订单创建成功", payOrderDTO, result)
+			//将参数填充为URL地址直接返回前端
+			url, parseErr := orderDomian.CreatePaymentUrl(payOrderDTO)
+			if parseErr != nil {
+				pc.FailedResult("生成订单链接失败", result)
+				err = exception.NewWithException(exception.CreatePaymentOrderFailed, err.Error())
+				return
+			}
+			pc.SuccessResult("支付订单创建成功", PayResponse{
+				PaymentToken: payOrderDTO.PaymentToken,
+				PaymentUrl:   url,
+			}, result)
 			return
 		} else {
 			pc.FailedResult("商品订单处理中,请稍后再试", result)

+ 37 - 8
domian/order/trade_order.go

@@ -1,6 +1,7 @@
 package order
 
 import (
+	"bytes"
 	"encoding/json"
 	logger "eta/eta_mini_ht_api/common/component/log"
 	"eta/eta_mini_ht_api/models"
@@ -10,20 +11,34 @@ import (
 	"fmt"
 	"math/rand"
 	"strconv"
+	"text/template"
 	"time"
 )
 
 const (
-	RMB                      = "CNY"
-	AliPayWay     PaymentWay = "alipay"
-	WechatPay     PaymentWay = "wechat"
-	RefundSuccess            = "success"
-	RefundFail               = "fail"
-	PaySuccess               = "success"
-	PayFail                  = "fail"
+	RMB                       = "CNY"
+	AliPayWay      PaymentWay = "alipay"
+	WechatPay      PaymentWay = "wechat"
+	RefundSuccess             = "success"
+	RefundFail                = "fail"
+	PaySuccess                = "success"
+	PayFail                   = "fail"
+	PayUrlTemplate            = "{{.Url}}?appId={{.AppId}}&totalOrder={{.Amount}}&sorderNo={{.TradeOrderNo}}&title={{.ProductTitle}}&description={{.ProductDescription}}&buyerId={{.BuyerId}}&payType={{.PayType}}"
 )
 
 type PaymentWay string
+type PayOrderDTO struct {
+	Url                string
+	AppId              string
+	Amount             string
+	ProductTitle       string
+	ProductDescription string
+	BuyerId            int
+	PayType            string
+	ProductOrderNo     string
+	TradeOrderNo       string
+	PaymentToken       string
+}
 
 type TradeOrderDTO struct {
 	ID             int       `gorm:"column:id;primaryKey"`
@@ -41,6 +56,10 @@ type TradeOrderDTO struct {
 	DealTime       time.Time `gorm:"column:deal_time;type:datetime;comment:完成时间"`
 }
 
+func CreatePaymentUrl(meta PayOrderDTO) (urlInfo string, err error) {
+	urlInfo, err = generateMessage(meta, PayUrlTemplate)
+	return
+}
 func GenerateTradeOrderNo() string {
 	timestamp := time.Now().UnixNano() / 1000000 // 毫秒级时间戳
 	// 生成随机数
@@ -50,7 +69,17 @@ func GenerateTradeOrderNo() string {
 	orderNumber := fmt.Sprintf("T%d%06d", timestamp, randomPart)
 	return orderNumber
 }
-
+func generateMessage(data interface{}, tmpl string) (message string, err error) {
+	t := template.Must(template.New("messageTemplate").Parse(tmpl))
+	var buffer bytes.Buffer
+	err = t.Execute(&buffer, data)
+	if err != nil {
+		logger.Error("生成消息模板失败:%v", err)
+		return
+	}
+	message = buffer.String()
+	return
+}
 func CreateTradeOrder(userId, templateUserId int, productOrderNo, tradeOrderNo, merchantNo string) (err error) {
 	db := models.Main()
 	productOrder, err := order.GetOrderByUser(templateUserId, productOrderNo)

+ 9 - 13
service/facade/ht_trade_service.go

@@ -2,7 +2,9 @@ package facade
 
 import (
 	"eta/eta_mini_ht_api/api"
+	"eta/eta_mini_ht_api/common/component/config"
 	logger "eta/eta_mini_ht_api/common/component/log"
+	"eta/eta_mini_ht_api/common/contants"
 	payUtils "eta/eta_mini_ht_api/common/utils/payment"
 	"eta/eta_mini_ht_api/domian/order"
 	"eta/eta_mini_ht_api/service/payment"
@@ -17,19 +19,10 @@ const (
 
 var (
 	htTradeApi = api.GetPaymentInstance()
+	htConfig   = config.GetConfig(contants.HT).(*config.HTBizConfig)
 )
 
-type PayOrderDTO struct {
-	ProductTitle       string
-	ProductDescription string
-	BuyId              int
-	PayType            []int
-	ProductOrderNo     string
-	TradeOrderNo       string
-	PaymentToken       string
-}
-
-func PayOrder(userId, templateUserId int, productOrder order.ProductOrderDTO) (payOrderDTO PayOrderDTO, err error) {
+func PayOrder(userId, templateUserId int, productOrder order.ProductOrderDTO) (payOrderDTO order.PayOrderDTO, err error) {
 	//在redis中生成token,前端通过token获取订单状态
 	expiredTime := productOrder.ExpiredTime.Sub(time.Now().Add(BeforeOrderClose))
 	tradeOrderNo := order.GenerateTradeOrderNo()
@@ -52,12 +45,15 @@ func PayOrder(userId, templateUserId int, productOrder order.ProductOrderDTO) (p
 		return
 	}
 	payOrderDTO.TradeOrderNo = tradeOrderNo
+	payOrderDTO.AppId = htConfig.GetPaymentAppId()
+	payOrderDTO.Url = htConfig.GetPaymentApiUrl()
+	payOrderDTO.Amount = productOrder.TotalAmount
 	payOrderDTO.PaymentToken = token
 	payOrderDTO.ProductOrderNo = productOrder.OrderID
 	payOrderDTO.ProductTitle = productOrder.ProductName
 	payOrderDTO.ProductDescription = productOrder.ProductDescription
 	//微信用户的ID
-	payOrderDTO.BuyId = templateUserId
-	payOrderDTO.PayType = []int{1, 2}
+	payOrderDTO.BuyerId = templateUserId
+	payOrderDTO.PayType = "1,2"
 	return
 }