github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mch/payutil/report.go (about)

     1  package payutil
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	"github.com/chanxuehong/wechat/mch/core"
     8  	"github.com/chanxuehong/wechat/util"
     9  )
    10  
    11  // Report 交易保障.
    12  func Report(clt *core.Client, req map[string]string) (resp map[string]string, err error) {
    13  	return clt.PostXML(core.APIBaseURL()+"/payitil/report", req)
    14  }
    15  
    16  type ReportRequest struct {
    17  	XMLName      struct{}  `xml:"xml" json:"-"`
    18  	DeviceInfo   string    `xml:"device_info"`   // 微信支付分配的终端设备号,商户自定义
    19  	NonceStr     string    `xml:"nonce_str"`     // 随机字符串,不长于32位。NOTE: 如果为空则系统会自动生成一个随机字符串。
    20  	SignType     string    `xml:"sign_type"`     // 签名类型,目前支持HMAC-SHA256和MD5,默认为MD5
    21  	InterfaceURL string    `xml:"interface_url"` // 刷卡支付终端上报统一填:https://api.mch.weixin.qq.com/pay/batchreport/micropay/total
    22  	UserIP       string    `xml:"user_ip"`       // 发起接口调用时的机器IP
    23  	Trades       string    `xml:"trades"`        // 上报数据包
    24  	ExecuteTime  int       `xml:"execute_time"`  // 接口耗时情况,单位为毫秒
    25  	ReturnCode   string    `xml:"return_code"`   // 返回状态码
    26  	ReturnMsg    string    `xml:"return_msg"`    // 返回信息
    27  	ResultCode   string    `xml:"result_code"`   // 业务结果
    28  	ErrCode      string    `xml:"err_code"`      // 错误代码
    29  	ErrCodeDesc  string    `xml:"err_code_des"`  // 错误代码描述
    30  	OutTradeNo   string    `xml:"out_trade_no"`  // 商户订单号
    31  	Time         time.Time `xml:"time"`          // 商户上报时间
    32  }
    33  
    34  // Report2 交易保障.
    35  func Report2(clt *core.Client, req *ReportRequest) (err error) {
    36  	m1 := make(map[string]string, 24)
    37  	if req.DeviceInfo != "" {
    38  		m1["device_info"] = req.DeviceInfo
    39  	}
    40  	if req.NonceStr != "" {
    41  		m1["nonce_str"] = req.NonceStr
    42  	} else {
    43  		m1["nonce_str"] = util.NonceStr()
    44  	}
    45  	if req.SignType != "" {
    46  		m1["sign_type"] = req.SignType
    47  	}
    48  	if req.InterfaceURL != "" {
    49  		m1["interface_url"] = req.InterfaceURL
    50  	}
    51  	if req.UserIP != "" {
    52  		m1["user_ip"] = req.UserIP
    53  	}
    54  	if req.Trades != "" {
    55  		m1["trades"] = req.Trades
    56  	}
    57  	if req.ExecuteTime > 0 {
    58  		m1["execute_time"] = strconv.Itoa(req.ExecuteTime)
    59  	}
    60  	if req.ReturnCode != "" {
    61  		m1["return_code"] = req.ReturnCode
    62  	}
    63  	if req.ReturnMsg != "" {
    64  		m1["return_msg"] = req.ReturnMsg
    65  	}
    66  	if req.ResultCode != "" {
    67  		m1["result_code"] = req.ResultCode
    68  	}
    69  	if req.ErrCode != "" {
    70  		m1["err_code"] = req.ErrCode
    71  	}
    72  	if req.ErrCodeDesc != "" {
    73  		m1["err_code_des"] = req.ErrCodeDesc
    74  	}
    75  	if req.OutTradeNo != "" {
    76  		m1["out_trade_no"] = req.OutTradeNo
    77  	}
    78  	if !req.Time.IsZero() {
    79  		m1["time"] = core.FormatTime(req.Time)
    80  	}
    81  
    82  	_, err = Report(clt, m1)
    83  	return
    84  }