2024-06-11 18:47:02 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-06-13 18:45:49 +08:00
|
|
|
"bytes"
|
2024-06-11 18:47:02 +08:00
|
|
|
"encoding/json"
|
2024-06-13 18:45:49 +08:00
|
|
|
"errors"
|
2024-06-11 18:47:02 +08:00
|
|
|
"fmt"
|
2024-06-13 18:45:49 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"qteam/app/constants/errorcode"
|
2024-06-11 18:47:02 +08:00
|
|
|
"qteam/app/http/entities/front"
|
2024-06-13 18:45:49 +08:00
|
|
|
"qteam/app/models/ordermodel"
|
2024-07-08 18:01:02 +08:00
|
|
|
dfbconfig "qteam/app/third/dfpOpenSdk/config"
|
|
|
|
"qteam/app/third/dfpOpenSdk/opensdk"
|
|
|
|
"qteam/app/utils"
|
2024-06-13 18:45:49 +08:00
|
|
|
"qteam/config"
|
|
|
|
"time"
|
2024-06-11 18:47:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func DecryptXyData(data string) (info front.XyDecryptData) {
|
2024-06-12 19:11:00 +08:00
|
|
|
// 先解Base64,再解Hex16进
|
2024-07-08 18:01:02 +08:00
|
|
|
decrypt, err := utils.SM2Decrypt(data)
|
2024-06-11 18:47:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(decrypt), &info)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("解析JSON错误:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-06-13 18:45:49 +08:00
|
|
|
|
|
|
|
func CreateXyOrder(order ordermodel.Order) (code int, res front.XinYeOrderResponse) {
|
2024-07-08 18:01:02 +08:00
|
|
|
url := "/api/payment/retailCombinedCashier/payOrder"
|
2024-06-13 18:45:49 +08:00
|
|
|
request := front.XinYeOrderRequest{
|
|
|
|
SAppId: config.GetConf().XinYe.SAppId,
|
|
|
|
TxnType: config.GetConf().XinYe.TxnType,
|
|
|
|
ProdId: config.GetConf().XinYe.ProdId,
|
|
|
|
AccessType: config.GetConf().XinYe.AccessType,
|
|
|
|
MchtId: config.GetConf().XinYe.MchtId,
|
|
|
|
MchtUserId: order.XinYeUserId,
|
|
|
|
MchtOrderDateTime: time.Now().Format("20060102150405"),
|
|
|
|
MchtOrderAmt: order.Price,
|
|
|
|
MchtTxnAmt: order.Price,
|
|
|
|
MainPayType: config.GetConf().XinYe.MainPayType,
|
|
|
|
CcyType: "156",
|
|
|
|
BuRealFlag: "0",
|
2024-07-08 18:01:02 +08:00
|
|
|
FrontEndUrl: config.GetConf().XinYe.FrontEndUrl + "?order_no=" + order.OrderNo,
|
2024-06-13 18:45:49 +08:00
|
|
|
OrderRemark: "测试",
|
|
|
|
}
|
2024-07-08 18:01:02 +08:00
|
|
|
|
|
|
|
// 配置应用配置,可配置多个应用
|
|
|
|
configure := dfbconfig.KeyConfigure{
|
|
|
|
KeyId: config.GetConf().XinYeBank.KEYID,
|
|
|
|
PriKey: config.GetConf().XinYeBank.PriKey,
|
|
|
|
RespPubKey: config.GetConf().XinYeBank.RespPubKey,
|
|
|
|
RespSignAlgorithm: "sm4",
|
|
|
|
ReqParamEncryptKey: "",
|
2024-06-13 18:45:49 +08:00
|
|
|
}
|
2024-07-08 18:01:02 +08:00
|
|
|
|
|
|
|
m := make(map[string]*dfbconfig.KeyConfigure)
|
|
|
|
m[config.GetConf().XinYeBank.KEYID] = &configure
|
|
|
|
sdkConfigure := dfbconfig.OpenSdkConfigure{
|
|
|
|
DevEnv: true,
|
|
|
|
DevUrl: "https://open.test.cibfintech.com",
|
|
|
|
KeyConfigures: m,
|
2024-06-13 18:45:49 +08:00
|
|
|
}
|
2024-07-08 18:01:02 +08:00
|
|
|
|
|
|
|
sdk := opensdk.OpenSDK{Configure: sdkConfigure, KeyConfigure: configure}
|
|
|
|
bodyParam := request.ToMap()
|
|
|
|
id, err := sdk.GateWayWithKeyId(url, http.MethodPost, nil, nil, bodyParam, config.GetConf().XinYeBank.KEYID)
|
|
|
|
if err != nil {
|
|
|
|
utils.Log(nil, "CreateOrderService", id)
|
2024-06-13 18:45:49 +08:00
|
|
|
return errorcode.SystemError, res
|
|
|
|
}
|
2024-07-08 18:01:02 +08:00
|
|
|
//if err = json.Unmarshal(data, &res); err != nil {
|
|
|
|
// return errorcode.SystemError, res
|
|
|
|
//}
|
2024-06-13 18:45:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func OrderXyQuery(orderNo string) (code int, res front.XinYeOrderQueryResponse) {
|
|
|
|
url := "/api/payment/retailCombinedCashier/paymentOrderQuery"
|
|
|
|
request := front.XinYeOrderQueryRequest{
|
|
|
|
SAppId: config.GetConf().XinYe.SAppId,
|
|
|
|
TxnType: config.GetConf().XinYe.TxnType,
|
|
|
|
ProdId: config.GetConf().XinYe.ProdId,
|
|
|
|
AccessType: config.GetConf().XinYe.AccessType,
|
|
|
|
MchtId: config.GetConf().XinYe.MchtId,
|
|
|
|
OriMchtOrderId: orderNo,
|
|
|
|
}
|
|
|
|
bytes, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, res
|
|
|
|
}
|
|
|
|
data, err := doPost(url, bytes)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, res
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(data, &res); err != nil {
|
|
|
|
return errorcode.SystemError, res
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func doPost(url string, jsonBytes []byte) (body []byte, err error) {
|
|
|
|
// 创建POST请求
|
|
|
|
url = config.GetConf().XinYe.Host + url
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置Content-Type头
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// 创建HTTP客户端
|
2024-07-08 18:01:02 +08:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 3 * time.Second, // 设置请求超时时间为5秒
|
|
|
|
}
|
2024-06-13 18:45:49 +08:00
|
|
|
|
|
|
|
// 发送请求并处理响应
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// 读取响应体
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err = errors.New("HTTP request failed: " + resp.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|