Files
kami_gateway/internal/controllers/gateway/scan_controller.go
2024-09-03 21:52:44 +08:00

298 lines
8.8 KiB
Go

package gateway
import (
"context"
"errors"
"fmt"
"gateway/internal/config"
"gateway/internal/consts"
"gateway/internal/entities/supplier/t_mall_game"
"gateway/internal/entities/supplier/third_party"
"gateway/internal/models/merchant"
"gateway/internal/models/merchant_deploy"
"gateway/internal/models/order"
"gateway/internal/models/road"
request2 "gateway/internal/schema/request"
response2 "gateway/internal/schema/response"
"gateway/internal/service"
"gateway/internal/utils"
"strconv"
"strings"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/core/validation"
)
type ScanController struct {
BaseGateway
}
// Scan 处理扫码的请求
func (c *ScanController) Scan() {
// 获取所有请求参数
p := c.PayPrepare()
if p.Code == -1 {
c.SolveFailJSON(p)
return
}
// 签名验证
paySecret := p.MerchantInfo.MerchantSecret
if !utils.Md5MFVerify(p.Params, paySecret) {
p.Code = -1
p.Msg = "签名异常"
c.SolveFailJSON(p)
return
}
p = service.ChooseRoadV2(p)
if p.Code == -1 {
c.SolveFailJSON(p)
return
}
mt := merchant_deploy.GetMerchantDeployByUidAndRoadUid(p.MerchantInfo.MerchantUid, p.RoadInfo.RoadUid)
if mt.Id == 0 {
p.Msg = "当前用户没有开通该通道"
c.SolveFailJSON(p)
return
}
orderPrice, err := strconv.ParseFloat(p.Params["orderPrice"], 64)
if err != nil {
p.Code = -1
p.Msg = fmt.Sprintf("订单金额转换失败:%v", err.Error())
c.SolveFailJSON(p)
return
}
pm, err := mt.GetShowMMValue(orderPrice)
if err != nil {
p.Code = -1
p.Msg = fmt.Sprintf("获取展示比例失败:%v", err.Error())
c.SolveFailJSON(p)
return
}
if p.RoadInfo.TransactionType == string(consts.TransactionTypeRedeem) {
p.Params["exValue"], err = service.CompleteRedeemExValue(p.Params["exValue"], strconv.FormatFloat(pm.ShowLabel, 'f', 0, 64))
} else {
p.Params["exValue"], err = service.CompleteRechargeExValue(p.Params["exValue"], strconv.FormatFloat(pm.ShowLabel, 'f', 0, 64))
}
if err != nil {
p.Code = -1
p.Msg = fmt.Sprintf("订单金额转换失败:%v", err.Error())
c.SolveFailJSON(p)
return
}
// 生成订单记录
orderInfo, _, err := service.GenerateRecord(p)
if err != nil {
p.Msg = fmt.Sprintf("生成订单失败:%v", err.Error())
c.SolveFailJSON(p)
return
}
if p.Code == -1 {
c.SolveFailJSON(p)
return
}
if mt.AutoSettle == config.NO {
params := make(map[string]string)
params["statusCode"] = "00"
params["orderNo"] = orderInfo.BankOrderId
params["orderPrice"] = strconv.FormatFloat(orderInfo.OrderAmount, 'f', 2, 64)
params["statusCode"] = "00"
sign := utils.GetMD5SignMF(params, p.MerchantInfo.MerchantSecret)
c.Data["json"] = response2.ScanSuccessData{
OrderNo: orderInfo.BankOrderId,
OrderPrice: strconv.FormatFloat(orderInfo.OrderAmount, 'f', 2, 64),
StatusCode: "00",
Sign: sign,
Msg: "请求成功,请等待兑换!",
Code: 0,
}
_ = c.ServeJSON()
return
}
// 获取到对应的上游
supplierCode := p.RoadInfo.ProductUid
supplier := third_party.GetPaySupplierByCode(supplierCode)
if supplier == nil {
// 插入处理失败的动账通知
service.SolvePayFail(orderInfo.BankOrderId, "")
logs.Error("获取上游渠道失败,请联系客服", supplierCode)
err = errors.New("获取上游渠道失败,请联系客服")
c.Data["json"] = response2.CommonErr(-1, err.Error())
_ = c.ServeJSON()
c.StopRun()
return
}
scanData := supplier.Scan(orderInfo, p.RoadInfo, p.MerchantInfo)
order.InsertCardReturnData(scanData.BankNo, scanData.ReturnData)
if scanData.Status == "00" {
scanSuccessData := service.GenerateSuccessData(scanData, p)
c.Data["json"] = scanSuccessData
_ = c.ServeJSON()
return
} else {
// 插入处理失败的动账通知
service.SolvePayFail(orderInfo.BankOrderId, "")
p.Msg = scanData.Msg
p.Code = -1
c.SolveFailJSON(p)
return
}
}
// SolveFailJSON 处理错误的返回
func (c *ScanController) SolveFailJSON(p *response2.PayBaseResp) {
scanFailJSON := new(response2.ScanFailData)
scanFailJSON.StatusCode = "01"
scanFailJSON.PayKey = p.Params["payKey"]
scanFailJSON.Msg = p.Msg
scanFailJSON.Code = -1
c.Data["json"] = scanFailJSON
_ = c.ServeJSON()
c.StopRun()
}
func (c *ScanController) GetAllowedMM() {
payKey := strings.TrimSpace(c.GetString("payKey"))
showMMValue, err := c.GetFloat("showMMValue")
productCode := strings.TrimSpace(c.GetString("productCode"))
if payKey == "" || showMMValue == 0 || productCode == "" {
res := response2.CommonErr(-1, "获取面额失败,参数缺失")
c.Data["json"] = res
_ = c.ServeJSON()
return
}
if err != nil {
c.Data["json"] = response2.CommonErr(-1, err.Error())
_ = c.ServeJSON()
c.StopRun()
}
merchantInfo, err := service.GetMerchantInfoByPayKey(payKey)
if err != nil || merchantInfo.Id == 0 {
c.Data["json"] = response2.CommonErr(-1, "获取面额失败,获取商户信息出错")
_ = c.ServeJSON()
c.StopRun()
}
merchantDeployInfo := service.GerMerchantDeployInfoByUidAndProductCode(merchantInfo.MerchantUid, productCode)
if merchantDeployInfo.Id == 0 {
res := response2.CommonErr(-1, "获取面额失败,当前通道不存在")
c.Data["json"] = res
_ = c.ServeJSON()
return
}
profitMarginList, err := merchantDeployInfo.GetFactMMValue(showMMValue)
if err != nil {
c.Data["json"] = response2.CommonErr(-1, err.Error())
_ = c.ServeJSON()
c.StopRun()
}
type profitMarginStruct struct {
Sort int `json:"sort" description:"排序"`
FactLabel float64 `json:"factLabel" description:"实际面值"`
ShowLabel float64 `json:"showLabel" description:"展示面额"`
PlatformLabel string `json:"platformLabel" description:"平台"`
IsLinkSingle bool `json:"isLinkSingle" description:"链接是否单独放置"`
LinkID string `json:"linkID" description:"链接"`
}
logs.Info("当前请求面额数据:%+v", profitMarginList)
resData := make([]profitMarginStruct, 0)
for _, v := range profitMarginList {
if v.ShowLabel != 0 || v.FactLabel != 0 {
resData = append(resData, profitMarginStruct{
FactLabel: v.FactLabel,
ShowLabel: v.ShowLabel,
PlatformLabel: v.PlatformLabel,
IsLinkSingle: v.IsLinkSingle,
LinkID: v.LinkID,
Sort: v.Sort,
})
}
}
logs.Info("转换后的面额数据:%+v", resData)
c.Data["json"] = response2.Ok(resData)
_ = c.ServeJSON()
}
// CreateOrder 创建订单
func (c *ScanController) CreateOrder() {
createdOrder := request2.CreatedOrder{}
_ = c.BindJSON(&createdOrder)
valid := validation.Validation{}
b, err := valid.Valid(&createdOrder)
if err != nil {
logs.Error("创建订单错误:", err)
res := response2.CommonErr(-1, "创建订单错误,参数错误")
c.Data["json"] = res
_ = c.ServeJSON()
return
}
if !b {
logs.Error("创建订单错误:", valid.Errors)
res := response2.CommonErr(-1, "创建订单错误,参数验证错误")
c.Data["json"] = res
_ = c.ServeJSON()
return
}
merchantInfo := merchant.GetMerchantByPasskey(createdOrder.PayKey)
if merchantInfo.Id == 0 {
logs.Error("创建订单错误:", err)
res := response2.CommonErr(-1, "创建订单错误")
c.Data["json"] = res
_ = c.ServeJSON()
return
}
if !utils.Md5MFVerify(createdOrder.ToMap(), merchantInfo.MerchantSecret) {
res := response2.CommonErr(-1, "sign验证错误")
logs.Error("sign验证错误")
c.Data["json"] = res
_ = c.ServeJSON()
return
}
orderInfo := order.GetOrderByMerchantOrderId(createdOrder.OrderNo)
roadInfo := road.GetRoadInfoByProductCode(createdOrder.ProductCode)
if orderInfo.Id != 0 {
res := response2.Ok(struct {
ProductCode string `json:"productCode"`
PaymentName string `json:"paymentName"`
TransactionType string `json:"TransactionType"`
}{
ProductCode: createdOrder.ProductCode,
PaymentName: roadInfo.PaymentHtml,
TransactionType: roadInfo.TransactionType,
})
c.Data["json"] = res
_ = c.ServeJSON()
return
}
roadInfo, err = service.CreateOrderInfoAndOrderProfitInfo(createdOrder, merchantInfo)
if err != nil {
res := response2.CommonErr(-1, err.Error())
logs.Info("创建订单错误:", err)
c.Data["json"] = res
_ = c.ServeJSON()
return
}
res := response2.Ok(struct {
ProductCode string `json:"productCode"`
PaymentName string `json:"paymentName"`
TransactionType string `json:"TransactionType"`
}{
ProductCode: createdOrder.ProductCode,
PaymentName: roadInfo.PaymentHtml,
TransactionType: roadInfo.TransactionType,
})
c.Data["json"] = res
_ = c.ServeJSON()
}
func (c *ScanController) QueryAccountInfo() {
accountInfo := request2.ThirdPartyAccountInfo{}
channelName := c.Ctx.Input.Param("channel")
if channelName == "TMallGame" {
_ = c.BindJSON(&accountInfo)
err := t_mall_game.QueryTMallGameAccountInfo(context.Background(), request2.ThirdPartyAccountInfo{})
_ = err
}
return
}