新增 eggplant.pem 公钥文件和 eggplant_test.go 测试文件;在 eggplant.go 中优化了订单创建逻辑,调整了请求超时设置,并增加了 RSA 加密功能以增强数据安全性。

This commit is contained in:
danial
2025-07-09 15:35:40 +08:00
parent ae757c62e8
commit 7cf74dfb9d
3 changed files with 98 additions and 52 deletions

9
data/pem/eggplant.pem Normal file
View File

@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlNhFxwwma6deBnkWtI8M
jLiy5ytvvA8sq8X6Hb7feQARFMbQoYGLfJ2jrGqmz6oCq7o5FpfrlnUHrTJc+cv5
PTHHFnJ8qX9cCYLm70dQA/gZgt4ogWkMlVxFC8zUbgbd70ZwOMdDHl0mwM1CfU1+
sbdAxf6Ad/pUm4E0zvzCuFf8wTZq4MBg32bSrMpsa7SibTxf92VGra6tTi3EwJDT
QQM/CnE9OK22vMQ2MDMTJPSuFm261Du55ylujQLRei19co+/oDtsytNr/5VYRi5o
qxZIamD1De5cJTcaXtioz3dXDELJ9PKuf+0AqqSyHvGCsobxI/8QUxAH9XKrx7RS
3QIDAQAB
-----END PUBLIC KEY-----

View File

