2024-06-19 18:32:34 +08:00
|
|
|
package youchu
|
|
|
|
|
|
|
|
import (
|
2024-08-21 14:30:20 +08:00
|
|
|
"encoding/base64"
|
2024-06-19 18:32:34 +08:00
|
|
|
"encoding/json"
|
2024-07-31 14:17:48 +08:00
|
|
|
"fmt"
|
2024-06-19 18:32:34 +08:00
|
|
|
"qteam/app/constants/errorcode"
|
|
|
|
"qteam/app/http/entities/front"
|
|
|
|
"qteam/app/models/ordersmodel"
|
|
|
|
"qteam/app/utils"
|
|
|
|
"qteam/app/utils/postbank"
|
|
|
|
"qteam/config"
|
2024-08-29 22:21:12 +08:00
|
|
|
"strings"
|
2024-06-19 18:32:34 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewYouChuClient(cfg config.YouChuConfig) *YouChuClient {
|
|
|
|
return &YouChuClient{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 11:44:38 +08:00
|
|
|
func (this *YouChuClient) Login(code string) (Code int, response front.YouChuLoginResponse) {
|
2024-06-25 10:12:48 +08:00
|
|
|
partnerTxSriNo := time.Now().Format("20060102150405") + utils.RandomNumber(10)
|
|
|
|
url := this.cfg.LoginHost + this.cfg.MerchantId + ".htm?partnerTxSriNo=" + partnerTxSriNo
|
2024-06-24 11:44:38 +08:00
|
|
|
request := front.YouChuLoginRequest{
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: partnerTxSriNo,
|
|
|
|
AccessType: "API",
|
|
|
|
Reserve: "",
|
2024-06-25 10:12:48 +08:00
|
|
|
ReqTime: time.Now().Format("20060102150405"),
|
2024-06-24 11:44:38 +08:00
|
|
|
Method: "crecard.getCustomerInfo",
|
|
|
|
Version: "1",
|
|
|
|
MerchantId: config.GetConf().YouChu.MerchantId,
|
|
|
|
AppID: config.GetConf().YouChu.AppID,
|
|
|
|
},
|
|
|
|
Body: front.YouChuLoginBody{
|
|
|
|
Code: code,
|
|
|
|
CustNo: "000000",
|
|
|
|
ReqTransTime: time.Now().Format("20060102150405"),
|
|
|
|
},
|
2024-06-19 18:32:34 +08:00
|
|
|
}
|
2024-07-09 11:02:07 +08:00
|
|
|
|
2024-06-24 11:44:38 +08:00
|
|
|
requestData := EncryptRequest(request)
|
|
|
|
bytes, err := json.Marshal(requestData)
|
2024-06-25 13:51:22 +08:00
|
|
|
utils.Log(nil, "YouChuLoginRequest", string(bytes))
|
2024-06-19 18:32:34 +08:00
|
|
|
if err != nil {
|
2024-06-24 11:44:38 +08:00
|
|
|
return errorcode.SystemError, response
|
2024-06-19 18:32:34 +08:00
|
|
|
}
|
2024-06-24 11:44:38 +08:00
|
|
|
post, err := this.doPost(url, partnerTxSriNo, "crecard.getCustomerInfo", bytes)
|
2024-07-10 15:39:18 +08:00
|
|
|
var rq, _ = json.Marshal(request)
|
|
|
|
utils.Log(nil, "YouChuLogin", string(post), string(rq), err)
|
2024-06-19 18:32:34 +08:00
|
|
|
if err != nil {
|
2024-06-24 11:44:38 +08:00
|
|
|
return errorcode.SystemError, response
|
2024-06-19 18:32:34 +08:00
|
|
|
}
|
2024-06-25 10:12:48 +08:00
|
|
|
responseData := DecryptResponse(string(post), false)
|
2024-06-24 11:44:38 +08:00
|
|
|
utils.Log(nil, "YouChuLogin", responseData)
|
|
|
|
err = json.Unmarshal([]byte(responseData), &response)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, response
|
|
|
|
}
|
|
|
|
return errorcode.Success, response
|
2024-06-19 18:32:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *YouChuClient) OrderQuery(order ordersmodel.Orders) (code int, response front.YouChuOrderQueryResponse) {
|
2024-06-25 10:12:48 +08:00
|
|
|
var BusiMainId = time.Now().Format("20060102150405") + utils.RandomNumber(10)
|
2024-06-19 18:32:34 +08:00
|
|
|
request := front.YouChuRequestBody{
|
|
|
|
Body: front.YouChuOrderRequest{
|
|
|
|
BusiMainId: BusiMainId,
|
2024-06-25 18:31:16 +08:00
|
|
|
ReqTransTime: order.CreateTime.Format("20060102150405"),
|
2024-06-19 18:32:34 +08:00
|
|
|
Data: front.OrderQuery{
|
|
|
|
TxnCode: "1004",
|
|
|
|
SourceId: "16",
|
2024-06-27 11:01:49 +08:00
|
|
|
ReqTraceId: utils.GenerateRequestNumber(),
|
2024-06-19 18:32:34 +08:00
|
|
|
ReqDate: time.Now().Format("20060102150405"),
|
|
|
|
TxnDt: time.Now().Format("20060102"),
|
|
|
|
MchtNo: config.GetConf().YouChu.MchtNo,
|
2024-06-27 11:01:49 +08:00
|
|
|
OldSeqNo: order.OrderNo,
|
2024-06-19 18:32:34 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: BusiMainId,
|
2024-06-25 10:12:48 +08:00
|
|
|
ReqTime: time.Now().Format("20060102150405"),
|
2024-06-19 18:32:34 +08:00
|
|
|
AccessType: "API",
|
|
|
|
Reserve: "",
|
|
|
|
Method: "b2c.gatewaypay.orderQuery",
|
|
|
|
Version: "1",
|
|
|
|
MerchantId: config.GetConf().YouChu.MerchantId,
|
|
|
|
AppID: config.GetConf().YouChu.AppID,
|
|
|
|
},
|
|
|
|
}
|
2024-06-27 11:01:49 +08:00
|
|
|
str, _ := json.Marshal(request.Body)
|
|
|
|
utils.Log(nil, "OrderQuery", string(str))
|
2024-06-25 18:31:16 +08:00
|
|
|
url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Body.BusiMainId
|
2024-06-19 18:32:34 +08:00
|
|
|
requestData := EncryptRequest(request)
|
|
|
|
bytes, err := json.Marshal(requestData)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, response
|
|
|
|
}
|
|
|
|
post, err := this.doPost(url, request.Body.BusiMainId, "b2c.gatewaypay.orderQuery", bytes)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, response
|
|
|
|
}
|
2024-06-25 10:12:48 +08:00
|
|
|
responseData := DecryptResponse(string(post), false)
|
|
|
|
utils.Log(nil, "OrderQuery", responseData)
|
2024-06-24 11:44:38 +08:00
|
|
|
err = json.Unmarshal([]byte(responseData), &response)
|
2024-06-19 18:32:34 +08:00
|
|
|
if err != nil {
|
2024-06-24 11:44:38 +08:00
|
|
|
return errorcode.SystemError, response
|
2024-06-19 18:32:34 +08:00
|
|
|
}
|
|
|
|
return errorcode.Success, response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *YouChuClient) OrderRefund(order ordersmodel.Orders) (code int, response front.YouChuOrderRefundResponse) {
|
2024-06-25 10:12:48 +08:00
|
|
|
var BusiMainId = time.Now().Format("20060102150405") + utils.RandomNumber(10)
|
2024-06-19 18:32:34 +08:00
|
|
|
request := front.YouChuOrderRefundRequest{
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: BusiMainId,
|
|
|
|
AccessType: "API",
|
2024-06-25 10:12:48 +08:00
|
|
|
ReqTime: time.Now().Format("20060102150405"),
|
2024-06-19 18:32:34 +08:00
|
|
|
Reserve: "",
|
|
|
|
Method: "b2c.gatewaypay.orderRefund",
|
|
|
|
Version: "1",
|
|
|
|
MerchantId: config.GetConf().YouChu.MerchantId,
|
|
|
|
AppID: config.GetConf().YouChu.AppID,
|
|
|
|
},
|
|
|
|
Body: front.RefundRequestBody{
|
|
|
|
BusiMainId: BusiMainId,
|
2024-07-31 14:17:48 +08:00
|
|
|
ReqTransTime: order.CreateTime.Format("20060102150405"),
|
2024-06-19 18:32:34 +08:00
|
|
|
Data: front.RefundRequestData{
|
|
|
|
TxnCode: "1003",
|
|
|
|
SourceId: "16",
|
2024-07-31 15:05:25 +08:00
|
|
|
ReqTraceId: order.OrderNo + time.Now().Format("20060102150405"),
|
2024-06-19 18:32:34 +08:00
|
|
|
ReqDate: time.Now().Format("20060102150405"),
|
|
|
|
MchtNo: config.GetConf().YouChu.MchtNo,
|
|
|
|
OrgTxnSeq: order.OrgTxnSeq,
|
|
|
|
TxnAmt: order.Price,
|
|
|
|
OrgTxnAmt: order.Price,
|
|
|
|
RefundDesc: "用户主动退款",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-06-25 18:31:16 +08:00
|
|
|
url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Body.BusiMainId
|
2024-06-19 18:32:34 +08:00
|
|
|
requestData := EncryptRequest(request)
|
|
|
|
bytes, err := json.Marshal(requestData)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, response
|
|
|
|
}
|
|
|
|
post, err := this.doPost(url, request.Body.BusiMainId, "b2c.gatewaypay.orderRefund", bytes)
|
2024-07-31 15:05:25 +08:00
|
|
|
utils.Log(nil, "b2c.orderRefund", string(post))
|
2024-06-19 18:32:34 +08:00
|
|
|
if err != nil {
|
2024-07-31 15:05:25 +08:00
|
|
|
fmt.Println(err, "****************")
|
2024-06-19 18:32:34 +08:00
|
|
|
return errorcode.YouChuOrderRefundFail, response
|
|
|
|
}
|
2024-06-25 10:12:48 +08:00
|
|
|
responseData := DecryptResponse(string(post), false)
|
|
|
|
utils.Log(nil, "OrderQuery", responseData)
|
|
|
|
err = json.Unmarshal([]byte(responseData), &response)
|
2024-06-19 18:32:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, front.YouChuOrderRefundResponse{}
|
|
|
|
}
|
|
|
|
return errorcode.Success, response
|
|
|
|
}
|
|
|
|
|
2024-08-21 14:30:20 +08:00
|
|
|
// 查询对账单
|
|
|
|
func (this *YouChuClient) AccountBillQuery() (code int, response front.AccountBillResponse) {
|
|
|
|
var now = time.Now().Format("20060102150405")
|
|
|
|
var BusiMainId = now + utils.RandomNumber(10)
|
|
|
|
request := front.AccountBillRequest{
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: BusiMainId,
|
|
|
|
AccessType: "API",
|
|
|
|
ReqTime: now,
|
|
|
|
Reserve: "",
|
|
|
|
Method: "ufile.query.commonQuery",
|
|
|
|
Version: "1",
|
|
|
|
MerchantId: config.GetConf().YouChu.MerchantId,
|
|
|
|
AppID: config.GetConf().YouChu.AppID,
|
|
|
|
},
|
|
|
|
Body: front.AccountBillBody{
|
2024-08-29 22:21:12 +08:00
|
|
|
"20240529",
|
|
|
|
"100610100018971",
|
2024-08-21 14:30:20 +08:00
|
|
|
},
|
|
|
|
}
|
2024-08-29 22:21:12 +08:00
|
|
|
url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo
|
2024-08-21 14:30:20 +08:00
|
|
|
requestData := EncryptRequest(request)
|
|
|
|
bytes, err := json.Marshal(requestData)
|
2024-08-29 22:21:12 +08:00
|
|
|
fmt.Println(string(bytes))
|
2024-08-21 14:30:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, response
|
|
|
|
}
|
|
|
|
post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.query.commonQuery", bytes)
|
|
|
|
utils.Log(nil, "ufile.query", string(post))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err, "****************")
|
|
|
|
return errorcode.YouChuOrderRefundFail, response
|
|
|
|
}
|
|
|
|
responseData := DecryptResponse(string(post), false)
|
|
|
|
utils.Log(nil, "OrderQuery", responseData)
|
|
|
|
err = json.Unmarshal([]byte(responseData), &response)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, front.AccountBillResponse{}
|
|
|
|
}
|
|
|
|
return errorcode.Success, response
|
|
|
|
}
|
|
|
|
|
|
|
|
// 下载对账单
|
2024-08-29 22:21:12 +08:00
|
|
|
func (this *YouChuClient) AccountBillDownload(fileId string, key int) (code int, response front.AccountBillDownloadResponse) {
|
2024-08-21 14:30:20 +08:00
|
|
|
var now = time.Now().Format("20060102150405")
|
|
|
|
var BusiMainId = now + utils.RandomNumber(10)
|
|
|
|
request := front.AccountBillDownloadRequest{
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: BusiMainId,
|
|
|
|
AccessType: "API",
|
|
|
|
ReqTime: now,
|
|
|
|
Reserve: "",
|
2024-08-29 22:21:12 +08:00
|
|
|
Method: "ufile.download.commonDownload",
|
2024-08-21 14:30:20 +08:00
|
|
|
Version: "1",
|
|
|
|
MerchantId: config.GetConf().YouChu.MerchantId,
|
|
|
|
AppID: config.GetConf().YouChu.AppID,
|
|
|
|
},
|
|
|
|
Body: front.AccountBillDownloadBody{
|
2024-08-29 22:21:12 +08:00
|
|
|
"100610100018971",
|
2024-08-21 14:30:20 +08:00
|
|
|
config.GetConf().SmFourKey,
|
|
|
|
fileId,
|
|
|
|
},
|
|
|
|
}
|
2024-08-29 22:21:12 +08:00
|
|
|
url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo
|
2024-08-21 14:30:20 +08:00
|
|
|
requestData := EncryptRequest(request)
|
|
|
|
bytes, err := json.Marshal(requestData)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, response
|
|
|
|
}
|
2024-08-29 22:21:12 +08:00
|
|
|
post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.download.commonDownload", bytes)
|
2024-08-21 14:30:20 +08:00
|
|
|
utils.Log(nil, "ufile.query", string(post))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err, "****************")
|
|
|
|
return errorcode.YouChuOrderRefundFail, response
|
|
|
|
}
|
2024-08-29 22:21:12 +08:00
|
|
|
//fmt.Println(string(post))
|
|
|
|
//responseData := DecryptResponse(string(post), false)
|
|
|
|
//utils.Log(nil, "OrderQuery", responseData)
|
|
|
|
//err = json.Unmarshal([]byte(responseData), &response)
|
|
|
|
//fmt.Println(responseData)
|
2024-08-21 14:30:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, front.AccountBillDownloadResponse{}
|
|
|
|
} else {
|
|
|
|
//存入文件
|
2024-08-29 22:21:12 +08:00
|
|
|
//var rs []byte
|
|
|
|
res, err := base64.StdEncoding.DecodeString(string(post))
|
|
|
|
//fmt.Println(string(res))
|
2024-08-21 14:30:20 +08:00
|
|
|
if err == nil {
|
2024-08-29 22:21:12 +08:00
|
|
|
rs, err := utils.Sm4Decrpt(res)
|
|
|
|
//fmt.Println(string(rs), "dd")
|
2024-08-21 14:30:20 +08:00
|
|
|
if err == nil {
|
2024-08-29 22:21:12 +08:00
|
|
|
var ori = string(rs)
|
|
|
|
var content = ori[0 : strings.Index(ori, "=")+1]
|
|
|
|
rds, _ := base64.StdEncoding.DecodeString(content)
|
|
|
|
//fmt.Println(string(rds))
|
|
|
|
utils.DownloadBill(string(rds), key)
|
2024-08-21 14:30:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return errorcode.SystemError, front.AccountBillDownloadResponse{}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return errorcode.Success, response
|
|
|
|
}
|
|
|
|
|
2024-06-19 18:32:34 +08:00
|
|
|
func EncryptRequest(request interface{}) (RequestData front.YouChuRequest) {
|
|
|
|
input, _ := json.Marshal(request)
|
|
|
|
MerchantId := config.GetConf().YouChu.MerchantId
|
|
|
|
PrivateKey := config.GetConf().Sm2.PrivateKey
|
2024-06-24 11:44:38 +08:00
|
|
|
PublicKey := config.GetConf().YouChu.SopPublicKey
|
2024-06-19 18:32:34 +08:00
|
|
|
encrypt, err := postbank.Encrypt(MerchantId, PrivateKey, PublicKey, string(input), "", true)
|
|
|
|
if err != nil {
|
|
|
|
return front.YouChuRequest{}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(encrypt), &RequestData)
|
|
|
|
if err != nil {
|
|
|
|
return front.YouChuRequest{}
|
|
|
|
}
|
|
|
|
return RequestData
|
|
|
|
}
|
2024-06-24 11:44:38 +08:00
|
|
|
|
2024-06-25 10:12:48 +08:00
|
|
|
func DecryptResponse(response string, isRequest bool) (Rsponse string) {
|
2024-07-10 13:50:24 +08:00
|
|
|
var MerchantId, PrivateKey, PublicKey string
|
|
|
|
if isRequest {
|
|
|
|
MerchantId = config.GetConf().YouChuCallBack.MerchantId
|
2024-09-04 19:14:26 +08:00
|
|
|
PrivateKey = config.GetConf().YouChuCallBack.PrivateKey
|
2024-07-10 13:50:24 +08:00
|
|
|
PublicKey = config.GetConf().YouChuCallBack.SopPublicKey
|
|
|
|
} else {
|
|
|
|
MerchantId = config.GetConf().YouChu.MerchantId
|
|
|
|
PrivateKey = config.GetConf().Sm2.PrivateKey
|
|
|
|
PublicKey = config.GetConf().YouChu.SopPublicKey
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:12:48 +08:00
|
|
|
encrypt, err := postbank.Decrypt(MerchantId, PrivateKey, PublicKey, response, isRequest)
|
2024-06-24 11:44:38 +08:00
|
|
|
if err != nil {
|
2024-06-25 10:12:48 +08:00
|
|
|
return Rsponse
|
2024-06-24 11:44:38 +08:00
|
|
|
}
|
2024-06-25 10:12:48 +08:00
|
|
|
var RsponseData map[string]interface{}
|
2024-06-24 11:44:38 +08:00
|
|
|
err = json.Unmarshal([]byte(encrypt), &RsponseData)
|
2024-06-25 10:12:48 +08:00
|
|
|
utils.Log(nil, "DecryptResponse-body", err)
|
2024-06-24 11:44:38 +08:00
|
|
|
if err != nil {
|
2024-06-25 10:12:48 +08:00
|
|
|
return Rsponse
|
2024-06-24 11:44:38 +08:00
|
|
|
} else {
|
2024-06-25 10:12:48 +08:00
|
|
|
if RsponseData["body"] != nil {
|
2024-07-03 18:30:53 +08:00
|
|
|
if body, ok := RsponseData["body"].(string); ok {
|
|
|
|
Rsponse = body
|
|
|
|
} else {
|
|
|
|
notify, _ := json.Marshal(RsponseData["body"].(map[string]interface{}))
|
|
|
|
Rsponse = string(notify)
|
2024-06-25 10:12:48 +08:00
|
|
|
}
|
2024-06-24 11:44:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
utils.Log(nil, "DecryptResponse-body", Rsponse)
|
|
|
|
return Rsponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type T struct {
|
|
|
|
Head string `json:"head"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
}
|