|
@@ -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)
|