@@ -2,6 +2,7 @@ package card_sender
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"gateway/internal/config"
@@ -20,6 +21,7 @@ import (
"github.com/beego/beego/v2/client/httplib"
"github.com/duke-git/lancet/v2/convertor"
"github.com/duke-git/lancet/v2/cryptor"
"github.com/duke-git/lancet/v2/strutil"
"github.com/go-resty/resty/v2"
"github.com/widuu/gojson"
@@ -66,9 +68,9 @@ func (s *SendCardTaskTypeEggplant) CreateOrder(ctx context.Context, roadUid stri
params["sign"] = s.GenerateSign(ctx, params, gojson.Json(roadInfo.Params).Get("key").Tostring())
req := httplib.NewBeegoRequestWithCtx(ctx, "https://admin.djfkm.xyz/api/order/create", "POST").
req := httplib.NewBeegoRequestWithCtx(ctx, "https://admin.kmsfkm.xyz/api/order/create", "POST").
SetTransport(otelhttp.NewTransport(http.DefaultTransport)).
SetTimeout(time.Second*30, time.Second*30).Retries(3)
SetTimeout(time.Second*5, time.Second*5).Retries(3).RetryDelay(time.Second)
for k, v := range params {
req.Param(k, convertor.ToString(v))
@@ -136,25 +138,24 @@ func (s *SendCardTaskTypeEggplant) GenerateSign(ctx context.Context, params map[
type eggplantProductCode struct {
ProductCode string `json:"productCode"`
ForwoardUrl string `json:"forwoardUrl"`
CardNo string `json:"cardNo"`
CardPassword string `json:"cardPassword"`
}
func (c *eggplantProductCode) SendData(ctx context.Context, orderId string, forwardUrl *url.URL) (bool, string, string) {
// 通道1
chnnalOneCodeArray := []string{"8055", "8076", "8055"}
if slices.Contains(chnnalOneCodeArray, strutil.Trim(c.ProductCode)) {
channelOneCodeArray := []string{"8055", "8076", "8055"}
if slices.Contains(channelOneCodeArray, strutil.Trim(c.ProductCode)) {
return c.channelOne(ctx, forwardUrl)
}
// 通道2
chnnalTwoCodeArray := []string{"8069"}
if slices.Contains(chnnalTwoCodeArray, strutil.Trim(c.ProductCode)) {
channelTwoCodeArray := []string{"8069"}
if slices.Contains(channelTwoCodeArray, strutil.Trim(c.ProductCode)) {
return c.channelTwo(ctx, forwardUrl)
}
// 通道3
chnnalThreeCodeArray := []string{"8052", "8053"}
if slices.Contains(chnnalThreeCodeArray, strutil.Trim(c.ProductCode)) {
channelThreeCodeArray := []string{"8052", "8053"}
if slices.Contains(channelThreeCodeArray, strutil.Trim(c.ProductCode)) {
return c.channelThree(ctx, forwardUrl)
}
return false, "通道不存在", ""
@@ -162,61 +163,69 @@ func (c *eggplantProductCode) SendData(ctx context.Context, orderId string, forw
// 第一个通道
func (c *eggplantProductCode) channelOne(ctx context.Context, forwardUrl *url.URL) (bool, string, string) {
req := httplib.NewBeegoRequestWithCtx(ctx, "https://api.djfkm.xyz/api/order/getOrderById", "GET").SetTransport(otelhttp.NewTransport(http.DefaultTransport)).
SetTimeout(time.Second*30, time.Second*30).Retries(3)
// req := httplib.NewBeegoRequestWithCtx(ctx, "https://api.djfkm.xyz/api/order/getOrderById", "GET").SetTransport(otelhttp.NewTransport(http.DefaultTransport)).
// SetTimeout(time.Second*30, time.Second*30).Retries(3)
req.Param("id", forwardUrl.Query().Get("orderId"))
response, err := req.String()
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("获取订单接口失败", zap.Error(err))
return false, "内部数据处理失败", ""
}
// req.Param("id", forwardUrl.Query().Get("orderId"))
// response, err := req.String()
// if err != nil {
// otelTrace.Logger.WithContext(ctx).Error("获取订单接口失败", zap.Error(err))
// return false, "内部数据处理失败", ""
// }
var orderGetOrderByIdResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data struct {
ID int `json:"id"`
OrderNo string `json:"orderNo"`
TraceNo string `json:"traceNo"`
ChannelCode string `json:"channelCode"`
ChannelName string `json:"channelName"`
Amount float64 `json:"amount"`
OrderTime string `json:"orderTime"`
WarnNotes string `json:"warnNotes"`
ShopPayTimeout string `json:"shopPayTimeout"`
OrderStatus string `json:"orderStatus"`
JumpUrl int `json:"jumpUrl"`
} `json:"data"`
}
err = json.Unmarshal([]byte(response), &orderGetOrderByIdResp)
otelTrace.Logger.WithContext(ctx).Info("获取订单接口", zap.Any("response", response), zap.Any("解析结果", orderGetOrderByIdResp))
if orderGetOrderByIdResp.Code != 200 {
return false, orderGetOrderByIdResp.Msg, ""
}
// var orderGetOrderByIdResp struct {
// Code int `json:"code"`
// Msg string `json:"msg"`
// Data struct {
// ID int `json:"id"`
// OrderNo string `json:"orderNo"`
// TraceNo string `json:"traceNo"`
// ChannelCode string `json:"channelCode"`
// ChannelName string `json:"channelName"`
// Amount float64 `json:"amount"`
// OrderTime string `json:"orderTime"`
// WarnNotes string `json:"warnNotes"`
// ShopPayTimeout string `json:"shopPayTimeout"`
// OrderStatus string `json:"orderStatus"`
// JumpUrl int `json:"jumpUrl"`
// } `json:"data"`
// }
// _ = json.Unmarshal([]byte(response), &orderGetOrderByIdResp)
// otelTrace.Logger.WithContext(ctx).Info("获取订单接口", zap.Any("response", response), zap.Any("解析结果", orderGetOrderByIdResp))
// if orderGetOrderByIdResp.Code != 200 {
// return false, orderGetOrderByIdResp.Msg, ""
// }
req = httplib.NewBeegoRequestWithCtx(ctx, "https://api.djfkm.xyz/api/order/shopSubCard", "POST").
req := httplib.NewBeegoRequestWithCtx(ctx, "https://api.djfkm.xyz/api/order/shopSubCard", "POST").
SetTransport(otelhttp.NewTransport(http.DefaultTransport)).
SetTimeout(time.Second*30, time.Second*30)
SetTimeout(time.Second*5, time.Second*5).Retries(3).RetryDelay(3)
jsonBody := struct {
OrderId string `json:"orderId"`
TraceNo string `json:"traceNo"`
Amount int `json:"amount"`
ChannelName string `json:"channelName"`
ChannelCode string `json:"channelCode"`
OrderId string `json:"orderId"`
// TraceNo string `json:"traceNo"`
// Amount int `json:"amount"`
// ChannelName string `json:"channelName"`
// ChannelCode string `json:"channelCode"`
CardNo string `json:"cardNo"`
CardPassword string `json:"cardPassword"`
Pass string `json:"pass,omitempty"`
}{
OrderId: forwardUrl.Query().Get("orderId"),
TraceNo: orderGetOrderByIdResp.Data.TraceNo,
Amount: int(orderGetOrderByIdResp.Data.Amount),
ChannelName: orderGetOrderByIdResp.Data.ChannelName,
ChannelCode: orderGetOrderByIdResp.Data.ChannelCode,
OrderId: forwardUrl.Query().Get("orderId"),
// TraceNo: orderGetOrderByIdResp.Data.TraceNo,
// Amount: int(orderGetOrderByIdResp.Data.Amount),
// ChannelName: orderGetOrderByIdResp.Data.ChannelName,
// ChannelCode: orderGetOrderByIdResp.Data.ChannelCode,
CardNo: c.CardNo,
CardPassword: c.CardPassword,
}
jsonBo, err := json.Marshal(jsonBody)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("json序列化失败", zap.Error(err))
}
encrypted := cryptor.RsaEncrypt(jsonBo, "./data/pem/eggplant.pem")
jsonBody.Pass = base64.StdEncoding.EncodeToString(encrypted)
req, err = req.JSONBody(jsonBody)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("json序列化失败", zap.Error(err))
@@ -347,7 +356,7 @@ func (c *eggplantProductCode) channelThree(ctx context.Context, forwardUrl *url.
return false, "未知错误,请联系技术人员排查", ""
}
func (c *SendCardTaskTypeEggplant) QueryOrder(ctx context.Context, orderItem OrderPoolItem, task SendCardTask) error {
func (s *SendCardTaskTypeEggplant) QueryOrder(ctx context.Context, orderItem OrderPoolItem, task SendCardTask) error {
queryCardResp, err := client.NewHeePayClient().ToString(ctx, &client.QueryCardInput{
CardNo: task.CardInfo.CardNo,
CardPassword: task.CardInfo.Data,

View File

@@ -0,0 +1,28 @@
package card_sender
import (
"context"
"net/url"
"testing"
)
func TestSendCardTaskTypeEggplant_CreateOrder(t *testing.T) {
service := &SendCardTaskTypeEggplant{}
order, err := service.CreateOrder(context.Background(), "1234567890", 100)
if err != nil {
t.Errorf("CreateOrder() error = %v", err)
}
t.Logf("CreateOrder() got = %v", order)
productCode := &eggplantProductCode{
ProductCode: "8055",
CardNo: "1234567",
CardPassword: "234567",
}
forwardUrl, err := url.Parse(order.PayURL)
if err != nil {
t.Errorf("Parse() error = %v", err)
}
ok, str, _ := productCode.channelOne(context.Background(), forwardUrl)
t.Logf("channelOne() got = %v", ok)
t.Logf("channelOne() got = %v", str)
}