新增对账单下载
This commit is contained in:
parent
54979f4359
commit
f0292afa9a
|
@ -1,6 +1,7 @@
|
|||
package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/qit-team/snow-core/command"
|
||||
"qteam/app/constants/errorcode"
|
||||
"qteam/app/models/ordersmodel"
|
||||
|
@ -46,8 +47,14 @@ func ClearUnpayOrder() {
|
|||
func DownLoadAccountBill() {
|
||||
client := youchu.NewYouChuClient(config.GetConf().YouChu)
|
||||
code, rs := client.AccountBillQuery()
|
||||
fmt.Println(rs)
|
||||
if code == errorcode.Success {
|
||||
fileId := rs.FileId
|
||||
client.AccountBillDownload(fileId)
|
||||
if len(rs.Files) > 0 {
|
||||
for k, v := range rs.Files {
|
||||
fileId := v["fileId"].(string)
|
||||
client.AccountBillDownload(fileId, k)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,8 +126,8 @@ type AccountBillBody struct {
|
|||
}
|
||||
|
||||
type AccountBillResponse struct {
|
||||
RespCode string `json:"respCode"`
|
||||
FileId string `json:"fileId"`
|
||||
RespCode string `json:"respCode"`
|
||||
Files []map[string]interface{} `json:"files"`
|
||||
}
|
||||
|
||||
type RefundRequestData struct {
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"qteam/app/constants/errorcode"
|
||||
"qteam/app/http/entities/front"
|
||||
"qteam/app/models/ordersmodel"
|
||||
"qteam/app/utils"
|
||||
"qteam/app/utils/postbank"
|
||||
"qteam/config"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -176,13 +176,14 @@ func (this *YouChuClient) AccountBillQuery() (code int, response front.AccountBi
|
|||
AppID: config.GetConf().YouChu.AppID,
|
||||
},
|
||||
Body: front.AccountBillBody{
|
||||
now,
|
||||
config.GetConf().YouChu.MerchantId,
|
||||
"20240529",
|
||||
"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)
|
||||
bytes, err := json.Marshal(requestData)
|
||||
fmt.Println(string(bytes))
|
||||
if err != nil {
|
||||
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 BusiMainId = now + utils.RandomNumber(10)
|
||||
request := front.AccountBillDownloadRequest{
|
||||
|
@ -211,44 +212,50 @@ func (this *YouChuClient) AccountBillDownload(fileId string) (code int, response
|
|||
AccessType: "API",
|
||||
ReqTime: now,
|
||||
Reserve: "",
|
||||
Method: "ufile.query.commonQuery",
|
||||
Method: "ufile.download.commonDownload",
|
||||
Version: "1",
|
||||
MerchantId: config.GetConf().YouChu.MerchantId,
|
||||
AppID: config.GetConf().YouChu.AppID,
|
||||
},
|
||||
Body: front.AccountBillDownloadBody{
|
||||
config.GetConf().YouChu.MerchantId,
|
||||
"100610100018971",
|
||||
config.GetConf().SmFourKey,
|
||||
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)
|
||||
bytes, err := json.Marshal(requestData)
|
||||
if err != nil {
|
||||
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))
|
||||
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)
|
||||
//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 {
|
||||
//存入文件
|
||||
bt, err := base64.StdEncoding.DecodeString(response.FileContent)
|
||||
//var rs []byte
|
||||
res, err := base64.StdEncoding.DecodeString(string(post))
|
||||
//fmt.Println(string(res))
|
||||
if err == nil {
|
||||
rs, err := utils.Sm4Decrpt(bt)
|
||||
rs, err := utils.Sm4Decrpt(res)
|
||||
//fmt.Println(string(rs), "dd")
|
||||
if err == nil {
|
||||
err = ioutil.WriteFile("./bills/"+time.Now().Format(time.DateOnly)+"output.csv", rs, 0644)
|
||||
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 {
|
||||
|
@ -279,7 +286,7 @@ 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
|
||||
PrivateKey = config.GetConf().Sm2.PrivateKey
|
||||
PublicKey = config.GetConf().YouChuCallBack.SopPublicKey
|
||||
} else {
|
||||
MerchantId = config.GetConf().YouChu.MerchantId
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/csv"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/ZZMarquis/gm/sm4"
|
||||
|
@ -48,8 +49,7 @@ func GetHostIp() string {
|
|||
}
|
||||
func Log(c *gin.Context, name string, msg ...interface{}) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
timeLayout := "2006-01-01 03:04:05" //转化所需模板
|
||||
var datetime = time.Unix(time.Now().Unix(), 0).Format(timeLayout)
|
||||
var datetime = time.Now().Format(time.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)
|
||||
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()
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ type YouChuConfig struct {
|
|||
AppID string //appid
|
||||
SopPublicKey string //服开公钥
|
||||
SopPrivateKey string //服开私钥
|
||||
FileHost string
|
||||
}
|
||||
|
||||
type YouChuCallBack struct {
|
||||
|
|
Loading…
Reference in New Issue