123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package order
- import (
- "eta/eta_mini_ht_api/models"
- "gorm.io/gorm"
- "time"
- )
- type PaymentStatus string
- type PaymentType string
- type PaymentWay string
- const (
- PaymentStatusPending PaymentStatus = "pending"
- PaymentStatusProcessing PaymentStatus = "processing"
- PaymentStatusDone PaymentStatus = "done"
- PaymentStatusFailed PaymentStatus = "failed"
- PaymentTypePay PaymentType = "pay"
- PaymentTypeRefund PaymentType = "refund"
- WechatPayWay PaymentWay = "wechat"
- AliPayWay PaymentWay = "alipay"
- )
- type TradeOrder struct {
- ID int `gorm:"column:id;primaryKey"`
- TransactionID string `gorm:"column:transaction_id;type:varchar(255);comment:第三方平台ID"`
- OrgTransactionID string `gorm:"column:org_transaction_id;type:varchar(255);comment:第三方平台ID"`
- ProductOrderID string `gorm:"column:product_order_id;type:varchar(255);comment:商品订单号"`
- ProductName string `gorm:"column:product_name;type:varchar(255);comment:商品名称"`
- RealName string `gorm:"column:real_name;default:null;comment:'姓名'" json:"real_name"`
- AreaCode string `gorm:"column:area_code;default:null;comment:'手机区号'" json:"area_code"`
- Mobile string `gorm:"column:mobile;default:null;comment:'手机号'" json:"mobile"`
- PaymentAccount string `gorm:"column:payment_account;type:varchar(255);comment:支付账号"`
- PaymentWay PaymentWay `gorm:"column:payment_way;type:enum('wechat');comment:支付渠道"`
- Amount string `gorm:"column:amount;type:varchar(20);comment:支付金额"`
- Currency string `gorm:"column:currency;type:varchar(255);comment:货币"`
- MerchantID string `gorm:"column:merchant_id;type:varchar(255);comment:商户id"`
- UserID int `gorm:"column:user_id;type:int(11);comment:用户id"`
- TemplateUserID int `gorm:"column:template_user_id;type:int(11);comment:临时用户id"`
- PaymentType PaymentType `gorm:"column:payment_type;type:enum('pay','refund');comment:订单类型"`
- PaymentStatus PaymentStatus `gorm:"column:payment_status;type:enum('pending','processing','done','failed');comment:支付状态"`
- DealTime time.Time `gorm:"column:deal_time;type:datetime;comment:完成时间"`
- CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"`
- UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;comment:更新时间"`
- }
- func (to *TradeOrder) TableName() string {
- return "trade_orders"
- }
- func (to *TradeOrder) BeforeCreate(_ *gorm.DB) (err error) {
- to.CreatedTime = time.Now()
- to.PaymentStatus = PaymentStatusPending
- return
- }
- func GetUnFailedTradeFlowByProductOrder(no string) (list []TradeOrder, err error) {
- db := models.Main()
- err = db.Where("product_order_id=? and payment_status!=? order by updated_time,created_time desc", no, PaymentStatusFailed).Find(&list).Error
- return
- }
- func GetTradeOrderByNo(tradeOrderNo string, paymentType PaymentType) (order TradeOrder, err error) {
- db := models.Main()
- err = db.Where("transaction_id=? and payment_type =?", tradeOrderNo, paymentType).First(&order).Error
- return
- }
- func DealRefundOrder(order TradeOrder, isSuccess bool) (err error) {
- var paymentStatus PaymentStatus
- var refundStatus RefundStatus
- if isSuccess {
- paymentStatus = PaymentStatusDone
- refundStatus = RefundStatusSuccess
- } else {
- paymentStatus = PaymentStatusFailed
- refundStatus = RefundStatusFailed
- }
- db := models.Main()
- tx := db.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- return
- }
- tx.Commit()
- }()
- err = tx.Model(&TradeOrder{}).Where("id=?", order.ID).Updates(map[string]interface{}{
- "payment_status": paymentStatus,
- "deal_time": time.Now(),
- }).Error
- if err != nil {
- return
- }
- err = tx.Model(&ProductOrder{}).Where("order_id=?", order.ProductOrderID).Updates(map[string]interface{}{
- "refund_status": refundStatus,
- "refund_finish_time": time.Now(),
- }).Error
- return
- }
- func DealPaymentOrder(order TradeOrder, isSuccess bool, validDuration string) (err error) {
- var paymentStatus PaymentStatus
- if isSuccess {
- paymentStatus = PaymentStatusDone
- } else {
- paymentStatus = PaymentStatusFailed
- }
- db := models.Main()
- tx := db.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- return
- }
- tx.Commit()
- }()
- err = tx.Model(&TradeOrder{}).Where("id=?", order.ID).Updates(map[string]interface{}{
- "payment_status": paymentStatus,
- "deal_time": time.Now(),
- }).Error
- if err != nil {
- return
- }
- if isSuccess {
- err = tx.Model(&ProductOrder{}).Where("order_id=?", order.ProductOrderID).Updates(map[string]interface{}{
- "status": OrderStatusPaid,
- "valid_duration": validDuration,
- }).Error
- }
- return
- }
|