trade_order.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package order
  2. import (
  3. "encoding/json"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/models"
  6. "eta/eta_mini_ht_api/models/message"
  7. "eta/eta_mini_ht_api/models/order"
  8. "fmt"
  9. "math/rand"
  10. "strconv"
  11. "time"
  12. )
  13. const (
  14. RMB = "CNY"
  15. AliPayWay PaymentWay = "alipay"
  16. WechatPay PaymentWay = "wechat"
  17. RefundSuccess = "success"
  18. RefundFail = "fail"
  19. PaySuccess = "success"
  20. PayFail = "fail"
  21. SuccessIcon = `<svg-icon name="success" size="16px"></svg-icon>`
  22. FailIcon = `<svg-icon name="success" size="16px"></svg-icon>`
  23. )
  24. type PaymentWay string
  25. type TradeOrderDTO struct {
  26. ID int `gorm:"column:id;primaryKey"`
  27. TransactionID string `gorm:"column:transaction_id;type:varchar(255);comment:第三方平台ID"`
  28. ProductOrderID string `gorm:"column:product_order_id;type:varchar(255);comment:商品订单号"`
  29. PaymentAccount string `gorm:"column:payment_account;type:varchar(255);comment:支付账号"`
  30. PaymentWay string `gorm:"column:payment_way;type:enum('wechat');comment:支付渠道"`
  31. Amount string `gorm:"column:amount;type:varchar(20);comment:支付金额"`
  32. Currency string `gorm:"column:currency;type:varchar(255);comment:货币"`
  33. MerchantID string `gorm:"column:merchant_id;type:int(11);comment:商户id"`
  34. UserID int `gorm:"column:user_id;type:int(11);comment:用户id"`
  35. TemplateUserID int `gorm:"column:template_user_id;type:int(11);comment:临时用户id"`
  36. PaymentType string `gorm:"column:payment_type;type:enum('pay','refund');comment:订单类型"`
  37. PaymentStatus string `gorm:"column:payment_status;type:enum('pending','processing','done','failed');comment:支付状态"`
  38. DealTime time.Time `gorm:"column:deal_time;type:datetime;comment:完成时间"`
  39. }
  40. func GenerateTradeOrderNo() string {
  41. timestamp := time.Now().UnixNano() / 1000000 // 毫秒级时间戳
  42. // 生成随机数
  43. rand.New(rand.NewSource(time.Now().UnixNano()))
  44. randomPart := rand.Intn(999999)
  45. // 格式化订单号
  46. orderNumber := fmt.Sprintf("T%d%06d", timestamp, randomPart)
  47. return orderNumber
  48. }
  49. func CreateTradeOrder(userId, templateUserId int, productOrderNo, tradeOrderNo, merchantNo string) (err error) {
  50. db := models.Main()
  51. productOrder, err := order.GetOrderByUser(templateUserId, productOrderNo)
  52. if err != nil {
  53. logger.Error("获取商品订单信息失败%v", err)
  54. return
  55. }
  56. tx := db.Begin()
  57. defer func() {
  58. if err != nil {
  59. tx.Rollback()
  60. return
  61. }
  62. tx.Commit()
  63. }()
  64. tradeOrder := order.TradeOrder{
  65. ProductOrderID: productOrderNo,
  66. ProductName: productOrder.ProductName,
  67. TransactionID: tradeOrderNo,
  68. Amount: productOrder.TotalAmount,
  69. Mobile: productOrder.Mobile,
  70. AreaCode: productOrder.AreaCode,
  71. RealName: productOrder.RealName,
  72. PaymentWay: order.WechatPayWay,
  73. Currency: RMB,
  74. MerchantID: merchantNo,
  75. UserID: userId,
  76. TemplateUserID: templateUserId,
  77. PaymentType: order.PaymentTypePay,
  78. }
  79. err = tx.Create(&tradeOrder).Error
  80. if err != nil {
  81. logger.Error("创建支付订单失败%v", err)
  82. return
  83. }
  84. err = tx.Model(&order.ProductOrder{}).Where("id = ?", productOrder.ID).Updates(map[string]interface{}{
  85. "trade_id": tradeOrder.ID,
  86. "trade_no": tradeOrder.TransactionID,
  87. "payment_time": time.Now(),
  88. "payment_amount": tradeOrder.Amount,
  89. "payment_way": tradeOrder.PaymentWay,
  90. }).Error
  91. return
  92. }
  93. func convertToDTO(tradeOrder order.TradeOrder) TradeOrderDTO {
  94. return TradeOrderDTO{
  95. ID: tradeOrder.ID,
  96. TransactionID: tradeOrder.TransactionID,
  97. ProductOrderID: tradeOrder.ProductOrderID,
  98. PaymentAccount: tradeOrder.PaymentAccount,
  99. PaymentWay: string(tradeOrder.PaymentWay),
  100. Amount: tradeOrder.Amount,
  101. Currency: tradeOrder.Currency,
  102. MerchantID: tradeOrder.MerchantID,
  103. UserID: tradeOrder.UserID,
  104. TemplateUserID: tradeOrder.UserID,
  105. PaymentType: string(tradeOrder.PaymentType),
  106. PaymentStatus: string(tradeOrder.PaymentStatus),
  107. DealTime: tradeOrder.DealTime,
  108. }
  109. }
  110. func GetUnFailedTradeFlowByProductOrder(productOrderNo string) (dtoList []TradeOrderDTO, err error) {
  111. var tradeOrderList []order.TradeOrder
  112. tradeOrderList, err = order.GetUnFailedTradeFlowByProductOrder(productOrderNo)
  113. for _, tradeOrder := range tradeOrderList {
  114. dtoList = append(dtoList, convertToDTO(tradeOrder))
  115. }
  116. return
  117. }
  118. func GetTradeOrderByNo(tradeOrderNo string) (dto TradeOrderDTO, err error) {
  119. var tradeOrder order.TradeOrder
  120. tradeOrder, err = order.GetTradeOrderByNo(tradeOrderNo, order.PaymentTypePay)
  121. dto = convertToDTO(tradeOrder)
  122. return
  123. }
  124. type RefundDealFlowDTO struct {
  125. OperatorUserID int `gorm:"column:operator_user_id"`
  126. ProductOrderNo string `gorm:"column:product_order_no"`
  127. RefundOrderNo string `gorm:"column:refund_order_no"`
  128. }
  129. func convertRefundDTO(flow order.RefundDealFlow) RefundDealFlowDTO {
  130. return RefundDealFlowDTO{
  131. OperatorUserID: flow.OperatorUserID,
  132. ProductOrderNo: flow.ProductOrderNo,
  133. RefundOrderNo: flow.RefundOrderNo,
  134. }
  135. }
  136. func createRefundMetaInfo(sysUserId int, productOrderNo string, flag string) (err error) {
  137. productOrder, err := order.GetOrderByOrderNo(productOrderNo)
  138. if err != nil {
  139. logger.Error("生成退款消息通知失败,获取订单信息失败:v,订单编号:%s", err, productOrderNo)
  140. return
  141. }
  142. var result string
  143. var icon string
  144. switch flag {
  145. case RefundSuccess:
  146. result = "成功"
  147. icon = SuccessIcon
  148. case RefundFail:
  149. result = "失败"
  150. icon = FailIcon
  151. default:
  152. logger.Error("生成退款消息通知失败,未知的退款状态%s,订单编号:%s", flag, productOrderNo)
  153. return
  154. }
  155. refundInfo := message.RefundMetaData{
  156. Icon: icon,
  157. RealName: productOrder.RealName,
  158. ProductOrderNo: productOrderNo,
  159. Result: result,
  160. }
  161. bytes, err := json.Marshal(refundInfo)
  162. if err != nil {
  163. logger.Error("生成退款消息通知失败,序列化退款信息失败:%v,订单编号:%s", err, productOrderNo)
  164. return
  165. }
  166. metaInfo := message.MetaInfo{
  167. Meta: string(bytes),
  168. MetaType: message.SysUserNoticeType,
  169. SourceType: message.RefundSourceType,
  170. Status: message.InitStatusType,
  171. From: "HTTradePlate",
  172. To: strconv.Itoa(sysUserId),
  173. }
  174. return message.CreateMetaInfo(metaInfo)
  175. }
  176. func DealRefund(refundOrderNo string, flag string) (dto RefundDealFlowDTO, err error) {
  177. flow, err := order.GetRefundFlow(refundOrderNo)
  178. if err != nil {
  179. logger.Error("获取退款流水失败%v,退款订单:%s", err, refundOrderNo)
  180. return
  181. }
  182. dto = convertRefundDTO(flow)
  183. //处理退款订单
  184. TradeOrder, err := order.GetTradeOrderByNo(flow.RefundOrderNo, order.PaymentTypeRefund)
  185. if err != nil {
  186. logger.Error("获取退款订单失败%v,退款订单:%s", err, refundOrderNo)
  187. return
  188. }
  189. isSuccess := false
  190. if flag == RefundSuccess {
  191. isSuccess = true
  192. }
  193. err = order.DealRefundOrder(TradeOrder, isSuccess)
  194. if err != nil {
  195. logger.Error("处理退款结果失败%v,退款订单:%s", err, refundOrderNo)
  196. return
  197. }
  198. _ = createRefundMetaInfo(flow.OperatorUserID, flow.ProductOrderNo, flag)
  199. return
  200. }
  201. func DealPayment(tradeOrderNo string, flag string) (productOrderNo string, err error) {
  202. //处理退款订单
  203. tradeOrder, err := order.GetTradeOrderByNo(tradeOrderNo, order.PaymentTypePay)
  204. if err != nil {
  205. logger.Error("获取支付订单失败%v,支付订单:%s", err, tradeOrderNo)
  206. return
  207. }
  208. isSuccess := false
  209. if flag == PaySuccess {
  210. isSuccess = true
  211. }
  212. err = order.DealPaymentOrder(tradeOrder, isSuccess)
  213. if err != nil {
  214. logger.Error("处理支付结果失败%v,支付订单:%s", err, tradeOrderNo)
  215. return
  216. }
  217. productOrderNo = tradeOrder.ProductOrderID
  218. return
  219. }