소스 검색

add order refund pay

abiao 5 년 전
부모
커밋
34e254e3f7

+ 76 - 0
go/gopath/src/fohow.com/apps/controllers/railsadmin_controller/order_refund_controller.go

@@ -0,0 +1,76 @@
+package railsadmin_controller
+
+import (
+	"fmt"
+	"fohow.com/apps"
+	"fohow.com/apps/models/balance_model"
+	"fohow.com/apps/models/order_model"
+	"fohow.com/libs/wx_mp"
+
+	"github.com/astaxie/beego"
+	"strconv"
+	"time"
+)
+
+//订单商品汇总
+func (self *RailsadminController) OrderRefund() {
+
+	_id := self.Ctx.Input.Param(":id")
+	id, _ := strconv.ParseInt(_id, 10, 64)
+
+	refund := order_model.GetRefundById(id, false)
+	if refund == nil {
+		self.ReturnError(404, apps.NoExist, "", nil)
+	}
+
+	o := order_model.GetOrderById(refund.OrderId, true)
+	if o == nil || o.Status == order_model.STATUS_UNPAY {
+		self.ReturnError(404, apps.OrderNotExist, "", nil)
+	}
+	if o.Status == order_model.STATUS_UNPAY || o.Status == order_model.STATUS_CLOSED || o.Status == order_model.STATUS_REFUNDED {
+		self.ReturnError(404, apps.OrderStatusNotSuit, "", nil)
+	}
+	o.Status = order_model.STATUS_REFUNDED
+	if !o.Save() {
+		beego.BeeLogger.Error("OrderRefund[%d] fail", id)
+	} else {
+		refund.Status = true
+		refund.RefundTime = time.Now().Unix()
+		err := refund.Save()
+		if err != nil {
+			self.ReturnError(404, apps.OrderRefundError, "", nil)
+		}
+		//退上级佣金
+		source := balance_model.CASH_SOURCE_PRODUCT_BENEFIT
+		cash := balance_model.GetCashBalanceBySourceAndRId(source, refund.OrderId)
+		if cash == nil {
+			//佣金账户增加扣款记录
+			refund_source := balance_model.CASH_SOURCE_PRODUCT_REFUND
+			remark := fmt.Sprintf("%s%s", refund.OrderId, "退款")
+			cb := balance_model.GetCashBalanceByWxUIdAndRIdAndSource(cash.WxUId, refund.OrderId, refund_source)
+			if cb == nil {
+				cb = new(balance_model.CashBalance).Create(cash.WxUId, -cash.Count, refund_source, refund.OrderId, remark)
+			}
+		}
+		//退款至代金券账户
+		if o.CouponPrice > int64(0) {
+			source := balance_model.BALANCE_SOURCE_ALL_REFUNDED_NAME
+			remark := fmt.Sprintf("取消订单退款")
+			new(balance_model.Balance).Create(o.WxUserId, o.UserId, o.CouponPrice, source, o.OrderId, remark)
+		}
+		//微信支付金额原路退款
+		if o.PaiedPrice > int64(0) {
+			outTradeNo := fmt.Sprintf("%s-%d", refund.OrderId, refund.Id)
+			outRefundNo := fmt.Sprintf("%s-%d", refund.OrderId, refund.Id)
+			remark := fmt.Sprintf("%s%s", refund.OrderId, "退款")
+			payData := wx_mp.GetRefundDataPay(outTradeNo, outRefundNo, o.TotalPrice, o.TradeNo, remark)
+			if payData != nil {
+				if payData["return_code"] == "SUCCESS" {
+					fmt.Printf("接口已全额退款")
+				}
+			}
+		}
+	}
+
+	self.ServeJSON()
+}

+ 1 - 0
go/gopath/src/fohow.com/apps/init.go

@@ -27,6 +27,7 @@ const (
 
 var (
 	// 共用
+	OrderRefundError      = []string{"orderRefundError", "退款失败"}
 	OtherSNotAllow        = []string{"othersNotAllow", "非店长无法在专区下单!"}
 	AlreadyShop           = []string{"alreadyShop", "您已经是店主,请勿重复申请"}
 	ShopNoRepit           = []string{"shopNoRepit", "请勿重复申请"}

+ 59 - 0
go/gopath/src/fohow.com/apps/models/order_model/order_reund.go

@@ -0,0 +1,59 @@
+package order_model
+
+import (
+	"fmt"
+	// "strings"
+	"time"
+
+	"fohow.com/cache"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+)
+
+const (
+	order_refunds_tablename = "order_refunds"
+)
+
+type OrderRefund struct {
+	Id            int64     `orm:"column(id);pk"                                  json:"id"` // int(11)
+	OrderId       string    `orm:"column(order_id);null"                           json:"-"` // tinyint(1)
+	WxUserId      int64     `orm:"column(wx_user_id)"                            json:"-"`   // int(11)
+	TransactionId string    `orm:"column(transaction_id);null"                    json:"-"`  // tinyint(1)
+	Total         int64     `orm:"column(total)"                                 json:"-"`   // int(11)
+	RefundFee     int64     `orm:"column(refund_fee)"                            json:"-"`   // int(11)
+	RefundTime    int64     `orm:"column(refund_time)"                            json:"-"`  // int(11)
+	Status        bool      `orm:"column(status);null"                             json:"-"` // tinyint(1)
+	Remark        string    `orm:"column(remark);null"                           json:"-"`   // tinyint(1)
+	CreatedAt     time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`  // datetime
+	UpdatedAt     time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`  // datetime
+}
+
+func (self *OrderRefund) TableName() string {
+	return order_refunds_tablename
+}
+
+//获取退款记录
+func GetRefundById(id int64, useCache bool) *OrderRefund {
+	k := fmt.Sprintf("order_model.GetRefundById.(%d)", id)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).(*OrderRefund); ok {
+			return ret
+		}
+	}
+	item := new(OrderRefund)
+	if err := orm.NewOrm().QueryTable(item).Filter("id", id).One(item); err != nil {
+		beego.BeeLogger.Error("get refund by id=[%d] err=[%s]", id, err)
+		return nil
+	}
+
+	cache.Cache.Put(k, item, 5*time.Minute)
+	return item
+}
+
+func (self *OrderRefund) Save() error {
+	if _, err := orm.NewOrm().Update(self); err != nil {
+		beego.BeeLogger.Error("SaveOrderRefund id=[%d] .err=[%s]", self.Id, err)
+		return err
+	}
+	return nil
+}

+ 1 - 0
go/gopath/src/fohow.com/routers/routes.go

@@ -212,6 +212,7 @@ func init() {
 	beego.Router("/railsadmin/create_qrcode/scene_str/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:CreateWxQrcodeWithSceneString")
 	beego.Router("/railsadmin/order/dispatch/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:OrderDispatch")
 	beego.Router("/railsadmin/order/static/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:OrderStatic")
+	beego.Router("/railsadmin/order/refund/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:OrderRefund")
 
 	//设置群主
 	beego.Router("/railsadmin/update/intro/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:UpdateIntroUser")