|
@@ -1,22 +1,26 @@ |
|
|
package svc |
|
|
package svc |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"applet/app/customer/enum" |
|
|
"applet/app/customer/md" |
|
|
"applet/app/customer/md" |
|
|
"applet/app/db" |
|
|
"applet/app/db" |
|
|
"applet/app/db/model" |
|
|
"applet/app/db/model" |
|
|
enum2 "applet/app/enum" |
|
|
enum2 "applet/app/enum" |
|
|
|
|
|
svc3 "applet/app/svc" |
|
|
"applet/app/utils" |
|
|
"applet/app/utils" |
|
|
"applet/app/utils/logx" |
|
|
"applet/app/utils/logx" |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"github.com/gin-gonic/gin" |
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core" |
|
|
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/partnerpayments/jsapi" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"net/url" |
|
|
"net/url" |
|
|
"time" |
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func BuyPackage(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, total string, err error) { |
|
|
|
|
|
|
|
|
func BuyPackageForAli(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, total string, err error) { |
|
|
user := GetUser(c) |
|
|
user := GetUser(c) |
|
|
session := db.Db.NewSession() |
|
|
session := db.Db.NewSession() |
|
|
defer session.Close() |
|
|
defer session.Close() |
|
@@ -117,6 +121,7 @@ func BuyPackage(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, tota |
|
|
OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait, |
|
|
OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait, |
|
|
ReqContent: string(utils.Serialize(req)), |
|
|
ReqContent: string(utils.Serialize(req)), |
|
|
WithDayData: string(utils.Serialize(data)), |
|
|
WithDayData: string(utils.Serialize(data)), |
|
|
|
|
|
PayWay: enum.PayWayForAli, |
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
}) |
|
|
}) |
|
@@ -129,6 +134,126 @@ func BuyPackage(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, tota |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func BuyPackageForWx(c *gin.Context, req md.BuyPackageReq) (outTradeNo, prepayId, total string, err error) { |
|
|
|
|
|
user := GetUser(c) |
|
|
|
|
|
session := db.Db.NewSession() |
|
|
|
|
|
defer session.Close() |
|
|
|
|
|
session.Begin() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
_ = session.Rollback() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//1、判断是否为教师 |
|
|
|
|
|
isTeacher := false |
|
|
|
|
|
userIdentityDb := db.UserIdentityDb{} |
|
|
|
|
|
userIdentityDb.Set(0) |
|
|
|
|
|
userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
if userIdentity == nil { |
|
|
|
|
|
err = errors.New("未查询到对应身份记录") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
if userIdentity.Identity == enum2.UserIdentityForCentralKitchenForTeacher { |
|
|
|
|
|
isTeacher = true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//2、计算数据(1:按学期购买 2:按月购买 3:按天购买 4:补餐) |
|
|
|
|
|
var totalPrice float64 |
|
|
|
|
|
var data []*model.CentralKitchenForSchoolUserWithDay |
|
|
|
|
|
if req.Kind == 1 { |
|
|
|
|
|
totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if req.Kind == 2 { |
|
|
|
|
|
totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if req.Kind == 3 { |
|
|
|
|
|
totalPrice, data, err = CalcByDay(user.Id, isTeacher, req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if req.Kind == 4 { |
|
|
|
|
|
totalPrice, data, err = CalcSupplementaryByDay(user.Id, isTeacher, req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
total = utils.Float64ToStr(totalPrice) |
|
|
|
|
|
|
|
|
|
|
|
//3、生成订单号 |
|
|
|
|
|
outTradeNo = utils.OrderUUID(user.Id) |
|
|
|
|
|
|
|
|
|
|
|
//4、请求 /v3/pay/partner/transactions/jsapi (生成预支付交易单) |
|
|
|
|
|
sysCfgDb := db.SysCfgDb{} |
|
|
|
|
|
sysCfgDb.Set() |
|
|
|
|
|
client, err := svc3.NewWxPayClient(c) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
wxSvc := jsapi.JsapiApiService{Client: client} |
|
|
|
|
|
sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.WxSpAppId, enum2.WxSpMchId, enum2.WxAppletAppId, enum2.WxMchId, enum2.WxJsapiPayNotifyUrl) |
|
|
|
|
|
|
|
|
|
|
|
resp, _, err := wxSvc.Prepay(c, |
|
|
|
|
|
jsapi.PrepayRequest{ |
|
|
|
|
|
SpAppid: core.String(sysCfg[enum2.WxSpAppId]), |
|
|
|
|
|
SpMchid: core.String(sysCfg[enum2.WxSpMchId]), |
|
|
|
|
|
SubAppid: core.String(sysCfg[enum2.WxAppletAppId]), |
|
|
|
|
|
SubMchid: core.String(sysCfg[enum2.WxMchId]), |
|
|
|
|
|
Description: core.String("购买食堂套餐"), |
|
|
|
|
|
OutTradeNo: core.String(outTradeNo), |
|
|
|
|
|
TimeExpire: core.Time(time.Now().Add(30 * time.Minute)), |
|
|
|
|
|
//Attach: core.String("自定义数据说明"), |
|
|
|
|
|
NotifyUrl: core.String(sysCfg[enum2.WxJsapiPayNotifyUrl]), |
|
|
|
|
|
Amount: &jsapi.Amount{ |
|
|
|
|
|
Currency: core.String("CNY"), |
|
|
|
|
|
Total: core.Int64(utils.StrToInt64(total) * 100), |
|
|
|
|
|
}, |
|
|
|
|
|
Payer: &jsapi.Payer{ |
|
|
|
|
|
//SpOpenid: core.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"), |
|
|
|
|
|
SubOpenid: core.String(user.OpenId), |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
//5、插入订单记录 |
|
|
|
|
|
now := time.Now() |
|
|
|
|
|
centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} |
|
|
|
|
|
centralKitchenForSchoolPackageOrd.Set(outTradeNo) |
|
|
|
|
|
_, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdInsertBySession(session, &model.CentralKitchenForSchoolPackageOrd{ |
|
|
|
|
|
EnterpriseId: req.EnterpriseId, |
|
|
|
|
|
Uid: user.Id, |
|
|
|
|
|
UserIdentityId: req.UserIdentityId, |
|
|
|
|
|
UserIdentityName: userIdentity.Name, |
|
|
|
|
|
TotalPrice: total, |
|
|
|
|
|
Kind: req.Kind, |
|
|
|
|
|
OutTradeNo: outTradeNo, |
|
|
|
|
|
TradeNo: "", |
|
|
|
|
|
State: enum2.CentralKitchenForSchoolPackageOrdStateForWait, |
|
|
|
|
|
OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait, |
|
|
|
|
|
ReqContent: string(utils.Serialize(req)), |
|
|
|
|
|
WithDayData: string(utils.Serialize(data)), |
|
|
|
|
|
PayWay: enum.PayWayForWx, |
|
|
|
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
|
|
}) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
_ = session.Rollback() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
session.Commit() |
|
|
|
|
|
prepayId = *resp.PrepayId |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func NursingHomeBuyPackageReq(c *gin.Context, req md.NursingHomeBuyPackageReq) (outTradeNo, tradeNo, total string, err error) { |
|
|
func NursingHomeBuyPackageReq(c *gin.Context, req md.NursingHomeBuyPackageReq) (outTradeNo, tradeNo, total string, err error) { |
|
|
user := GetUser(c) |
|
|
user := GetUser(c) |
|
|
session := db.Db.NewSession() |
|
|
session := db.Db.NewSession() |
|
|