118 lines
3.0 KiB
Go
118 lines
3.0 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"
|
|
"qteam/config"
|
|
"time"
|
|
)
|
|
|
|
func DecryptXyData(data string) (info front.XyDecryptData) {
|
|
// 先解Base64,再解Hex16进
|
|
decrypt, err := 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/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
|
|
}
|