|
@@ -1,6 +1,7 @@
|
|
|
package pay
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"jfw/config"
|
|
@@ -18,40 +19,49 @@ import (
|
|
|
//订单结构
|
|
|
type Order struct {
|
|
|
*xweb.Action
|
|
|
- buyStatus xweb.Mapper `xweb:"/front/vipsubscribe/buyStatus"` //获取用户已购买
|
|
|
- createOrder xweb.Mapper `xweb:"/front/vipsubscribe/createOrder"` //创建订单
|
|
|
+ createOrder xweb.Mapper `xweb:"/front/vipsubscribe/createOrder"` //创建订单并支付
|
|
|
+ orderToPay xweb.Mapper `xweb:"/front/vipsubscribe/orderToPay"` //支付已创建订单
|
|
|
}
|
|
|
|
|
|
-//获取已购买选项
|
|
|
-func (this *Order) BuyStatus() {
|
|
|
- userid := qutil.ObjToString(this.GetSession("userId"))
|
|
|
- r := map[string]interface{}{"success": false}
|
|
|
- if userid != "" {
|
|
|
- m, ok := public.MQFW.FindById("user", userid, `{"i_vipstatus":1,"o_area":1,"a_buyerindustry":1}`)
|
|
|
- if ok && len(*m) > 0 {
|
|
|
- r["success"] = true
|
|
|
- r["area"] = qutil.ObjToMap((*m)["o_area"])
|
|
|
- r["industry"] = (*m)["a_buyerindustry"]
|
|
|
- //r["industry"] = qutil.obj (*m)["a_buyerindustry"]
|
|
|
- r["vipStatus"] = qutil.IntAll((*m)["i_vipstatus"])
|
|
|
- }
|
|
|
+func init() {
|
|
|
+ xweb.AddAction(&Order{})
|
|
|
+}
|
|
|
+
|
|
|
+type orderResult struct {
|
|
|
+ Success bool //是否成功
|
|
|
+ Err error //错误
|
|
|
+ Data map[string]interface{}
|
|
|
+}
|
|
|
+
|
|
|
+func (o *orderResult) Format() *map[string]interface{} {
|
|
|
+ errStr := ""
|
|
|
+ if o.Err != nil {
|
|
|
+ errStr = o.Err.Error()
|
|
|
+ }
|
|
|
+ return &map[string]interface{}{
|
|
|
+ "success": o.Success,
|
|
|
+ "errMsg": errStr,
|
|
|
+ "data": o.Data,
|
|
|
}
|
|
|
- this.ServeJson(r)
|
|
|
}
|
|
|
|
|
|
-//去支付
|
|
|
+//创建订单并支付
|
|
|
func (this *Order) CreateOrder() {
|
|
|
area := qutil.ObjToMap(this.GetString("area"))
|
|
|
industry := strings.Split(this.GetString("industry"), ",")
|
|
|
- date := this.GetString("date")
|
|
|
+ date := this.GetString("time")
|
|
|
payWay := this.GetString("payWay")
|
|
|
userId := qutil.ObjToString(this.GetSession("userId"))
|
|
|
openId := qutil.ObjToString(this.GetSession("s_m_openid"))
|
|
|
- ok, str := func() (bool, string) {
|
|
|
+ r := func() *orderResult {
|
|
|
+ now := time.Now()
|
|
|
//数据校验
|
|
|
+ if len(industry) == 1 { //去掉[""]
|
|
|
+ industry = []string{}
|
|
|
+ }
|
|
|
date_count, date_unit, err := checkReqDate(date)
|
|
|
if err != nil {
|
|
|
- return false, err.Error()
|
|
|
+ return &orderResult{false, err, nil}
|
|
|
}
|
|
|
log.Println(area, industry, date, payWay)
|
|
|
//插入订单表
|
|
@@ -62,34 +72,128 @@ func (this *Order) CreateOrder() {
|
|
|
"s_openid": openId,
|
|
|
"i_cyclecount": date_count, //时长
|
|
|
"i_cycleunit": date_unit, //单位
|
|
|
- "i_comeintime": time.Now().Unix(),
|
|
|
+ "i_comeintime": now.Unix(),
|
|
|
})
|
|
|
if mog_id == "" {
|
|
|
- return false, "创建订单出错"
|
|
|
+ return &orderResult{false, errors.New("创建订单出错"), nil}
|
|
|
}
|
|
|
//计算价格
|
|
|
totalfee := 1
|
|
|
//创建订单
|
|
|
tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), "e", this.IP(), openId, "", totalfee)
|
|
|
- if ret == nil {
|
|
|
- return false, "创建微信订单出错"
|
|
|
+ if ret == nil || (*ret)["status"] != "1" {
|
|
|
+ errMsg := "创建微信订单出错"
|
|
|
+ if (*ret)["errcodedes"] != "" {
|
|
|
+ errMsg = (*ret)["errcodedes"]
|
|
|
+ } else if (*ret)["errcode"] != "" {
|
|
|
+ errMsg = (*ret)["errcode"]
|
|
|
+ }
|
|
|
+ return &orderResult{false, errors.New(errMsg), nil}
|
|
|
}
|
|
|
- log.Println(tradeno, ret)
|
|
|
//存入订单表
|
|
|
+ ordercode := public.GetOrderCode(openId)
|
|
|
+ prepayid := qutil.ObjToString((*ret)["prepayid"])
|
|
|
+ jsPayParam := GetWxJsPayStr(prepayid)
|
|
|
|
|
|
- //返回订单支付字符串
|
|
|
- return true, ""
|
|
|
- }()
|
|
|
+ filter := map[string]interface{}{
|
|
|
+ "_id": mog_id,
|
|
|
+ "area": area,
|
|
|
+ "industry": industry,
|
|
|
+ "cyclecount": date_count, //时长
|
|
|
+ "cycleunit": date_unit, //单位
|
|
|
+ }
|
|
|
+ filterStr, _ := json.Marshal(filter)
|
|
|
|
|
|
- this.ServeJson(map[string]interface{}{
|
|
|
- "success": ok,
|
|
|
- "Str": str,
|
|
|
- })
|
|
|
+ orderid := public.Mysql.Insert("dataexport_order", map[string]interface{}{
|
|
|
+ "order_money": totalfee,
|
|
|
+ "order_status": 0,
|
|
|
+ "user_nickname": qutil.ObjToString(this.GetSession("s_nickname")),
|
|
|
+ "user_openid": openId,
|
|
|
+ "prepay_id": prepayid,
|
|
|
+ "code_url": jsPayParam,
|
|
|
+ "out_trade_no": tradeno,
|
|
|
+ "order_code": ordercode,
|
|
|
+ "product_type": "vip订阅",
|
|
|
+ "create_time": qutil.FormatDate(&now, qutil.Date_Full_Layout),
|
|
|
+ "prepay_time": qutil.FormatDate(&now, qutil.Date_Full_Layout),
|
|
|
+ "original_price": totalfee,
|
|
|
+ "filter_id": mog_id,
|
|
|
+ "pay_way": payWay,
|
|
|
+ "user_id": userId,
|
|
|
+ "filter": string(filterStr), //筛选
|
|
|
+ })
|
|
|
+ if orderid == -1 {
|
|
|
+ return &orderResult{false, errors.New("数据库操作异常"), nil}
|
|
|
+ }
|
|
|
+ return &orderResult{true, nil, map[string]interface{}{
|
|
|
+ "code": ordercode,
|
|
|
+ "res": jsPayParam,
|
|
|
+ }}
|
|
|
+ }()
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf("%s CreateOrder err:%v\n", userId, r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
}
|
|
|
|
|
|
-//查询订单是否支付
|
|
|
-func (this *Order) IsPaySuccess() {
|
|
|
-
|
|
|
+//支付已创建订单
|
|
|
+func (this *Order) OrderToPay() {
|
|
|
+ orderCode := qutil.ObjToString(this.GetString("orderCode"))
|
|
|
+ userId := qutil.ObjToString(this.GetSession("userId"))
|
|
|
+ r := func() *orderResult {
|
|
|
+ //根据订单号获取订单数据
|
|
|
+ query := map[string]interface{}{
|
|
|
+ "user_id": userId,
|
|
|
+ "order_code": orderCode,
|
|
|
+ "order_status": 0,
|
|
|
+ }
|
|
|
+ oData := public.Mysql.FindOne("dataexport_order", query, "", "")
|
|
|
+ if oData == nil || len(*oData) == 0 {
|
|
|
+ return &orderResult{false, errors.New("未知订单"), nil}
|
|
|
+ }
|
|
|
+ //判断订单是否超过两小时
|
|
|
+ prepayTime, _ := time.ParseInLocation(qutil.Date_Full_Layout, qutil.ObjToString((*oData)["prepay_time"]), time.Local)
|
|
|
+ jsPayParam := qutil.ObjToString((*oData)["code_url"])
|
|
|
+ if time.Now().Before(prepayTime.Add(time.Hour*2)) && jsPayParam != "" {
|
|
|
+ //未过期
|
|
|
+ return &orderResult{true, nil, map[string]interface{}{
|
|
|
+ "res": jsPayParam,
|
|
|
+ }}
|
|
|
+ }
|
|
|
+ //重新生成订单
|
|
|
+ openId := qutil.ObjToString((*oData)["user_openid"])
|
|
|
+ totalfee := qutil.IntAll((*oData)["order_money"])
|
|
|
+ tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), "e", this.IP(), openId, "", totalfee)
|
|
|
+ if ret == nil || (*ret)["status"] != "1" {
|
|
|
+ errMsg := "创建微信订单出错"
|
|
|
+ if (*ret)["errcodedes"] != "" {
|
|
|
+ errMsg = (*ret)["errcodedes"]
|
|
|
+ } else if (*ret)["errcode"] != "" {
|
|
|
+ errMsg = (*ret)["errcode"]
|
|
|
+ }
|
|
|
+ return &orderResult{false, errors.New(errMsg), nil}
|
|
|
+ }
|
|
|
+ prepayid := qutil.ObjToString((*ret)["prepayid"])
|
|
|
+ jsPayParam = GetWxJsPayStr(prepayid)
|
|
|
+ //更新订单表
|
|
|
+ now := time.Now()
|
|
|
+ ok := public.Mysql.Update("dataexport_order", query, map[string]interface{}{
|
|
|
+ "code_url": jsPayParam,
|
|
|
+ "prepay_time": qutil.FormatDate(&now, qutil.Date_Full_Layout),
|
|
|
+ "out_trade_no": tradeno,
|
|
|
+ "prepay_id": prepayid,
|
|
|
+ })
|
|
|
+ if !ok {
|
|
|
+ return &orderResult{false, errors.New("数据库操作异常"), nil}
|
|
|
+ }
|
|
|
+ return &orderResult{true, nil, map[string]interface{}{
|
|
|
+ "res": jsPayParam,
|
|
|
+ }}
|
|
|
+ }()
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf("%s CreateOrder err:%v\n", userId, r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
}
|
|
|
|
|
|
//cycleunit(1:年 2:月)
|
|
@@ -104,7 +208,7 @@ func checkReqDate(dateStr string) (cyclecount, cycleunit int, err error) {
|
|
|
}
|
|
|
return
|
|
|
} else if strings.HasSuffix(dateStr, "月") {
|
|
|
- cycleunit = 1
|
|
|
+ cycleunit = 2
|
|
|
dateStr = strings.Replace(dateStr, "个月", "", -1)
|
|
|
cyclecount, err = strconv.Atoi(dateStr)
|
|
|
if cyclecount > 12 && err == nil {
|