|
|
@@ -46,7 +46,10 @@ func (self *PayController) payExchange(oId, payWay, tradPwd, returnUrl, source s
|
|
|
if source != order_model.SOURCE_XCX && source != order_model.SOURCE_GZH {
|
|
|
self.ReturnError(403, apps.ParamsError, "", nil)
|
|
|
}
|
|
|
-
|
|
|
+ wxUser := user_model.GetWxUserById(wxUId, false)
|
|
|
+ if wxUser == nil {
|
|
|
+ self.ReturnError(403, apps.UserNeedLogin, "", nil)
|
|
|
+ }
|
|
|
//订单状态
|
|
|
order := order_model.GetOrderById(oId)
|
|
|
if order == nil || order.WxUserId != wxUId {
|
|
|
@@ -55,63 +58,59 @@ func (self *PayController) payExchange(oId, payWay, tradPwd, returnUrl, source s
|
|
|
if order.Status != order_model.STATUS_UNPAY {
|
|
|
self.ReturnError(403, apps.NotUnPay, "", nil)
|
|
|
}
|
|
|
- SaleNumsMap := make(map[int64]int64)
|
|
|
- //支付方式判断
|
|
|
- if order.OrderType == order_model.ORDER_TYPE_SEKILL && payWay == pay_model.PAYWAY_BALANCE {
|
|
|
- self.ReturnError(403, apps.NotRightPayWay, "", nil)
|
|
|
- }
|
|
|
-
|
|
|
- //获取购物商品明细
|
|
|
- buy_price_total := int64(0)
|
|
|
- total_price := int64(0)
|
|
|
- list := order_model.GetAllDetailsOrderId(order.OrderId)
|
|
|
- for _, item := range list {
|
|
|
- //商品状态
|
|
|
- product := product_model.GetProductById(item.ProductId, false)
|
|
|
- if product == nil {
|
|
|
- self.ReturnError(403, []string{apps.ProductNotExist[0], fmt.Sprintf("%s产品不存在", product.Name)}, "", nil)
|
|
|
+ //第一次支付已更新支付方式,第一次支付才计算支付金额
|
|
|
+ if len(order.PayWay) <= 0 {
|
|
|
+ SaleNumsMap := make(map[int64]int64)
|
|
|
+ //支付方式判断
|
|
|
+ if order.OrderType == order_model.ORDER_TYPE_SEKILL && payWay == pay_model.PAYWAY_BALANCE {
|
|
|
+ self.ReturnError(403, apps.NotRightPayWay, "", nil)
|
|
|
}
|
|
|
- //商品下架
|
|
|
- if product.Status == product_model.PRODUCT_STATUS_DOWN {
|
|
|
- self.ReturnError(403, []string{apps.ProductOffSale[0], fmt.Sprintf("%s产品已经下架", product.Name)}, "", nil)
|
|
|
- }
|
|
|
- //秒杀逻辑: 判断是否处于秒杀时间段内
|
|
|
- if product.SeckilShowPrice > 0 {
|
|
|
- now := time.Now()
|
|
|
- if now.Unix() < product.SeckillStart.Unix() {
|
|
|
- self.ReturnError(403, []string{apps.SeckillNotStart[0], fmt.Sprintf("%s秒杀活动尚未开始", product.Name)}, "", nil)
|
|
|
- } else if now.Unix() > product.SeckillEnd.Unix() {
|
|
|
- self.ReturnError(403, []string{apps.SeckillIsEnd[0], fmt.Sprintf("%s秒杀活动已经结束", product.Name)}, "", nil)
|
|
|
+ //获取购物商品明细
|
|
|
+ buy_price_total := int64(0)
|
|
|
+ total_price := int64(0)
|
|
|
+ list := order_model.GetAllDetailsOrderId(order.OrderId)
|
|
|
+ for _, item := range list {
|
|
|
+ //商品状态
|
|
|
+ product := product_model.GetProductById(item.ProductId, false)
|
|
|
+ if product == nil {
|
|
|
+ self.ReturnError(403, []string{apps.ProductNotExist[0], fmt.Sprintf("%s产品不存在", product.Name)}, "", nil)
|
|
|
+ }
|
|
|
+ //商品下架
|
|
|
+ if product.Status == product_model.PRODUCT_STATUS_DOWN {
|
|
|
+ self.ReturnError(403, []string{apps.ProductOffSale[0], fmt.Sprintf("%s产品已经下架", product.Name)}, "", nil)
|
|
|
}
|
|
|
+ //秒杀逻辑: 判断是否处于秒杀时间段内
|
|
|
+ if product.SeckilShowPrice > 0 {
|
|
|
+ now := time.Now()
|
|
|
+ if now.Unix() < product.SeckillStart.Unix() {
|
|
|
+ self.ReturnError(403, []string{apps.SeckillNotStart[0], fmt.Sprintf("%s秒杀活动尚未开始", product.Name)}, "", nil)
|
|
|
+ } else if now.Unix() > product.SeckillEnd.Unix() {
|
|
|
+ self.ReturnError(403, []string{apps.SeckillIsEnd[0], fmt.Sprintf("%s秒杀活动已经结束", product.Name)}, "", nil)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ buy_price_total += product.BuyPrice * item.Count
|
|
|
+ total_price += product.Price * item.Count
|
|
|
+ SaleNumsMap[product.Id] = item.Count
|
|
|
}
|
|
|
- buy_price_total += product.BuyPrice * item.Count
|
|
|
- total_price += product.Price * item.Count
|
|
|
- SaleNumsMap[product.Id] = item.Count
|
|
|
- }
|
|
|
- order.PayWay = payWay
|
|
|
- order.Contact = address.Contact
|
|
|
- order.Tel = address.Tel
|
|
|
- order.Address = fmt.Sprintf("%s%s%s%s", address.Province, address.City, address.District, address.Address)
|
|
|
- order.BuyPrice = buy_price_total
|
|
|
- order.TotalPrice = total_price
|
|
|
- //支付信息判断
|
|
|
- curWxUser := user_model.GetWxUserById(wxUId, false)
|
|
|
- if curWxUser == nil {
|
|
|
- self.ReturnError(403, apps.UserNeedLogin, "", nil)
|
|
|
- }
|
|
|
- //黑名单用户
|
|
|
- curUser := user_model.GetUserById(curWxUser.UserId, false)
|
|
|
- if curUser != nil && curUser.IsBlackUser == 1 {
|
|
|
- self.ReturnError(403, apps.NetworkBusy, "", nil)
|
|
|
- }
|
|
|
- //再次增加订单状态判断
|
|
|
- if order.Status != order_model.STATUS_UNPAY {
|
|
|
- self.ReturnError(403, apps.NotUnPay, "", nil)
|
|
|
- }
|
|
|
- //若账户有代金券则优先抵扣代金券
|
|
|
- needWx := true
|
|
|
- //订单第一次支付
|
|
|
- if len(order.PayWay) == 0 {
|
|
|
+
|
|
|
+ order.PayWay = payWay
|
|
|
+ order.Contact = address.Contact
|
|
|
+ order.Tel = address.Tel
|
|
|
+ order.Address = fmt.Sprintf("%s%s%s%s", address.Province, address.City, address.District, address.Address)
|
|
|
+ order.BuyPrice = buy_price_total
|
|
|
+ order.TotalPrice = total_price
|
|
|
+ //支付信息判断
|
|
|
+ //黑名单用户
|
|
|
+ curUser := user_model.GetUserById(wxUser.UserId, false)
|
|
|
+ if curUser != nil && curUser.IsBlackUser == 1 {
|
|
|
+ self.ReturnError(403, apps.NetworkBusy, "", nil)
|
|
|
+ }
|
|
|
+ //再次增加订单状态判断
|
|
|
+ if order.Status != order_model.STATUS_UNPAY {
|
|
|
+ self.ReturnError(403, apps.NotUnPay, "", nil)
|
|
|
+ }
|
|
|
+ //若账户有代金券则优先抵扣代金券
|
|
|
+ needWx := true
|
|
|
totalCoupon := int64(0)
|
|
|
userLeftBalanceCount := balance_model.GetUserTotalBalance(wxUId)
|
|
|
tp := order.TotalPrice
|
|
|
@@ -135,61 +134,91 @@ func (self *PayController) payExchange(oId, payWay, tradPwd, returnUrl, source s
|
|
|
new(balance_model.Balance).Create(wxUId, uId, -totalCoupon, source, oId, remark)
|
|
|
}
|
|
|
order.CouponPrice = totalCoupon
|
|
|
- } else {
|
|
|
+ switch needWx { // 1.true需要微信支付,2.false不需要微信支付
|
|
|
+ case false: // 代金券抵扣完毕
|
|
|
+ //更新订单状态
|
|
|
+ order.Status = order_model.STATUS_PROCESSING
|
|
|
+ order.PaiedAt = time.Now().Unix()
|
|
|
+ order.PaiedTime = time.Now()
|
|
|
+ order.PaiedPrice = 0
|
|
|
+ order.PayWay = pay_model.PAYWAY_BALANCE
|
|
|
+ order.Source = source
|
|
|
+ order.Save()
|
|
|
+ //发放赠品
|
|
|
+ productId := int64(0)
|
|
|
+ if beego.AppConfig.String("RunMode") == "dev" {
|
|
|
+ productId = int64(91)
|
|
|
+ } else {
|
|
|
+ productId = order_model.SEND_PRODUCT_ID
|
|
|
+ }
|
|
|
+ saleList := order_model.GetWxUserOrders(wxUser.Id)
|
|
|
+ //beego.BeeLogger.Error("en(saleList)=%d", len(saleList))
|
|
|
+ // 七月注册会员首单赠送赠品
|
|
|
+ if order.TotalPrice >= order_model.PROMOTION_TOTAL && len(saleList) == 1 {
|
|
|
+ product := product_model.GetProductById(productId, false)
|
|
|
+ go order_model.SendCreate(order.OrderId, order.Id, productId, product.Price, product.Price, product.Name, int64(1))
|
|
|
+ }
|
|
|
+ //已支付订单移除未支付队列
|
|
|
+ cancelKey := helpers.GetOrderCancelList()
|
|
|
+ helpers.ThrowOutRedisList(cancelKey, order.OrderId)
|
|
|
+ //更新已售数量
|
|
|
+ go order_model.UpdateSaleNums(SaleNumsMap)
|
|
|
+ //go CreateOrderNotify(order, product)
|
|
|
+ //wxUser := user_model.GetWxUserById(order.WxUserId, true)
|
|
|
+ //go sendInviterBenefit(wxUser, order.OrderId, user_model.SOURCE_PRODUCT_BENEFIT)
|
|
|
+ payUrl, payData = fmt.Sprintf("%s?order_id=%s", returnUrl, order.OrderId), nil
|
|
|
+ result := PayUrl{NeedWx: false, PayUrl: payUrl, PayData: payData, OrderId: order.OrderId}
|
|
|
+ //self.Data["json"] = self.FormatResult([]interface{}{result})
|
|
|
+ self.Data["json"] = result
|
|
|
+ case true: // 微信支付
|
|
|
+ order.Contact = address.Contact
|
|
|
+ order.PaiedPrice = tp - totalCoupon
|
|
|
+ order.Tel = address.Tel
|
|
|
+ order.Address = fmt.Sprintf("%s%s%s%s", address.Province, address.City, address.District, address.Address)
|
|
|
+ order.Source = source
|
|
|
+ order.Save()
|
|
|
+ if order.Source == order_model.SOURCE_XCX { //小程序微信支付
|
|
|
+ freight := order_model.FREIGHT
|
|
|
+ if order.TotalPrice >= order_model.FREIGHT_LIMIT || beego.AppConfig.String("RunMode") == "dev" {
|
|
|
+ freight = int64(0)
|
|
|
+ }
|
|
|
+ order.TotalPrice += freight
|
|
|
+ notifyUrl := fmt.Sprintf("%s/v1/pay/%s/async/%s", beego.AppConfig.String("ApiHost"), EXCHANGE_TARGET, pay_model.PAYWAY_WEIXINPAY)
|
|
|
+ body := fmt.Sprintf("FOHOW玖玖-购买商品")
|
|
|
+ payData := wx_mp.GetPayData(wxUser.Openid, order.OrderId, order.PaiedPrice, body, notifyUrl, self.Ctx.Input.IP())
|
|
|
|
|
|
- }
|
|
|
+ //返回数据
|
|
|
+ type PayData struct {
|
|
|
+ NeedWx bool `json:"need_wx"`
|
|
|
+ PayData map[string]string `json:"pay_data"`
|
|
|
+ }
|
|
|
+ self.Data["json"] = &PayData{NeedWx: true, PayData: payData}
|
|
|
|
|
|
- switch needWx { // 1.true需要微信支付,2.false不需要微信支付
|
|
|
- case false: // 代金券抵扣完毕
|
|
|
- //更新订单状态
|
|
|
- order.Status = order_model.STATUS_PROCESSING
|
|
|
- order.PaiedAt = time.Now().Unix()
|
|
|
- order.PaiedTime = time.Now()
|
|
|
- order.PaiedPrice = 0
|
|
|
- order.PayWay = pay_model.PAYWAY_BALANCE
|
|
|
- order.Source = source
|
|
|
- order.Save()
|
|
|
- //发放赠品
|
|
|
- productId := int64(0)
|
|
|
- if beego.AppConfig.String("RunMode") == "dev" {
|
|
|
- productId = int64(91)
|
|
|
- } else {
|
|
|
- productId = order_model.SEND_PRODUCT_ID
|
|
|
- }
|
|
|
- saleList := order_model.GetWxUserOrders(curWxUser.Id)
|
|
|
- //beego.BeeLogger.Error("en(saleList)=%d", len(saleList))
|
|
|
- // 七月注册会员首单赠送赠品
|
|
|
- if order.TotalPrice >= order_model.PROMOTION_TOTAL && len(saleList) == 1 {
|
|
|
- product := product_model.GetProductById(productId, false)
|
|
|
- go order_model.SendCreate(order.OrderId, order.Id, productId, product.Price, product.Price, product.Name, int64(1))
|
|
|
+ } else { //公众号微信支付
|
|
|
+
|
|
|
+ notifyUrl := fmt.Sprintf("%s/v1/pay/%s/async/%s", beego.AppConfig.String("ApiHost"), EXCHANGE_TARGET, pay_model.PAYWAY_WEIXINPAY)
|
|
|
+ body := fmt.Sprintf("FOHOW玖玖-购买商品")
|
|
|
+ wxGzh := user_model.GetWxUserGzhByWxUIdAndAppId(wxUser.Id, beego.AppConfig.String("WxMPAppId"), false)
|
|
|
+ if wxGzh == nil {
|
|
|
+ self.ReturnError(403, apps.NoExist, "", nil)
|
|
|
+ }
|
|
|
+ //payData := wx_mp.GetPayData(wxGzh.GzhOpenId, order.OrderId , order.TotalPrice, body,notifyUrl, self.Ctx.Input.IP())
|
|
|
+ payData := wx_mp.GetGzhPayData(wxGzh.GzhOpenId, order.OrderId, order.PaiedPrice, body, notifyUrl, self.Ctx.Input.IP())
|
|
|
+
|
|
|
+ //返回数据
|
|
|
+ type PayData struct {
|
|
|
+ NeedWx bool `json:"need_wx"`
|
|
|
+ PayData map[string]string `json:"pay_data"`
|
|
|
+ }
|
|
|
+ self.Data["json"] = &PayData{NeedWx: true, PayData: payData}
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ beego.BeeLogger.Error("pay way not match, payway:%s", payWay)
|
|
|
}
|
|
|
- //已支付订单移除未支付队列
|
|
|
- cancelKey := helpers.GetOrderCancelList()
|
|
|
- helpers.ThrowOutRedisList(cancelKey, order.OrderId)
|
|
|
- //更新已售数量
|
|
|
- go order_model.UpdateSaleNums(SaleNumsMap)
|
|
|
- //go CreateOrderNotify(order, product)
|
|
|
- //wxUser := user_model.GetWxUserById(order.WxUserId, true)
|
|
|
- //go sendInviterBenefit(wxUser, order.OrderId, user_model.SOURCE_PRODUCT_BENEFIT)
|
|
|
- payUrl, payData = fmt.Sprintf("%s?order_id=%s", returnUrl, order.OrderId), nil
|
|
|
- result := PayUrl{NeedWx: false, PayUrl: payUrl, PayData: payData, OrderId: order.OrderId}
|
|
|
- //self.Data["json"] = self.FormatResult([]interface{}{result})
|
|
|
- self.Data["json"] = result
|
|
|
- case true: // 微信支付
|
|
|
-
|
|
|
- wxUser := self.GetCurrentWxUser(false)
|
|
|
- order.Contact = address.Contact
|
|
|
- order.PaiedPrice = tp - totalCoupon
|
|
|
- order.Tel = address.Tel
|
|
|
- order.Address = fmt.Sprintf("%s%s%s%s", address.Province, address.City, address.District, address.Address)
|
|
|
- order.Source = source
|
|
|
- order.Save()
|
|
|
+ } else {
|
|
|
+ //如果是第二次支付
|
|
|
if order.Source == order_model.SOURCE_XCX { //小程序微信支付
|
|
|
- freight := order_model.FREIGHT
|
|
|
- if order.TotalPrice >= order_model.FREIGHT_LIMIT || beego.AppConfig.String("RunMode") == "dev" {
|
|
|
- freight = int64(0)
|
|
|
- }
|
|
|
- order.TotalPrice += freight
|
|
|
+
|
|
|
notifyUrl := fmt.Sprintf("%s/v1/pay/%s/async/%s", beego.AppConfig.String("ApiHost"), EXCHANGE_TARGET, pay_model.PAYWAY_WEIXINPAY)
|
|
|
body := fmt.Sprintf("FOHOW玖玖-购买商品")
|
|
|
payData := wx_mp.GetPayData(wxUser.Openid, order.OrderId, order.PaiedPrice, body, notifyUrl, self.Ctx.Input.IP())
|
|
|
@@ -219,8 +248,6 @@ func (self *PayController) payExchange(oId, payWay, tradPwd, returnUrl, source s
|
|
|
}
|
|
|
self.Data["json"] = &PayData{NeedWx: true, PayData: payData}
|
|
|
}
|
|
|
- default:
|
|
|
- beego.BeeLogger.Error("pay way not match, payway:%s", payWay)
|
|
|
}
|
|
|
self.ServeJSON()
|
|
|
}
|