新增对账单下载

This commit is contained in:
qiyunfanbo126.com 2024-08-29 22:21:12 +08:00
parent 54979f4359
commit f0292afa9a
5 changed files with 73 additions and 25 deletions

View File

@ -1,6 +1,7 @@
package console package console
import ( import (
"fmt"
"github.com/qit-team/snow-core/command" "github.com/qit-team/snow-core/command"
"qteam/app/constants/errorcode" "qteam/app/constants/errorcode"
"qteam/app/models/ordersmodel" "qteam/app/models/ordersmodel"
@ -46,8 +47,14 @@ func ClearUnpayOrder() {
func DownLoadAccountBill() { func DownLoadAccountBill() {
client := youchu.NewYouChuClient(config.GetConf().YouChu) client := youchu.NewYouChuClient(config.GetConf().YouChu)
code, rs := client.AccountBillQuery() code, rs := client.AccountBillQuery()
fmt.Println(rs)
if code == errorcode.Success { if code == errorcode.Success {
fileId := rs.FileId if len(rs.Files) > 0 {
client.AccountBillDownload(fileId) for k, v := range rs.Files {
fileId := v["fileId"].(string)
client.AccountBillDownload(fileId, k)
}
}
} }
} }

View File

@ -127,7 +127,7 @@ type AccountBillBody struct {
type AccountBillResponse struct { type AccountBillResponse struct {
RespCode string `json:"respCode"` RespCode string `json:"respCode"`
FileId string `json:"fileId"` Files []map[string]interface{} `json:"files"`
} }
type RefundRequestData struct { type RefundRequestData struct {

View File

@ -4,13 +4,13 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"qteam/app/constants/errorcode" "qteam/app/constants/errorcode"
"qteam/app/http/entities/front" "qteam/app/http/entities/front"
"qteam/app/models/ordersmodel" "qteam/app/models/ordersmodel"
"qteam/app/utils" "qteam/app/utils"
"qteam/app/utils/postbank" "qteam/app/utils/postbank"
"qteam/config" "qteam/config"
"strings"
"time" "time"
) )
@ -176,13 +176,14 @@ func (this *YouChuClient) AccountBillQuery() (code int, response front.AccountBi
AppID: config.GetConf().YouChu.AppID, AppID: config.GetConf().YouChu.AppID,
}, },
Body: front.AccountBillBody{ Body: front.AccountBillBody{
now, "20240529",
config.GetConf().YouChu.MerchantId, "100610100018971",
}, },
} }
url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo
requestData := EncryptRequest(request) requestData := EncryptRequest(request)
bytes, err := json.Marshal(requestData) bytes, err := json.Marshal(requestData)
fmt.Println(string(bytes))
if err != nil { if err != nil {
return errorcode.SystemError, response return errorcode.SystemError, response
} }
@ -202,7 +203,7 @@ func (this *YouChuClient) AccountBillQuery() (code int, response front.AccountBi
} }
// 下载对账单 // 下载对账单
func (this *YouChuClient) AccountBillDownload(fileId string) (code int, response front.AccountBillDownloadResponse) { func (this *YouChuClient) AccountBillDownload(fileId string, key int) (code int, response front.AccountBillDownloadResponse) {
var now = time.Now().Format("20060102150405") var now = time.Now().Format("20060102150405")
var BusiMainId = now + utils.RandomNumber(10) var BusiMainId = now + utils.RandomNumber(10)
request := front.AccountBillDownloadRequest{ request := front.AccountBillDownloadRequest{
@ -211,44 +212,50 @@ func (this *YouChuClient) AccountBillDownload(fileId string) (code int, response
AccessType: "API", AccessType: "API",
ReqTime: now, ReqTime: now,
Reserve: "", Reserve: "",
Method: "ufile.query.commonQuery", Method: "ufile.download.commonDownload",
Version: "1", Version: "1",
MerchantId: config.GetConf().YouChu.MerchantId, MerchantId: config.GetConf().YouChu.MerchantId,
AppID: config.GetConf().YouChu.AppID, AppID: config.GetConf().YouChu.AppID,
}, },
Body: front.AccountBillDownloadBody{ Body: front.AccountBillDownloadBody{
config.GetConf().YouChu.MerchantId, "100610100018971",
config.GetConf().SmFourKey, config.GetConf().SmFourKey,
fileId, fileId,
}, },
} }
url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo
requestData := EncryptRequest(request) requestData := EncryptRequest(request)
bytes, err := json.Marshal(requestData) bytes, err := json.Marshal(requestData)
if err != nil { if err != nil {
return errorcode.SystemError, response return errorcode.SystemError, response
} }
post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.query.commonQuery", bytes) post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.download.commonDownload", bytes)
utils.Log(nil, "ufile.query", string(post)) utils.Log(nil, "ufile.query", string(post))
if err != nil { if err != nil {
fmt.Println(err, "****************") fmt.Println(err, "****************")
return errorcode.YouChuOrderRefundFail, response return errorcode.YouChuOrderRefundFail, response
} }
responseData := DecryptResponse(string(post), false) //fmt.Println(string(post))
utils.Log(nil, "OrderQuery", responseData) //responseData := DecryptResponse(string(post), false)
err = json.Unmarshal([]byte(responseData), &response) //utils.Log(nil, "OrderQuery", responseData)
//err = json.Unmarshal([]byte(responseData), &response)
//fmt.Println(responseData)
if err != nil { if err != nil {
return errorcode.SystemError, front.AccountBillDownloadResponse{} return errorcode.SystemError, front.AccountBillDownloadResponse{}
} else { } else {
//存入文件 //存入文件
bt, err := base64.StdEncoding.DecodeString(response.FileContent) //var rs []byte
res, err := base64.StdEncoding.DecodeString(string(post))
//fmt.Println(string(res))
if err == nil { if err == nil {
rs, err := utils.Sm4Decrpt(bt) rs, err := utils.Sm4Decrpt(res)
//fmt.Println(string(rs), "dd")
if err == nil { if err == nil {
err = ioutil.WriteFile("./bills/"+time.Now().Format(time.DateOnly)+"output.csv", rs, 0644) var ori = string(rs)
if err != nil { var content = ori[0 : strings.Index(ori, "=")+1]
rds, _ := base64.StdEncoding.DecodeString(content)
} //fmt.Println(string(rds))
utils.DownloadBill(string(rds), key)
} }
} else { } else {
@ -279,7 +286,7 @@ func DecryptResponse(response string, isRequest bool) (Rsponse string) {
var MerchantId, PrivateKey, PublicKey string var MerchantId, PrivateKey, PublicKey string
if isRequest { if isRequest {
MerchantId = config.GetConf().YouChuCallBack.MerchantId MerchantId = config.GetConf().YouChuCallBack.MerchantId
PrivateKey = config.GetConf().YouChuCallBack.PrivateKey PrivateKey = config.GetConf().Sm2.PrivateKey
PublicKey = config.GetConf().YouChuCallBack.SopPublicKey PublicKey = config.GetConf().YouChuCallBack.SopPublicKey
} else { } else {
MerchantId = config.GetConf().YouChu.MerchantId MerchantId = config.GetConf().YouChu.MerchantId

View File

@ -6,6 +6,7 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"crypto/sha512" "crypto/sha512"
"encoding/csv"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/ZZMarquis/gm/sm4" "github.com/ZZMarquis/gm/sm4"
@ -48,8 +49,7 @@ func GetHostIp() string {
} }
func Log(c *gin.Context, name string, msg ...interface{}) { func Log(c *gin.Context, name string, msg ...interface{}) {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
timeLayout := "2006-01-01 03:04:05" //转化所需模板 var datetime = time.Now().Format(time.DateTime)
var datetime = time.Unix(time.Now().Unix(), 0).Format(timeLayout)
fmt.Println(name, msg, file, line, datetime) fmt.Println(name, msg, file, line, datetime)
} }
@ -475,3 +475,36 @@ func Sm4Decrpt(data []byte) ([]byte, error) {
data, err := sm4.CBCDecrypt([]byte(config.GetConf().SmFourKey), []byte(iv), data) data, err := sm4.CBCDecrypt([]byte(config.GetConf().SmFourKey), []byte(iv), data)
return data, err return data, err
} }
func DownloadBill(xmlString string, key int) {
f, _ := os.Create(fmt.Sprintf("./bills/对账单%s-%d.csv", time.Now().Format(time.DateOnly), key))
defer f.Close()
w := csv.NewWriter(f)
w.Write([]string{"交易缩写", "清算日期", "商户日期时间 ", "订单号", "平台流水号", "商户号", "终端号", "订单金额",
"手续费", "响应码", "商户保留 1", "商户保留 2", "交易渠道", "费率类型", "交易通道", "成本手续费", "账户卡类型", "商户保留 3", "交易状态"})
var string = xmlString[strings.Index(xmlString, "CSER"):]
data := string[0:strings.Index(string, "<")]
rows := strings.Split(data, "\n")
for _, v := range rows {
var tp = "支付"
if strings.Index(v, "SPR") > 0 {
tp = "退款"
}
v = strings.Replace(v, "CSER", "", 1)
v = strings.Replace(v, "CSPR", "", 1)
row := strings.Split(v, "|+|")
if len(row) == 19 {
row[0] = tp
row[18] = strings.Replace(row[18], "|-|", "", 1)
row[2] = "'" + row[2]
row[3] = "'" + row[3]
row[4] = "'" + row[4]
row[5] = "'" + row[5]
fmt.Println(w.Write(row))
} else {
fmt.Println(v)
}
}
w.Flush()
}

View File

@ -52,6 +52,7 @@ type YouChuConfig struct {
AppID string //appid AppID string //appid
SopPublicKey string //服开公钥 SopPublicKey string //服开公钥
SopPrivateKey string //服开私钥 SopPrivateKey string //服开私钥
FileHost string
} }
type YouChuCallBack struct { type YouChuCallBack struct {