|
@@ -0,0 +1,365 @@
|
|
|
+package pay
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/base64"
|
|
|
+ "encoding/json"
|
|
|
+ "jfw/config"
|
|
|
+ "jfw/public"
|
|
|
+ "log"
|
|
|
+ "qfw/util"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/SKatiyar/qr"
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
+)
|
|
|
+
|
|
|
+type DataExportPayAction struct {
|
|
|
+ *xweb.Action
|
|
|
+ sacnPay_WaitPay xweb.Mapper `xweb:"/dataExport/sacnPay/waitPay"` //生成支付二维码
|
|
|
+ sacnPay_CreateOrder xweb.Mapper `xweb:"/dataExport/sacnPay/createOrder"` //创建订单
|
|
|
+ isPaySuccess xweb.Mapper `xweb:"/dataExport/pay/isPaySuccess"` //是否支付成功
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ xweb.AddAction(&DataExportPayAction{})
|
|
|
+}
|
|
|
+
|
|
|
+func (p *DataExportPayAction) SacnPay_WaitPay() error {
|
|
|
+ defer util.Catch()
|
|
|
+ openid, _ := p.GetSession("s_m_openid").(string)
|
|
|
+ code := p.GetString("code")
|
|
|
+ token := p.GetString("token")
|
|
|
+ surplus, money, qrcode, pay_way := func() (int64, int, string, string) {
|
|
|
+ if openid == "" || code == "" || token == "" {
|
|
|
+ return 0, 0, "", ""
|
|
|
+ }
|
|
|
+ data := public.Mysql.FindOne("dataexport_order", map[string]interface{}{"order_code": code, "user_openid": openid}, "id,code_url,prepay_time,order_money,order_status,pay_way", "")
|
|
|
+ if data == nil {
|
|
|
+ log.Println("订单号不存在!", openid, " code", code)
|
|
|
+ return 0, 0, "", ""
|
|
|
+ }
|
|
|
+ if util.IntAll((*data)["order_status"]) != 0 {
|
|
|
+ log.Println("订单状态不允许支付!", openid)
|
|
|
+ return 0, 0, "", ""
|
|
|
+ }
|
|
|
+ orderid := util.Int64All((*data)["id"])
|
|
|
+ orderMoney := util.IntAll((*data)["order_money"])
|
|
|
+ pay_way := util.ObjToString((*data)["pay_way"])
|
|
|
+ realToken := public.GetWaitPayToken(orderid, orderMoney, code, pay_way, openid)
|
|
|
+ if realToken != token {
|
|
|
+ log.Println("token错误!", openid, realToken, token)
|
|
|
+ return 0, 0, "", ""
|
|
|
+ }
|
|
|
+ prepayTime, err := time.ParseInLocation(util.Date_Full_Layout, util.ObjToString((*data)["prepay_time"]), time.Local)
|
|
|
+ surplus_second := prepayTime.Unix() + 7200 - time.Now().Unix()
|
|
|
+ codeUrl := util.ObjToString((*data)["code_url"])
|
|
|
+ if err == nil && surplus_second <= 0 { //重新生成预订单
|
|
|
+ log.Println("二维码已失效,重新生成预订单!", openid)
|
|
|
+ surplus_second = 0
|
|
|
+ now := time.Now()
|
|
|
+ if pay_way == "微信" {
|
|
|
+ tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), "b", p.IP(), openid, "", orderMoney)
|
|
|
+ if ret != nil {
|
|
|
+ if public.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
+ "id": orderid,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "prepay_time": util.FormatDate(&now, util.Date_Full_Layout),
|
|
|
+ "prepay_id": (*ret)["prepayid"],
|
|
|
+ "code_url": (*ret)["codeurl"],
|
|
|
+ "out_trade_no": tradeno,
|
|
|
+ }) {
|
|
|
+ surplus_second = 7200
|
|
|
+ codeUrl = util.ObjToString((*ret)["codeurl"])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if pay_way == "支付宝" {
|
|
|
+ if qr_url, tradeno, err := Alipay.GetPayUrl(util.Float64All(orderMoney)); err == nil {
|
|
|
+ if public.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
+ "id": orderid,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "prepay_time": util.FormatDate(&now, util.Date_Full_Layout),
|
|
|
+ "prepay_id": "",
|
|
|
+ "code_url": qr_url,
|
|
|
+ "out_trade_no": tradeno,
|
|
|
+ }) {
|
|
|
+ surplus_second = 7200
|
|
|
+
|
|
|
+ }
|
|
|
+ codeUrl = qr_url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return surplus_second, orderMoney, codeUrl, pay_way
|
|
|
+ }()
|
|
|
+ if pay_way == "微信" {
|
|
|
+ r, _ := qr.Encode(qrcode, qr.M)
|
|
|
+ pngdat := r.PNG()
|
|
|
+ qrcode = base64.StdEncoding.EncodeToString(pngdat)
|
|
|
+ }
|
|
|
+ p.T["money"] = float64(money) / 100
|
|
|
+ p.T["surplus_second"] = surplus
|
|
|
+ p.T["qrcode"] = qrcode
|
|
|
+ p.T["ordercode"] = code
|
|
|
+ p.T["pay_way"] = pay_way
|
|
|
+ return p.Render("/pc/waitpay.html")
|
|
|
+}
|
|
|
+func (p *DataExportPayAction) SacnPay_CreateOrder() {
|
|
|
+ defer util.Catch()
|
|
|
+ openId, _ := p.GetSession("s_m_openid").(string)
|
|
|
+ id := util.SE.Decode4Hex(p.GetString("id"))
|
|
|
+ if openId == "" || id == "" {
|
|
|
+ log.Println("openId,id错误", openId, id)
|
|
|
+ p.ServeJson(map[string]interface{}{
|
|
|
+ "status": "n",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //
|
|
|
+ data_spec := p.GetString("data_spec")
|
|
|
+ if data_spec == "standard" {
|
|
|
+ data_spec = "标准字段包"
|
|
|
+ } else if data_spec == "senior" {
|
|
|
+ data_spec = "高级字段包"
|
|
|
+ }
|
|
|
+ user_mail_status := p.GetSession("EMVerifySucess").(bool)
|
|
|
+ pay_way := p.GetString("pay_way")
|
|
|
+ if pay_way == "weixin" {
|
|
|
+ pay_way = "微信"
|
|
|
+ } else if pay_way == "alipay" {
|
|
|
+ pay_way = "支付宝"
|
|
|
+ }
|
|
|
+ user_mail, _ := p.GetSession("DataExportVerifyEmail_val").(string)
|
|
|
+ user_phone, _ := p.GetSession("DataExportVerifyPhone").(string)
|
|
|
+ if user_phone != "" {
|
|
|
+ p.SetSession("DataExportVerifyPhone_val", user_phone) //提交后下次默认带出手机号
|
|
|
+ } else {
|
|
|
+ user_phone, _ = p.GetSession("DataExportVerifyPhone_val").(string)
|
|
|
+ }
|
|
|
+ //先看有没有创建过预支付订单
|
|
|
+ oldOrder := public.Mysql.FindOne("dataexport_order", map[string]interface{}{
|
|
|
+ "user_openid": openId,
|
|
|
+ "filter_id": id,
|
|
|
+ }, "id,order_status,order_code,order_money,user_mail,user_phone,data_spec,prepay_time,pay_way", "")
|
|
|
+ if oldOrder != nil {
|
|
|
+ if user_mail != "" && user_mail != util.ObjToString((*oldOrder)["user_mail"]) {
|
|
|
+ public.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
+ "user_openid": openId,
|
|
|
+ "filter_id": id,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "user_mail": user_mail,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if user_phone != "" && user_phone != util.ObjToString((*oldOrder)["user_phone"]) {
|
|
|
+ public.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
+ "user_openid": openId,
|
|
|
+ "filter_id": id,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "user_phone": user_phone,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ //如果已支付,则返回
|
|
|
+ if util.IntAll((*oldOrder)["order_status"]) != 0 {
|
|
|
+ log.Println("该订单已存在,订单状态不允许支付!", openId)
|
|
|
+ p.ServeJson(map[string]interface{}{
|
|
|
+ "status": "n",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //价格支付方式变动需要重新生成订单二维码
|
|
|
+ if (data_spec != "" && data_spec != util.ObjToString((*oldOrder)["data_spec"])) || pay_way != util.ObjToString((*oldOrder)["pay_way"]) {
|
|
|
+ data_count := public.GetDataExportSearchCountUseId(id)
|
|
|
+ if data_count > public.ExConf.MsgMaxCount {
|
|
|
+ data_count = public.ExConf.MsgMaxCount
|
|
|
+ }
|
|
|
+ order_money_, original_price_ := float64(0), float64(0)
|
|
|
+ if data_spec == "标准字段包" {
|
|
|
+ original_price_ = float64(data_count) * public.ExConf.UnitPrice_normal
|
|
|
+ } else if data_spec == "高级字段包" {
|
|
|
+ original_price_ = float64(data_count) * public.ExConf.UnitPrice_senior
|
|
|
+ }
|
|
|
+ order_money_ = float64(original_price_) * public.ExConf.Discount
|
|
|
+ if order_money_ < public.ExConf.OrderMinPrice {
|
|
|
+ order_money_ = public.ExConf.OrderMinPrice
|
|
|
+ }
|
|
|
+ if original_price_ < public.ExConf.OrderMinPrice {
|
|
|
+ original_price_ = public.ExConf.OrderMinPrice
|
|
|
+ }
|
|
|
+ order_money := int(order_money_ * 100)
|
|
|
+ original_price := int(original_price_ * 100)
|
|
|
+ prepayTime, _ := time.ParseInLocation(util.Date_Full_Layout, util.ObjToString((*oldOrder)["prepay_time"]), time.Local)
|
|
|
+ surplus_second := prepayTime.Unix() - 7200 //价格变动 支付二维码更新成超时状态--重新生订单二维码
|
|
|
+ public.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
+ "user_openid": openId,
|
|
|
+ "filter_id": id,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "data_spec": data_spec,
|
|
|
+ "order_money": order_money,
|
|
|
+ "original_price": original_price,
|
|
|
+ "prepay_time": util.FormatDateByInt64(&surplus_second, util.Date_Full_Layout),
|
|
|
+ "pay_way": pay_way,
|
|
|
+ })
|
|
|
+ (*oldOrder)["order_money"] = order_money
|
|
|
+ }
|
|
|
+ p.ServeJson(map[string]interface{}{
|
|
|
+ "status": "y",
|
|
|
+ "code": util.ObjToString((*oldOrder)["order_code"]),
|
|
|
+ "token": public.GetWaitPayToken(util.Int64All((*oldOrder)["id"]), util.IntAll((*oldOrder)["order_money"]), util.ObjToString((*oldOrder)["order_code"]), pay_way, openId),
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //
|
|
|
+ data_count := public.GetDataExportSearchCountUseId(id)
|
|
|
+ if data_count > public.ExConf.MsgMaxCount {
|
|
|
+ data_count = public.ExConf.MsgMaxCount
|
|
|
+ }
|
|
|
+ order_money_, original_price_ := float64(0), float64(0)
|
|
|
+ if data_spec == "标准字段包" {
|
|
|
+ original_price_ = float64(data_count) * public.ExConf.UnitPrice_normal
|
|
|
+ } else if data_spec == "高级字段包" {
|
|
|
+ original_price_ = float64(data_count) * public.ExConf.UnitPrice_senior
|
|
|
+ }
|
|
|
+ order_money_ = float64(original_price_) * public.ExConf.Discount
|
|
|
+ if order_money_ < public.ExConf.OrderMinPrice {
|
|
|
+ order_money_ = public.ExConf.OrderMinPrice
|
|
|
+ }
|
|
|
+ if original_price_ < public.ExConf.OrderMinPrice {
|
|
|
+ original_price_ = public.ExConf.OrderMinPrice
|
|
|
+ }
|
|
|
+ order_money := int(order_money_ * 100)
|
|
|
+ original_price := int(original_price_ * 100)
|
|
|
+ //order_money = 1
|
|
|
+ //original_price = 1
|
|
|
+ filter_keys, filter_publishtime, filter := "", "", ""
|
|
|
+ //
|
|
|
+ isPass := func() bool {
|
|
|
+ if pay_way == "" || user_mail == "" || user_phone == "" || !user_mail_status || data_spec == "" || order_money <= 0 || data_count <= 0 || original_price <= 0 {
|
|
|
+ log.Println("参数不合法! pay_way, user_mail,user_phone, user_mail_status, data_spec, order_money, data_count, original_price", pay_way, user_mail, user_phone, user_mail_status, data_spec, order_money, data_count, original_price)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ userfilter, ok := public.MQFW.FindById("export_search", id, nil)
|
|
|
+ if ok && userfilter != nil {
|
|
|
+ keywords, _ := (*userfilter)["keywords"].([]interface{})
|
|
|
+ var buffer bytes.Buffer
|
|
|
+ for _, v := range keywords {
|
|
|
+ vm, _ := v.(map[string]interface{})
|
|
|
+ if buffer.Len() > 0 {
|
|
|
+ buffer.WriteString(",")
|
|
|
+ }
|
|
|
+ buffer.WriteString(util.ObjToString(vm["keyword"]))
|
|
|
+ }
|
|
|
+ filter_keys = buffer.String()
|
|
|
+ filter_publishtime = util.ObjToString((*userfilter)["publishtime"])
|
|
|
+ filterByte, _ := json.Marshal(userfilter)
|
|
|
+ filter = string(filterByte)
|
|
|
+ } else {
|
|
|
+ log.Println("id is not find in mongodb", openId, id)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }()
|
|
|
+ nickname, _ := p.GetSession("s_nickname").(string)
|
|
|
+ result := map[string]interface{}{
|
|
|
+ "status": "n",
|
|
|
+ }
|
|
|
+ if isPass {
|
|
|
+ //创建微信预支付订单
|
|
|
+ if pay_way == "微信" {
|
|
|
+ tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), "b", p.IP(), openId, "", order_money)
|
|
|
+ log.Println("支付交易单", openId, ret)
|
|
|
+ if ret != nil && (*ret)["status"] == "1" {
|
|
|
+ now := time.Now()
|
|
|
+ ordercode := public.GetOrderCode(openId)
|
|
|
+ orderid := public.Mysql.Insert("dataexport_order", map[string]interface{}{
|
|
|
+ "order_money": order_money,
|
|
|
+ "order_status": 0,
|
|
|
+ "service_status": 0,
|
|
|
+ "user_nickname": nickname,
|
|
|
+ "user_openid": openId,
|
|
|
+ "filter": filter,
|
|
|
+ "prepay_id": (*ret)["prepayid"],
|
|
|
+ "code_url": (*ret)["codeurl"],
|
|
|
+ "out_trade_no": tradeno,
|
|
|
+ "order_code": ordercode,
|
|
|
+ "product_type": "历史数据",
|
|
|
+ "create_time": util.FormatDate(&now, util.Date_Full_Layout),
|
|
|
+ "prepay_time": util.FormatDate(&now, util.Date_Full_Layout),
|
|
|
+ "original_price": original_price,
|
|
|
+ "data_spec": data_spec,
|
|
|
+ "user_mail": user_mail,
|
|
|
+ "user_phone": user_phone,
|
|
|
+ "data_count": data_count,
|
|
|
+ "filter_publishtime": filter_publishtime,
|
|
|
+ "filter_keys": filter_keys,
|
|
|
+ "filter_id": id,
|
|
|
+ "pay_way": pay_way,
|
|
|
+ })
|
|
|
+ if orderid > 0 {
|
|
|
+ p.SetSession("DataExportVerifyPhone_val", user_phone)
|
|
|
+ result["status"] = "y"
|
|
|
+ result["code"] = ordercode
|
|
|
+ result["token"] = public.GetWaitPayToken(orderid, order_money, ordercode, pay_way, openId)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.Println("生成微信预支付交易单失败", ret)
|
|
|
+ }
|
|
|
+ } else if pay_way == "支付宝" {
|
|
|
+ if qr_url, tradeno, err := Alipay.GetPayUrl(util.Float64All(order_money)); err == nil {
|
|
|
+ now := time.Now()
|
|
|
+ ordercode := public.GetOrderCode(openId)
|
|
|
+ orderid := public.Mysql.Insert("dataexport_order", map[string]interface{}{
|
|
|
+ "order_money": order_money,
|
|
|
+ "order_status": 0,
|
|
|
+ "service_status": 0,
|
|
|
+ "user_nickname": nickname,
|
|
|
+ "user_openid": openId,
|
|
|
+ "filter": filter,
|
|
|
+ "prepay_id": "",
|
|
|
+ "code_url": qr_url,
|
|
|
+ "out_trade_no": tradeno,
|
|
|
+ "order_code": ordercode,
|
|
|
+ "product_type": "历史数据",
|
|
|
+ "create_time": util.FormatDate(&now, util.Date_Full_Layout),
|
|
|
+ "prepay_time": util.FormatDate(&now, util.Date_Full_Layout),
|
|
|
+ "original_price": original_price,
|
|
|
+ "data_spec": data_spec,
|
|
|
+ "user_mail": user_mail,
|
|
|
+ "user_phone": user_phone,
|
|
|
+ "data_count": data_count,
|
|
|
+ "filter_publishtime": filter_publishtime,
|
|
|
+ "filter_keys": filter_keys,
|
|
|
+ "filter_id": id,
|
|
|
+ "pay_way": pay_way,
|
|
|
+ })
|
|
|
+ if orderid > 0 {
|
|
|
+ p.SetSession("DataExportVerifyPhone_val", user_phone)
|
|
|
+ result["status"] = "y"
|
|
|
+ result["code"] = ordercode
|
|
|
+ result["token"] = public.GetWaitPayToken(orderid, order_money, ordercode, pay_way, openId)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.Println("生成支付宝预支付交易单失败")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ p.ServeJson(result)
|
|
|
+}
|
|
|
+
|
|
|
+func (p *DataExportPayAction) IsPaySuccess() {
|
|
|
+ defer util.Catch()
|
|
|
+ code := p.GetString("code")
|
|
|
+ openid, _ := p.GetSession("s_m_openid").(string)
|
|
|
+ status := "n"
|
|
|
+ if code != "" || openid != "" {
|
|
|
+ data := public.Mysql.FindOne("dataexport_order", map[string]interface{}{
|
|
|
+ "order_code": code,
|
|
|
+ "user_openid": openid,
|
|
|
+ }, "order_status", "")
|
|
|
+ if data != nil && util.IntAll((*data)["order_status"]) == 1 {
|
|
|
+ status = "y"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ p.ServeJson(map[string]interface{}{
|
|
|
+ "status": status,
|
|
|
+ })
|
|
|
+}
|