diff --git a/app/customer/hdl/hdl_call_back.go b/app/customer/hdl/hdl_call_back.go new file mode 100644 index 0000000..687ba20 --- /dev/null +++ b/app/customer/hdl/hdl_call_back.go @@ -0,0 +1,33 @@ +package hdl + +import ( + "applet/app/customer/md" + "applet/app/customer/svc" + "applet/app/e" + "fmt" + "github.com/gin-gonic/gin" +) + +func AlipayJsApiCallBack(c *gin.Context) { + data := svc.CheckAllCallbackParams(c) + if data == nil { + e.OutErr(c, e.ERR_INVALID_ARGS) + return + } + c.Set("callback", data) + args, ok := data.(*md.AliPayCallback) + if !ok { + fmt.Println("反射ok失败") + e.OutErr(c, e.ERR_INVALID_ARGS, "反射失败") + return + } + + _, err := svc.DealCentralKitchenForSchoolOrderCallBack(args.OutTradeNo, args.TradeStatus) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + //TODO::此处需要直接输出 success,避免支付宝重复回调 + //c.Header("Content-Type", "text/html; charset=utf-8") + c.String(200, "success") +} diff --git a/app/customer/hdl/hdl_pay.go b/app/customer/hdl/hdl_pay.go index 8fbcb3b..73af10f 100644 --- a/app/customer/hdl/hdl_pay.go +++ b/app/customer/hdl/hdl_pay.go @@ -38,6 +38,7 @@ func BuyPackage(c *gin.Context) { func OrdState(c *gin.Context) { outTradeNo := c.DefaultQuery("out_trade_no", "") + centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} centralKitchenForSchoolPackageOrd.Set(outTradeNo) ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd() @@ -71,92 +72,9 @@ func OrdState(c *gin.Context) { e.OutErr(c, e.ERR, err2.Error()) return } - if result.TradeStatus == "TRADE_CLOSED" { - ord.State = enum.CentralKitchenForSchoolPackageOrdStateForFail - } - - if result.TradeStatus == "TRADE_SUCCESS" { - ord.State = enum.CentralKitchenForSchoolPackageOrdStateForSuccess - ord.OrdState = enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess - - //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day` - var data []*model.CentralKitchenForSchoolUserWithDay - 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.CentralKitchenForSchoolUserWithDayDb{} - centralKitchenForSchoolUserWithDayDb.Set(0) - _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data) - if err != nil { - e.OutErr(c, e.ERR, err5.Error()) - return - } - - //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve` - centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{} - centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo) - for k, v := range hasMonths { - for _, v1 := range hasKinds[k] { - _, err3 := centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{ - 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 - } - } - } - } - _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state") - if err3 != nil { - e.OutErr(c, e.ERR_DB_ORM, err3.Error()) + ord.State, err2 = svc.DealCentralKitchenForSchoolOrderCallBack(outTradeNo, result.TradeStatus) + if err != nil { + e.OutErr(c, e.ERR, err2.Error()) return } } diff --git a/app/customer/md/alipay.go b/app/customer/md/alipay.go new file mode 100644 index 0000000..c798fd0 --- /dev/null +++ b/app/customer/md/alipay.go @@ -0,0 +1,34 @@ +package md + +// AliPayCallback 支付宝的回调结构体 +type AliPayCallback struct { + AppID string `json:"app_id"` + AuthAppID string `json:"auth_app_id"` + BuyerID string `json:"buyer_id"` + BuyerLogonID string `json:"buyer_logon_id"` + BuyerPayAmount string `json:"buyer_pay_amount"` + Charset string `json:"charset"` + FundBillList string `json:"fund_bill_list"` + GmtCreate string `json:"gmt_create"` + GmtPayment string `json:"gmt_payment"` + InvoiceAmount string `json:"invoice_amount"` + OrderType string `json:"order_type"` + MasterID string `json:"master_id"` + NotifyID string `json:"notify_id"` + NotifyTime string `json:"notify_time"` + NotifyType string `json:"notify_type"` + OutTradeNo string `json:"out_trade_no"` + PassbackParams string `json:"passback_params"` + PointAmount string `json:"point_amount"` + ReceiptAmount string `json:"receipt_amount"` + SellerEmail string `json:"seller_email"` + SellerID string `json:"seller_id"` + Sign string `json:"sign"` + SignType string `json:"sign_type"` + Subject string `json:"subject"` + TotalAmount string `json:"total_amount"` + TradeNo string `json:"trade_no"` + TradeStatus string `json:"trade_status"` + Version string `json:"version"` + PayMethod string `json:"pay_method"` +} diff --git a/app/customer/svc/svc_pay.go b/app/customer/svc/svc_pay.go index c26ff25..6cad2c2 100644 --- a/app/customer/svc/svc_pay.go +++ b/app/customer/svc/svc_pay.go @@ -6,8 +6,13 @@ import ( "applet/app/db/model" enum2 "applet/app/enum" "applet/app/utils" + "applet/app/utils/logx" + "encoding/json" "errors" + "fmt" "github.com/gin-gonic/gin" + "io/ioutil" + "net/url" "time" ) @@ -230,3 +235,139 @@ func NursingHomeBuyPackageReq(c *gin.Context, req md.NursingHomeBuyPackageReq) ( tradeNo = resp.TradeNo return } + +func DealCentralKitchenForSchoolOrderCallBack(outTradeNo, tradeStatus string) (state int, err error) { + centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} + centralKitchenForSchoolPackageOrd.Set(outTradeNo) + ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd() + if err != nil { + return + } + if ord == nil { + err = errors.New("未查询到对应订单记录") + return + } + + if ord.State == enum2.CentralKitchenForSchoolPackageOrdStateForWait { + if tradeStatus == "TRADE_CLOSED" { + ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForFail + } + + if tradeStatus == "TRADE_SUCCESS" { + ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForSuccess + ord.OrdState = enum2.CentralKitchenForSchoolPackageOrdOrdStateForSuccess + + //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day` + var data []*model.CentralKitchenForSchoolUserWithDay + err = json.Unmarshal([]byte(ord.WithDayData), &data) + if err != nil { + 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.CentralKitchenForSchoolUserWithDayDb{} + centralKitchenForSchoolUserWithDayDb.Set(0) + _, err = centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data) + if err != nil { + return + } + + //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve` + centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{} + centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo) + for k, v := range hasMonths { + for _, v1 := range hasKinds[k] { + _, err = centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{ + 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 err != nil { + return + } + } + } + } + _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state") + if err != nil { + return + } + } + state = ord.State + return +} + +func CheckAllCallbackParams(c *gin.Context) interface{} { + body, _ := ioutil.ReadAll(c.Request.Body) + utils.FilePutContents("CheckAllCallbackParams", utils.SerializeStr(string(body))) + dataAlipay, _ := GetAlipayCallbackParams(body) + if dataAlipay != nil { + return dataAlipay + } + return nil +} + +// GetAlipayCallbackParams 支付宝参数解析 +func GetAlipayCallbackParams(body []byte) (*md.AliPayCallback, error) { + decodeArgs, err := url.QueryUnescape(string(body)) + if err != nil { + _ = logx.Warn(err) + return nil, logx.Warn("回调参数解码错误") + } + data, err := url.ParseQuery(decodeArgs) + if err != nil { + return nil, err + } + dataMap := make(map[string]interface{}) + for k := range data { + dataMap[k] = data.Get(k) + } + callbackStr := utils.Serialize(dataMap) + fmt.Println("支付宝回调数据", string(callbackStr)) + var args md.AliPayCallback + if err := json.Unmarshal(callbackStr, &args); err != nil { + return nil, logx.Warn(err) + } + return &args, nil +} diff --git a/app/router/customer_router.go b/app/router/customer_router.go index f91a3df..e887bf4 100644 --- a/app/router/customer_router.go +++ b/app/router/customer_router.go @@ -18,6 +18,7 @@ func CustomerInit(r *gin.RouterGroup) { r.GET("/sanHu", hdl.SanHu) r.POST("/test", hdl.Demo) r.POST("/curlAlipayPlanetEcocampusApiRosterSignUpInfoTest", hdl.CurlAlipayPlanetEcocampusApiRosterSignUpInfo) + r.POST("/alipayJsApiCallBack", hdl.AlipayJsApiCallBack) r.POST("/login", hdl.Login) r.POST("/register", hdl.Register)