122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
package payment
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io/ioutil"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
// BillQuery 查询对账单
|
||
func (c *Client) BillQuery(date time.Time) (*BillQueryResponse, error) {
|
||
now := time.Now().Format("20060102150405")
|
||
BusiMainId := now + RandomNumber(10)
|
||
|
||
if date.IsZero() {
|
||
date = time.Now().AddDate(0, 0, -1)
|
||
}
|
||
|
||
requestBody := map[string]interface{}{
|
||
"head": map[string]string{
|
||
"partnerTxSriNo": BusiMainId,
|
||
"reqTime": now,
|
||
"method": "ufile.query.commonQuery",
|
||
"version": "1",
|
||
"merchantId": c.cfg.MerchantId,
|
||
"appID": c.cfg.AppID,
|
||
"accessType": "API",
|
||
"reserve": "",
|
||
},
|
||
"body": map[string]interface{}{
|
||
"date": date.Format("20060102"),
|
||
"mchtNo": c.cfg.MchtNo,
|
||
},
|
||
}
|
||
|
||
encryptedReq, err := EncryptRequest(requestBody, c.cfg)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("加密请求失败: %v", err)
|
||
}
|
||
|
||
encryptedBytes, _ := json.Marshal(encryptedReq)
|
||
url := c.cfg.FileHost + c.cfg.MerchantId + ".htm?partnerTxSriNo=" + BusiMainId
|
||
|
||
resp, err := http.Post(url, "application/json", bytes.NewReader(encryptedBytes))
|
||
if err != nil {
|
||
return nil, fmt.Errorf("请求失败: %v", err)
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
body, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("读取响应失败: %v", err)
|
||
}
|
||
|
||
if resp.StatusCode != http.StatusOK {
|
||
return nil, fmt.Errorf("HTTP请求失败,状态码: %d,响应: %s", resp.StatusCode, string(body))
|
||
}
|
||
|
||
responseData, err := c.DecryptResponse(string(body), false)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("解密响应失败: %v,原始响应: %s", err, string(body))
|
||
}
|
||
|
||
var result BillQueryResponse
|
||
if err := json.Unmarshal([]byte(responseData), &result); err != nil {
|
||
return nil, fmt.Errorf("解析响应失败: %v,响应内容: %s", err, responseData)
|
||
}
|
||
|
||
return &result, nil
|
||
}
|
||
|
||
// BillDownload 下载对账单
|
||
func (c *Client) BillDownload(fileId string, date time.Time) ([]byte, error) {
|
||
now := time.Now().Format("20060102150405")
|
||
BusiMainId := now + RandomNumber(10)
|
||
|
||
requestBody := map[string]interface{}{
|
||
"head": map[string]string{
|
||
"partnerTxSriNo": BusiMainId,
|
||
"reqTime": now,
|
||
"method": "ufile.download.commonDownload",
|
||
"version": "1",
|
||
"merchantId": c.cfg.MerchantId,
|
||
"appID": c.cfg.AppID,
|
||
"accessType": "API",
|
||
"reserve": "",
|
||
},
|
||
"body": map[string]interface{}{
|
||
"mchtNo": c.cfg.MchtNo,
|
||
"sm4Key": "",
|
||
"fileId": fileId,
|
||
},
|
||
}
|
||
|
||
encryptedReq, err := EncryptRequest(requestBody, c.cfg)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("加密请求失败: %v", err)
|
||
}
|
||
|
||
encryptedBytes, _ := json.Marshal(encryptedReq)
|
||
url := c.cfg.FileHost + c.cfg.MerchantId + ".htm?partnerTxSriNo=" + BusiMainId
|
||
|
||
resp, err := http.Post(url, "application/json", bytes.NewReader(encryptedBytes))
|
||
if err != nil {
|
||
return nil, fmt.Errorf("请求失败: %v", err)
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
body, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("读取响应失败: %v", err)
|
||
}
|
||
|
||
if resp.StatusCode != http.StatusOK {
|
||
return nil, fmt.Errorf("HTTP请求失败,状态码: %d,响应: %s", resp.StatusCode, string(body))
|
||
}
|
||
|
||
return body, nil
|
||
}
|