|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package hdl
-
- import (
- "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"
- "encoding/json"
- "github.com/gin-gonic/gin"
- )
-
- 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
- }
- outTradeNo, tradeNo, total, err := svc.BuyPackage(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 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 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
- }
- 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
- }
- for _, v := range data {
- v.OrdNo = outTradeNo
- }
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
- if err != nil {
- e.OutErr(c, e.ERR, err5.Error())
- return
- }
- }
-
- _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(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.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
- }, nil)
- return
- }
|