1 Star 0 Fork 0

bm_guangge / bm_common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
wx_download_bill.go 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
package wxpay
import (
"bytes"
"encoding/xml"
"io/ioutil"
"net/http"
"strings"
uuid "github.com/satori/go.uuid"
)
type DownloadBillReq struct {
AppId string `xml:"appid"`
MchId string `xml:"mch_id"`
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
BillDate string `xml:"bill_date"`
}
func WxDownloadBill(appId string, billDate string) (WxPayResponse, error) {
var downloadBillReq DownloadBillReq
downloadBillReq.AppId = appId
downloadBillReq.MchId = MchId
sourceUuid := uuid.NewV4()
formatUuid := strings.Replace(sourceUuid.String(), "-", "", -1)
downloadBillReq.NonceStr = formatUuid
downloadBillReq.BillDate = billDate
var mReqMap map[string]interface{}
mReqMap = make(map[string]interface{}, 0)
mReqMap["appid"] = downloadBillReq.AppId
mReqMap["mch_id"] = downloadBillReq.MchId
mReqMap["nonceStr"] = downloadBillReq.NonceStr
mReqMap["bill_date"] = downloadBillReq.BillDate
downloadBillReq.Sign = WxPayCalcSign(mReqMap, MchSecret)
xmlReq, err := xml.Marshal(downloadBillReq)
if err != nil {
return WxPayResponse{}, err
}
strReq := string(xmlReq)
strReq = strings.Replace(strReq, "DownloadBillReq", "xml", -1)
xmlReq = []byte(strReq)
request, err := http.NewRequest("POST", "https://api.mch.weixin.qq.com/pay/downloadbill", bytes.NewReader(xmlReq))
if err != nil {
return WxPayResponse{}, err
}
request.Header.Set("Accept", "application/xml")
request.Header.Set("Content-Type", "application/xml;charset=utf-8")
httpClient := http.Client{}
response, err := httpClient.Do(request)
if err != nil {
return WxPayResponse{}, err
}
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return WxPayResponse{}, err
}
var payResponse WxPayResponse
err = xml.Unmarshal(responseBody, &payResponse)
if err != nil {
return WxPayResponse{}, err
}
return payResponse, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bm_guangge/bm_common.git
git@gitee.com:bm_guangge/bm_common.git
bm_guangge
bm_common
bm_common
v1.1.22

搜索帮助

344bd9b3 5694891 D2dac590 5694891