XinYeYouKu/app/services/XinYeService.go

118 lines
3.0 KiB
Go
Raw Normal View History

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"
"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进
decrypt, err := 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) {
url := "/api/payment/retailCombinedCashier/paymentOrderQuery"
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.OrderNo,
OrderRemark: "测试",
}
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 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{}
// 发送请求并处理响应
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
}