140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
package services
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"qteam/app/constants/errorcode"
|
|
"qteam/app/http/entities/front"
|
|
"qteam/app/models/ordermodel"
|
|
dfbconfig "qteam/app/third/dfpOpenSdk/config"
|
|
"qteam/app/third/dfpOpenSdk/opensdk"
|
|
"qteam/app/utils"
|
|
"qteam/config"
|
|
"time"
|
|
)
|
|
|
|
func DecryptXyData(data string) (info front.XyDecryptData) {
|
|
// 先解Base64,再解Hex16进
|
|
decrypt, err := utils.SM2Decrypt(data)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal([]byte(decrypt), &info)
|
|
if err != nil {
|
|
fmt.Println("解析JSON错误:", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func CreateXyOrder(order ordermodel.Order) (code int, res front.XinYeOrderResponse) {
|
|
url := "/api/payment/retailCombinedCashier/payOrder"
|
|
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",
|
|
FrontEndUrl: config.GetConf().XinYe.FrontEndUrl + "?order_no=" + order.OrderNo,
|
|
OrderRemark: "测试",
|
|
}
|
|
|
|
// 配置应用配置,可配置多个应用
|
|
configure := dfbconfig.KeyConfigure{
|
|
KeyId: config.GetConf().XinYeBank.KEYID,
|
|
PriKey: config.GetConf().XinYeBank.PriKey,
|
|
RespPubKey: config.GetConf().XinYeBank.RespPubKey,
|
|
RespSignAlgorithm: "sm4",
|
|
ReqParamEncryptKey: "",
|
|
}
|
|
|
|
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,
|
|
}
|
|
|
|
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)
|
|
return errorcode.SystemError, res
|
|
}
|
|
//if err = json.Unmarshal(data, &res); err != nil {
|
|
// return errorcode.SystemError, res
|
|
//}
|
|
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客户端
|
|
client := &http.Client{
|
|
Timeout: 3 * time.Second, // 设置请求超时时间为5秒
|
|
}
|
|
|
|
// 发送请求并处理响应
|
|
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
|
|
}
|