YouChuKoffee/app/third/youchu/youchu_api.go

325 lines
11 KiB
Go

package youchu
import (
"encoding/base64"
"encoding/json"
"fmt"
"qteam/app/constants/errorcode"
"qteam/app/http/entities/front"
"qteam/app/models/ordersmodel"
"qteam/app/utils"
"qteam/app/utils/postbank"
"qteam/config"
"strings"
"time"
)
func NewYouChuClient(cfg config.YouChuConfig) *YouChuClient {
return &YouChuClient{
cfg: cfg,
}
}
func (this *YouChuClient) Login(code string) (Code int, response front.YouChuLoginResponse) {
partnerTxSriNo := time.Now().Format("20060102150405") + utils.RandomNumber(10)
url := this.cfg.LoginHost + this.cfg.MerchantId + ".htm?partnerTxSriNo=" + partnerTxSriNo
request := front.YouChuLoginRequest{
Head: front.YouChuRequestHeader{
PartnerTxSriNo: partnerTxSriNo,
AccessType: "API",
Reserve: "",
ReqTime: time.Now().Format("20060102150405"),
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"),
},
}
requestData := EncryptRequest(request)
bytes, err := json.Marshal(requestData)
utils.Log(nil, "YouChuLoginRequest", string(bytes))
if err != nil {
return errorcode.SystemError, response
}
post, err := this.doPost(url, partnerTxSriNo, "crecard.getCustomerInfo", bytes)
var rq, _ = json.Marshal(request)
utils.Log(nil, "YouChuLogin", string(post), string(rq), err)
if err != nil {
return errorcode.SystemError, response
}
responseData := DecryptResponse(string(post), false)
utils.Log(nil, "YouChuLogin", responseData)
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
return errorcode.SystemError, response
}
return errorcode.Success, response
}
func (this *YouChuClient) OrderQuery(order ordersmodel.Orders) (code int, response front.YouChuOrderQueryResponse) {
var BusiMainId = time.Now().Format("20060102150405") + utils.RandomNumber(10)
request := front.YouChuRequestBody{
Body: front.YouChuOrderRequest{
BusiMainId: BusiMainId,
ReqTransTime: order.CreateTime.Format("20060102150405"),
Data: front.OrderQuery{
TxnCode: "1004",
SourceId: "16",
ReqTraceId: utils.GenerateRequestNumber(),
ReqDate: time.Now().Format("20060102150405"),
TxnDt: time.Now().Format("20060102"),
MchtNo: config.GetConf().YouChu.MchtNo,
OldSeqNo: order.OrderNo,
},
},
Head: front.YouChuRequestHeader{
PartnerTxSriNo: BusiMainId,
ReqTime: time.Now().Format("20060102150405"),
AccessType: "API",
Reserve: "",
Method: "b2c.gatewaypay.orderQuery",
Version: "1",
MerchantId: config.GetConf().YouChu.MerchantId,
AppID: config.GetConf().YouChu.AppID,
},
}
str, _ := json.Marshal(request.Body)
utils.Log(nil, "OrderQuery", string(str))
url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Body.BusiMainId
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
}
responseData := DecryptResponse(string(post), false)
utils.Log(nil, "OrderQuery", responseData)
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
return errorcode.SystemError, response
}
return errorcode.Success, response
}
func (this *YouChuClient) OrderRefund(order ordersmodel.Orders) (code int, response front.YouChuOrderRefundResponse) {
var BusiMainId = time.Now().Format("20060102150405") + utils.RandomNumber(10)
request := front.YouChuOrderRefundRequest{
Head: front.YouChuRequestHeader{
PartnerTxSriNo: BusiMainId,
AccessType: "API",
ReqTime: time.Now().Format("20060102150405"),
Reserve: "",
Method: "b2c.gatewaypay.orderRefund",
Version: "1",
MerchantId: config.GetConf().YouChu.MerchantId,
AppID: config.GetConf().YouChu.AppID,
},
Body: front.RefundRequestBody{
BusiMainId: BusiMainId,
ReqTransTime: order.CreateTime.Format("20060102150405"),
Data: front.RefundRequestData{
TxnCode: "1003",
SourceId: "16",
ReqTraceId: order.OrderNo + time.Now().Format("20060102150405"),
ReqDate: time.Now().Format("20060102150405"),
MchtNo: config.GetConf().YouChu.MchtNo,
OrgTxnSeq: order.OrgTxnSeq,
TxnAmt: order.Price,
OrgTxnAmt: order.Price,
RefundDesc: "用户主动退款",
},
},
}
url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Body.BusiMainId
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)
utils.Log(nil, "b2c.orderRefund", string(post))
if err != nil {
fmt.Println(err, "****************")
return errorcode.YouChuOrderRefundFail, response
}
responseData := DecryptResponse(string(post), false)
utils.Log(nil, "OrderQuery", responseData)
//{"mchtNo":"100510102303326","reqDate":"20241022144013","reqTraceId":"2024102214370107154720241022144013","respCd":"9035","respCode":"719035","respDesc":"该订单已全部退货","respMsg":"该订单已全部退货","sourceId":"16","success":false,"txnAmt":"19.80","txnCode":"1003"}
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
return errorcode.SystemError, front.YouChuOrderRefundResponse{}
}
return errorcode.Success, response
}
// 查询对账单
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{
"20240529",
"100610100018971",
},
}
url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo
requestData := EncryptRequest(request)
bytes, err := json.Marshal(requestData)
fmt.Println(string(bytes))
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
}
// 下载对账单
func (this *YouChuClient) AccountBillDownload(fileId string, key int) (code int, response front.AccountBillDownloadResponse) {
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: "",
Method: "ufile.download.commonDownload",
Version: "1",
MerchantId: config.GetConf().YouChu.MerchantId,
AppID: config.GetConf().YouChu.AppID,
},
Body: front.AccountBillDownloadBody{
"100610100018971",
config.GetConf().SmFourKey,
fileId,
},
}
url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo
requestData := EncryptRequest(request)
bytes, err := json.Marshal(requestData)
if err != nil {
return errorcode.SystemError, response
}
post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.download.commonDownload", bytes)
utils.Log(nil, "ufile.query", string(post))
if err != nil {
fmt.Println(err, "****************")
return errorcode.YouChuOrderRefundFail, response
}
//fmt.Println(string(post))
//responseData := DecryptResponse(string(post), false)
//utils.Log(nil, "OrderQuery", responseData)
//err = json.Unmarshal([]byte(responseData), &response)
//fmt.Println(responseData)
if err != nil {
return errorcode.SystemError, front.AccountBillDownloadResponse{}
} else {
//存入文件
//var rs []byte
res, err := base64.StdEncoding.DecodeString(string(post))
//fmt.Println(string(res))
if err == nil {
rs, err := utils.Sm4Decrpt(res)
//fmt.Println(string(rs), "dd")
if err == nil {
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)
}
} else {
return errorcode.SystemError, front.AccountBillDownloadResponse{}
}
}
return errorcode.Success, response
}
func EncryptRequest(request interface{}) (RequestData front.YouChuRequest) {
input, _ := json.Marshal(request)
MerchantId := config.GetConf().YouChu.MerchantId
PrivateKey := config.GetConf().Sm2.PrivateKey
PublicKey := config.GetConf().YouChu.SopPublicKey
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
}
func DecryptResponse(response string, isRequest bool) (Rsponse string) {
var MerchantId, PrivateKey, PublicKey string
if isRequest {
MerchantId = config.GetConf().YouChuCallBack.MerchantId
PrivateKey = config.GetConf().YouChuCallBack.PrivateKey
PublicKey = config.GetConf().YouChuCallBack.SopPublicKey
} else {
MerchantId = config.GetConf().YouChu.MerchantId
PrivateKey = config.GetConf().Sm2.PrivateKey
PublicKey = config.GetConf().YouChu.SopPublicKey
}
encrypt, err := postbank.Decrypt(MerchantId, PrivateKey, PublicKey, response, isRequest)
if err != nil {
return Rsponse
}
var RsponseData map[string]interface{}
err = json.Unmarshal([]byte(encrypt), &RsponseData)
utils.Log(nil, "DecryptResponse-body", err)
if err != nil {
return Rsponse
} else {
if RsponseData["body"] != nil {
if body, ok := RsponseData["body"].(string); ok {
Rsponse = body
} else {
notify, _ := json.Marshal(RsponseData["body"].(map[string]interface{}))
Rsponse = string(notify)
}
}
}
utils.Log(nil, "DecryptResponse-body", Rsponse)
return Rsponse
}
type T struct {
Head string `json:"head"`
Body string `json:"body"`
}