|
|
@@ -0,0 +1,315 @@
|
|
|
+package pay_controller
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ // "net/url"
|
|
|
+ // "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/astaxie/beego"
|
|
|
+ // "github.com/astaxie/beego/context"
|
|
|
+
|
|
|
+ "fohow.com/apps"
|
|
|
+ "fohow.com/apps/helpers"
|
|
|
+ "fohow.com/apps/models/address_model"
|
|
|
+ "fohow.com/apps/models/balance_model"
|
|
|
+ "fohow.com/apps/models/merchant_model"
|
|
|
+ "fohow.com/apps/models/order_model"
|
|
|
+ "fohow.com/apps/models/pay_model"
|
|
|
+ "fohow.com/apps/models/product_model"
|
|
|
+ "fohow.com/apps/models/user_model"
|
|
|
+ "fohow.com/libs/tool"
|
|
|
+ "fohow.com/libs/wx_mp"
|
|
|
+ "strings"
|
|
|
+ // "fohow.com/apps/models/wx_gongzhonghao_model"
|
|
|
+ "fohow.com/apps/models/exchange_model"
|
|
|
+ "fohow.com/apps/models/subject_model"
|
|
|
+ "sync"
|
|
|
+)
|
|
|
+
|
|
|
+var createDrawCode sync.Mutex
|
|
|
+var payOrder sync.Mutex
|
|
|
+
|
|
|
+//支付订单
|
|
|
+func (self *PayController) payExchange(oId, payWay, tradPwd, returnUrl, source string) {
|
|
|
+ payOrder.Lock()
|
|
|
+ defer payOrder.Unlock()
|
|
|
+ var payUrl string
|
|
|
+ var payData map[string]string
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+ uId := self.GetCurrentUserId()
|
|
|
+
|
|
|
+ //地址
|
|
|
+ addressId, _ := self.GetInt64("address_id")
|
|
|
+ address := address_model.GetUserAddressById(addressId)
|
|
|
+ if address == nil || address.WxUserId != wxUId {
|
|
|
+ self.ReturnError(403, apps.AddressNotMatch, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ if source != order_model.SOURCE_XCX && source != order_model.SOURCE_GZH {
|
|
|
+ self.ReturnError(403, apps.ParamsError, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ //订单状态
|
|
|
+ order := order_model.GetOrderById(oId)
|
|
|
+ if order == nil || order.WxUserId != wxUId {
|
|
|
+ self.ReturnError(404, apps.OrderNotExist, "", nil)
|
|
|
+ }
|
|
|
+ if order.Status != order_model.STATUS_UNPAY {
|
|
|
+ self.ReturnError(403, apps.NotUnPay, "", nil)
|
|
|
+ }
|
|
|
+ SaleNumsMap := make(map[int64]int64)
|
|
|
+
|
|
|
+ //获取购物商品明细
|
|
|
+ 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
|
|
|
+ if payWay == order_model.PAY_WAY_WEIXIN { //微信支付总价
|
|
|
+ total_price += product.Price * item.Count
|
|
|
+ } else if payWay == order_model.PAY_WAY_BALANCE { //代金券支付总价
|
|
|
+ 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
|
|
|
+
|
|
|
+ switch payWay { // 1.代金券购买 2.微信支付
|
|
|
+ case pay_model.PAYWAY_BALANCE: // 代金券购买
|
|
|
+
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ userLeftBalanceCount := balance_model.GetUserTotalBalance(wxUId)
|
|
|
+ tp := order.TotalPrice
|
|
|
+ freight := order_model.FREIGHT
|
|
|
+ if tp >= order_model.FREIGHT_LIMIT || beego.AppConfig.String("RunMode") == "dev" {
|
|
|
+ freight = int64(0)
|
|
|
+ }
|
|
|
+ tp += freight
|
|
|
+ if userLeftBalanceCount < tp {
|
|
|
+ self.ReturnError(403, apps.BalanceNotEnough, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ if tp > 0 {
|
|
|
+ source := balance_model.BALANCE_SOURCE_EXCHANGE_PRODUCT
|
|
|
+ remark := fmt.Sprintf("代金券兑换商品")
|
|
|
+ new(balance_model.Balance).Create(wxUId, uId, -tp, source, oId, remark)
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新订单状态
|
|
|
+ order.Status = order_model.STATUS_PROCESSING
|
|
|
+ order.PaiedAt = time.Now().Unix()
|
|
|
+ order.PaiedTime = time.Now()
|
|
|
+ order.PaiedPrice = order.TotalPrice
|
|
|
+ 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 {
|
|
|
+ product := product_model.GetProductById(productId, false)
|
|
|
+ go order_model.SendCreate(order.OrderId, order.Id, productId, product.Price, product.Price, product.Name, int64(1))
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新已售数量
|
|
|
+ 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{PayUrl: payUrl, PayData: payData, OrderId: order.OrderId}
|
|
|
+ self.Data["json"] = self.FormatResult([]interface{}{result})
|
|
|
+ case pay_model.PAYWAY_WEIXINPAY: // 微信支付
|
|
|
+
|
|
|
+ wxUser := self.GetCurrentWxUser(false)
|
|
|
+
|
|
|
+ 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.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.TotalPrice, body, notifyUrl, self.Ctx.Input.IP())
|
|
|
+
|
|
|
+ //返回数据
|
|
|
+ type PayData struct {
|
|
|
+ PayData map[string]string `json:"pay_data"`
|
|
|
+ }
|
|
|
+ self.Data["json"] = &PayData{PayData: payData}
|
|
|
+
|
|
|
+ } 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.TotalPrice, body, notifyUrl, self.Ctx.Input.IP())
|
|
|
+
|
|
|
+ //返回数据
|
|
|
+ type PayData struct {
|
|
|
+ PayData map[string]string `json:"pay_data"`
|
|
|
+ }
|
|
|
+ self.Data["json"] = &PayData{PayData: payData}
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ beego.BeeLogger.Error("pay way not match, payway:%s", payWay)
|
|
|
+ }
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+//用户支付成功,后给卖家所有管理员发下单通知
|
|
|
+func CreateOrderNotify(order *order_model.Order, product *product_model.Product) {
|
|
|
+ merchantUserList := merchant_model.GetMerchantUserRelationListByMerchantId(product.MerchantId, true)
|
|
|
+ for _, item := range merchantUserList {
|
|
|
+ if item == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ isManageTheProduct, _ := tool.Contain(fmt.Sprintf("%d", product.Id), strings.Split(item.ManageProductIds, ","))
|
|
|
+ if item.ManageProductIds == "0" || isManageTheProduct {
|
|
|
+ sellerWxUser := user_model.GetWxUserByUserId(item.UserId, true)
|
|
|
+ if sellerWxUser != nil {
|
|
|
+ helpers.OrderCreateNotify(*sellerWxUser, order.CreatedAt, product.Name, order.OrderId, "您有新的商品订单,请及时处理发货。", order_model.STATUS_CN_TEXT[order.Status], order.Count, fmt.Sprintf("pages/start/start?url=packageMerchant/pages/merchant/orders/orders&id=%d", item.MerchantId))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (self *PayController) CheckBalanceBeforePayExchange() {
|
|
|
+ oId := self.GetString("order_id")
|
|
|
+
|
|
|
+ //user := self.GetCurrentUser(false)
|
|
|
+ //if user == nil {
|
|
|
+ // self.ReturnError(403, apps.UserNeedLogin, "", nil)
|
|
|
+ //}
|
|
|
+ wxUser := self.GetCurrentWxUser(false)
|
|
|
+ if wxUser == nil {
|
|
|
+ self.ReturnError(403, apps.UserNeedLogin, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ order := order_model.GetOrderById(oId)
|
|
|
+ if order == nil || order.WxUserId != wxUser.Id {
|
|
|
+ self.ReturnError(404, apps.OrderNotExist, "", nil)
|
|
|
+ }
|
|
|
+ if order.Status != order_model.STATUS_UNPAY {
|
|
|
+ self.ReturnError(403, apps.NotUnPay, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ type Ret struct {
|
|
|
+ LackLuoboCount int64 `json:"lack_luobo_count"`
|
|
|
+ RechargeMoney int64 `json:"need_recharge_money"`
|
|
|
+ }
|
|
|
+
|
|
|
+ userLeftBalanceCount := balance_model.GetUserTotalBalance(wxUser.Id)
|
|
|
+ tp := order.UnitRoboBalancePrice * order.Count
|
|
|
+ lackCount := tp - userLeftBalanceCount //(订单金额-用户剩余代金券1000-500=500,1000-2000=-1000,1000-1000=0)
|
|
|
+ if lackCount < 0 {
|
|
|
+ lackCount = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ self.Data["json"] = &Ret{LackLuoboCount: lackCount, RechargeMoney: lackCount}
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+//处理生成抽奖码
|
|
|
+func CreateDrawCode(order *order_model.Order) {
|
|
|
+
|
|
|
+ createDrawCode.Lock()
|
|
|
+ defer createDrawCode.Unlock()
|
|
|
+
|
|
|
+ subject := subject_model.GetProductSaleSubjectById(order.ProductSaleSubjectId, true)
|
|
|
+ if subject == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for i := int64(1); i <= order.Count; i++ {
|
|
|
+ drawCode := subject_model.GetLastSaleDrawCodeBySubjectId(order.ProductSaleSubjectId)
|
|
|
+ if drawCode == nil {
|
|
|
+ new(subject_model.SaleDrawCode).Create(int64(1), order.WxUserId, order.ProductSaleSubjectId, order.OrderId)
|
|
|
+ } else {
|
|
|
+ new(subject_model.SaleDrawCode).Create(drawCode.Code+1, order.WxUserId, order.ProductSaleSubjectId, order.OrderId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ go TmplMsgNotice(order)
|
|
|
+}
|
|
|
+
|
|
|
+func AnalyseMallBalance(user *user_model.User, platform *exchange_model.Platform, order *order_model.Order) bool {
|
|
|
+
|
|
|
+ if user != nil {
|
|
|
+ //查询是否在通用白名单上
|
|
|
+ if user.IsUnlimitPayProduct {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ //第五创查询
|
|
|
+ resultAnalyse := helpers.AnalysePlatformMallBalance(platform.Secret, user.Tel, helpers.ANALYSE_URL)
|
|
|
+ beego.BeeLogger.Warn("pay_controller.AnalyseMallBalance().Platform analyse retMsg:%v", resultAnalyse)
|
|
|
+ //查询错误,则提示用户错误请稍后重试
|
|
|
+ if resultAnalyse == nil || resultAnalyse.Normal == helpers.UNNORMAL {
|
|
|
+ return false
|
|
|
+ } else if resultAnalyse.Normal == helpers.UNNORMAL_POINT_TASK {
|
|
|
+ //非正常的代金券任务刷子,不能继续支付,关闭订单,且更新为黑名单用户
|
|
|
+ order.Status = order_model.STATUS_CLOSED
|
|
|
+ order.Save()
|
|
|
+ user.IsBlackUser = 1
|
|
|
+ user.Save()
|
|
|
+ return false
|
|
|
+ } else if resultAnalyse.Normal == helpers.NORMAL {
|
|
|
+ user.IsUnlimitPayProduct = true
|
|
|
+ user.Save()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|