2024-06-19 18:32:34 +08:00
|
|
|
package youchu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
mrand "math/rand"
|
|
|
|
"qteam/app/constants/errorcode"
|
|
|
|
"qteam/app/http/entities/front"
|
|
|
|
"qteam/app/models/ordersmodel"
|
|
|
|
"qteam/app/utils"
|
|
|
|
"qteam/app/utils/postbank"
|
|
|
|
"qteam/config"
|
|
|
|
"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-19 18:32:34 +08:00
|
|
|
partnerTxSriNo := time.Now().Format("20060102150405") + fmt.Sprintf("%10d", mrand.Intn(10000))
|
|
|
|
url := this.cfg.MerchantId + ".html?partnerTxSriNo=" + partnerTxSriNo
|
2024-06-24 11:44:38 +08:00
|
|
|
request := front.YouChuLoginRequest{
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: partnerTxSriNo,
|
|
|
|
AccessType: "API",
|
|
|
|
Reserve: "",
|
|
|
|
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-06-24 11:44:38 +08:00
|
|
|
requestData := EncryptRequest(request)
|
|
|
|
bytes, err := json.Marshal(requestData)
|
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-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
|
|
|
responseData := DecryptResponse(string(post))
|
|
|
|
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) {
|
|
|
|
var BusiMainId = time.Now().Format("20060102150405") + utils.RandomString(10)
|
|
|
|
request := front.YouChuRequestBody{
|
|
|
|
Body: front.YouChuOrderRequest{
|
|
|
|
BusiMainId: BusiMainId,
|
|
|
|
ReqTransTime: order.CreateTime.Format("2006-01-02 15:04:05"),
|
|
|
|
Data: front.OrderQuery{
|
|
|
|
TxnCode: "1004",
|
|
|
|
SourceId: "16",
|
|
|
|
ReqTraceId: order.OrderNo,
|
|
|
|
ReqDate: time.Now().Format("20060102150405"),
|
|
|
|
TxnDt: time.Now().Format("20060102"),
|
|
|
|
MchtNo: config.GetConf().YouChu.MchtNo,
|
|
|
|
OldSeqNo: utils.GenerateRequestNumber(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: BusiMainId,
|
|
|
|
AccessType: "API",
|
|
|
|
Reserve: "",
|
|
|
|
Method: "b2c.gatewaypay.orderQuery",
|
|
|
|
Version: "1",
|
|
|
|
MerchantId: config.GetConf().YouChu.MerchantId,
|
|
|
|
AppID: config.GetConf().YouChu.AppID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
url := config.GetConf().YouChu.MerchantId + ".html?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
|
|
|
|
}
|
2024-06-24 11:44:38 +08:00
|
|
|
responseData := DecryptResponse(string(post))
|
|
|
|
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) {
|
|
|
|
var BusiMainId = time.Now().Format("20060102150405") + utils.RandomString(10)
|
|
|
|
request := front.YouChuOrderRefundRequest{
|
|
|
|
Head: front.YouChuRequestHeader{
|
|
|
|
PartnerTxSriNo: BusiMainId,
|
|
|
|
AccessType: "API",
|
|
|
|
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("2006-01-02 15:04:05"),
|
|
|
|
Data: front.RefundRequestData{
|
|
|
|
TxnCode: "1003",
|
|
|
|
SourceId: "16",
|
|
|
|
ReqTraceId: order.OrderNo,
|
|
|
|
ReqDate: time.Now().Format("20060102150405"),
|
|
|
|
MchtNo: config.GetConf().YouChu.MchtNo,
|
|
|
|
OrgTxnSeq: order.OrgTxnSeq,
|
|
|
|
TxnAmt: order.Price,
|
|
|
|
OrgTxnAmt: order.Price,
|
|
|
|
RefundDesc: "用户主动退款",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
url := config.GetConf().YouChu.MerchantId + ".html?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", post)
|
|
|
|
if err != nil {
|
|
|
|
return errorcode.YouChuOrderRefundFail, response
|
|
|
|
}
|
2024-06-24 11:44:38 +08:00
|
|
|
responseData := DecryptResponse(string(post))
|
|
|
|
err = json.Unmarshal(post, &responseData)
|
2024-06-19 18:32:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return errorcode.SystemError, front.YouChuOrderRefundResponse{}
|
|
|
|
}
|
|
|
|
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
|
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
|
|
|
|
|
|
|
func DecryptResponse(response string) (Rsponse string) {
|
|
|
|
MerchantId := config.GetConf().YouChu.MerchantId
|
|
|
|
PrivateKey := config.GetConf().Sm2.PrivateKey
|
|
|
|
PublicKey := config.GetConf().YouChu.SopPublicKey
|
|
|
|
encrypt, err := postbank.Decrypt(MerchantId, PrivateKey, PublicKey, response, true)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var RsponseData map[string]string
|
|
|
|
err = json.Unmarshal([]byte(encrypt), &RsponseData)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
if RsponseData["body"] != "" {
|
|
|
|
Rsponse = RsponseData["body"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
utils.Log(nil, "DecryptResponse-body", Rsponse)
|
|
|
|
return Rsponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type T struct {
|
|
|
|
Head string `json:"head"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
}
|