trade_order.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package order
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "gorm.io/gorm"
  5. "time"
  6. )
  7. type PaymentStatus string
  8. type PaymentType string
  9. type PaymentWay string
  10. const (
  11. PaymentStatusPending PaymentStatus = "pending"
  12. PaymentStatusProcessing PaymentStatus = "processing"
  13. PaymentStatusDone PaymentStatus = "done"
  14. PaymentStatusFailed PaymentStatus = "failed"
  15. PaymentTypePay PaymentType = "pay"
  16. PaymentTypeRefund PaymentType = "refund"
  17. WechatPayWay PaymentWay = "wechat"
  18. AliPayWay PaymentWay = "alipay"
  19. )
  20. type TradeOrder struct {
  21. ID int `gorm:"column:id;primaryKey"`
  22. TransactionID string `gorm:"column:transaction_id;type:varchar(255);comment:第三方平台ID"`
  23. OrgTransactionID string `gorm:"column:org_transaction_id;type:varchar(255);comment:第三方平台ID"`
  24. ProductOrderID string `gorm:"column:product_order_id;type:varchar(255);comment:商品订单号"`
  25. PaymentAccount string `gorm:"column:payment_account;type:varchar(255);comment:支付账号"`
  26. PaymentWay PaymentWay `gorm:"column:payment_way;type:enum('wechat');comment:支付渠道"`
  27. Amount string `gorm:"column:amount;type:varchar(20);comment:支付金额"`
  28. Currency string `gorm:"column:currency;type:varchar(255);comment:货币"`
  29. MerchantID string `gorm:"column:merchant_id;type:varchar(255);comment:商户id"`
  30. UserID int `gorm:"column:user_id;type:int(11);comment:用户id"`
  31. TemplateUserID int `gorm:"column:template_user_id;type:int(11);comment:临时用户id"`
  32. PaymentType PaymentType `gorm:"column:payment_type;type:enum('pay','refund');comment:订单类型"`
  33. PaymentStatus PaymentStatus `gorm:"column:payment_status;type:enum('pending','processing','done','failed');comment:支付状态"`
  34. DealTime time.Time `gorm:"column:deal_time;type:datetime;comment:完成时间"`
  35. CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"`
  36. UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;comment:更新时间"`
  37. }
  38. func (to *TradeOrder) TableName() string {
  39. return "trade_orders"
  40. }
  41. func (to *TradeOrder) BeforeCreate(_ *gorm.DB) (err error) {
  42. to.CreatedTime = time.Now()
  43. to.PaymentStatus = PaymentStatusPending
  44. return
  45. }
  46. func GetUnFailedTradeFlowByProductOrder(no string) (list []TradeOrder, err error) {
  47. db := models.Main()
  48. err = db.Where("product_order_id=? and payment_status!=? order by updated_time,created_time desc", no, PaymentStatusFailed).Find(&list).Error
  49. return
  50. }
  51. func GetTradeOrderByNo(tradeOrderNo string, paymentType PaymentType) (order TradeOrder, err error) {
  52. db := models.Main()
  53. err = db.Where("transaction_id=? and payment_type =?", tradeOrderNo, paymentType).First(&order).Error
  54. return
  55. }
  56. func DealRefundOrder(order TradeOrder, isSuccess bool) (err error) {
  57. var paymentStatus PaymentStatus
  58. var refundStatus RefundStatus
  59. if isSuccess {
  60. paymentStatus = PaymentStatusDone
  61. refundStatus = RefundStatusSuccess
  62. } else {
  63. paymentStatus = PaymentStatusFailed
  64. refundStatus = RefundStatusFailed
  65. }
  66. db := models.Main()
  67. tx := db.Begin()
  68. defer func() {
  69. if err != nil {
  70. tx.Rollback()
  71. return
  72. }
  73. tx.Commit()
  74. }()
  75. err = tx.Model(&TradeOrder{}).Where("id=?", order.ID).Updates(map[string]interface{}{
  76. "payment_status": paymentStatus,
  77. "deal_time": time.Now(),
  78. }).Error
  79. if err != nil {
  80. return
  81. }
  82. err = tx.Model(&ProductOrder{}).Where("order_id=?", order.ProductOrderID).Updates(map[string]interface{}{
  83. "refund_status": refundStatus,
  84. "refund_finish_time": time.Now(),
  85. }).Error
  86. return
  87. }