package hdl import ( "applet/app/cfg" enum2 "applet/app/customer/enum" "applet/app/customer/lib/validate" "applet/app/customer/md" "applet/app/customer/svc" "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/enum" svc3 "applet/app/svc" "applet/app/utils" "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/services/partnerpayments/jsapi" jsapi2 "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi" utils2 "github.com/wechatpay-apiv3/wechatpay-go/utils" "strconv" "time" ) func BuyPackage(c *gin.Context) { var req md.BuyPackageReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } if cfg.Prd && req.EnterpriseId != 7 { //TODO::暂只开放金茂小学支付宝购买权限 e.OutErr(c, e.ERR, "暂不支持支付宝支付!") return } err = svc.CheckIsOpenLimitOrderFood(req.EnterpriseId) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } fmt.Println("req>>>>>>>>>>>>", utils.Serialize(req)) outTradeNo, tradeNo, total, err := svc.BuyPackageForAli(c, req) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, map[string]interface{}{ "out_trade_no": outTradeNo, "trade_no": tradeNo, "total": total, }, nil) return } func BuyPackageForWx(c *gin.Context) { var req md.BuyPackageReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } utils.FilePutContents("BuyPackageForWx", utils.SerializeStr(req)) err = svc.CheckIsOpenLimitOrderFood(req.EnterpriseId) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } fmt.Println("req>>>>>>>>>>>>", utils.SerializeStr(req)) outTradeNo, total, resp, err := svc.BuyPackageForWx(c, req) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, map[string]interface{}{ "out_trade_no": outTradeNo, "prams": resp, "total": total, }, nil) return } func RePayBuyPackageForWx(c *gin.Context) { prepayId := c.DefaultQuery("prepay_id", "") centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} centralKitchenForSchoolPackageOrd.Set("") ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrdByPrepayId(prepayId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if ord == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录") return } err = svc.CheckIsOpenLimitOrderFood(ord.EnterpriseId) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() client, err := svc3.NewWxPayClient(c) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } wxSvc := jsapi.JsapiApiService{Client: client} resp := new(jsapi2.PrepayWithRequestPaymentResponse) sysCfg := sysCfgDb.SysCfgFindWithDb(enum.WxAppletAppId) resp.PrepayId = &prepayId resp.SignType = core.String("RSA") appid := sysCfg[enum.WxAppletAppId] resp.Appid = &appid resp.TimeStamp = core.String(strconv.FormatInt(time.Now().Unix(), 10)) nonce, err := utils2.GenerateNonce() if err != nil { e.OutErr(c, e.ERR, err.Error()) return } resp.NonceStr = core.String(nonce) resp.Package = core.String("prepay_id=" + prepayId) message := fmt.Sprintf("%s\n%s\n%s\n%s\n", *resp.Appid, *resp.TimeStamp, *resp.NonceStr, *resp.Package) signatureResult, err := wxSvc.Client.Sign(c, message) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } resp.PaySign = core.String(signatureResult.Signature) e.OutSuc(c, map[string]interface{}{ //"out_trade_no": ord.OutTradeNo, "prams": resp, //"total": ord.TradeNo, }, nil) return } func OrdState(c *gin.Context) { outTradeNo := c.DefaultQuery("out_trade_no", "") centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} centralKitchenForSchoolPackageOrd.Set(outTradeNo) ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if ord == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录") return } if !cfg.Prd && ord.PayWay == enum2.PayWayForAli { if ord.State == enum.CentralKitchenForSchoolPackageOrdStateForWait { //处于待支付状态,请求支付宝同步订单状态 sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey) err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{ Config: struct { PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"` PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"` PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"` }{ PayAliAppId: sysCfg[enum.OpenAlipayAppid], PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey], PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey], }, OutTradeNo: outTradeNo, AppAuthToken: sysCfg[enum.JsapiPayAppAutToken], }) if err2 != nil { e.OutErr(c, e.ERR, err2.Error()) return } ord.State, err2 = svc.DealCentralKitchenForSchoolOrderCallBack(outTradeNo, result.TradeStatus, result.Date) if err != nil { e.OutErr(c, e.ERR, err2.Error()) return } } } e.OutSuc(c, map[string]interface{}{ "sate": ord.State, "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)), }, nil) return } func NursingHomeBuyPackage(c *gin.Context) { var req md.NursingHomeBuyPackageReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } outTradeNo, tradeNo, total, err := svc.NursingHomeBuyPackageReq(c, req) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, map[string]interface{}{ "out_trade_no": outTradeNo, "trade_no": tradeNo, "total": total, }, nil) return } func NursingHomeOrdState(c *gin.Context) { outTradeNo := c.DefaultQuery("out_trade_no", "") centralKitchenForSchoolPackageOrd := db.NursingHomePackageOrd{} centralKitchenForSchoolPackageOrd.Set(outTradeNo) ord, err := centralKitchenForSchoolPackageOrd.GetNursingHomePackageOrd() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if ord == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录") return } if ord.State == enum.NursingHomePackageOrdStateForWait { //处于待支付状态,请求支付宝同步订单状态 sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey) err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{ Config: struct { PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"` PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"` PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"` }{ PayAliAppId: sysCfg[enum.OpenAlipayAppid], PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey], PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey], }, OutTradeNo: outTradeNo, AppAuthToken: sysCfg[enum.JsapiPayAppAutToken], }) if err2 != nil { e.OutErr(c, e.ERR, err2.Error()) return } if result.TradeStatus == "TRADE_CLOSED" { ord.State = enum.NursingHomePackageOrdStateForFail } if result.TradeStatus == "TRADE_SUCCESS" { ord.State = enum.NursingHomePackageOrdStateForSuccess ord.OrdState = enum.NursingHomePackageOrdOrdStateForSuccess //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day` var data []*model.NursingHomeUserWithDay err4 := json.Unmarshal([]byte(ord.WithDayData), &data) if err4 != nil { e.OutErr(c, e.ERR, err4.Error()) return } var hasMonths = map[string]map[string]interface{}{} var hasKinds = map[string]map[string]string{} var hasTotalPrice = map[string]map[string]float64{} for _, v := range data { date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local) if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil { hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{}) hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02") hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02") } else { startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local) if startTime.After(date) { hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02") } endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local) if endTime.Before(date) { hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02") } } if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil { hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64) } if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil { hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string) } hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month()) hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind) hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount) v.OrdNo = outTradeNo } centralKitchenForSchoolUserWithDayDb := db.NursingHomeUserWithDayDb{} centralKitchenForSchoolUserWithDayDb.Set(0) _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddNursingHomeUserWithDays(data) if err != nil { e.OutErr(c, e.ERR, err5.Error()) return } //TODO::插入数据至 `nursing_home_package_ord_for_reserve` centralKitchenForSchoolPackageOrdForReserve := db.NursingHomePackageOrdForReserve{} centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo) var isHasKind []string for k, v := range hasMonths { for _, v1 := range hasKinds[k] { if !utils.InArr(v1, isHasKind) { _, err3 := centralKitchenForSchoolPackageOrdForReserve.NursingHomePackageOrdForReserveInsert(&model.NursingHomePackageOrdForReserve{ EnterpriseId: ord.EnterpriseId, Uid: ord.Uid, UserIdentityName: ord.UserIdentityName, UserIdentityId: ord.UserIdentityId, Kind: utils.StrToInt(v1), OutTradeNo: ord.OutTradeNo, ReserveMonth: utils.AnyToString(v["month"]), MealTimeStart: utils.AnyToString(v["meal_time_start"]), MealTimeEnd: utils.AnyToString(v["meal_time_end"]), TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]), CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), }) if err3 != nil { e.OutErr(c, e.ERR, err3.Error()) return } isHasKind = append(isHasKind, v1) } } } } _, err3 := centralKitchenForSchoolPackageOrd.NursingHomePackageOrdUpdate(ord, "state", "ord_state") if err3 != nil { e.OutErr(c, e.ERR_DB_ORM, err3.Error()) return } } e.OutSuc(c, map[string]interface{}{ "sate": ord.State, "sate_zh": enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdState(ord.State)), }, nil) return }