123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package wechat
- import (
- "bytes"
- "encoding/xml"
- "errors"
- "fmt"
- "git.chuangxin1.com/cx/util"
- )
- type WePayRefund struct {
- XMLName xml.Name `xml:"xml"`
- AppID string `xml:"appid"`
- MchID string `xml:"mch_id"`
- NonceStr string `xml:"nonce_str"`
- Sign string `xml:"sign"`
-
- OutTradeNo string `xml:"out_trade_no"`
- OutRefundNo string `xml:"out_refund_no"`
- TotalFee int `xml:"total_fee"`
- RefundFee int `xml:"refund_fee"`
-
- NotifyURL string `xml:"notify_url"`
- }
- type WePayRefundReply struct {
- XMLName xml.Name `xml:"xml" json:"_,omitempty"`
- ReturnCode string `xml:"return_code"`
- ReturnMsg string `xml:"return_msg"`
- ResultCode string `xml:"result_code"`
- ErrCode string `xml:"err_code"`
- ErrCodeDes string `xml:"err_code_des"`
- AppID string `xml:"appid"`
- MchID string `xml:"mch_id"`
- NonceStr string `xml:"nonce_str"`
- Sign string `xml:"sign"`
- TransactionID string `xml:"transaction_id"`
- OutTradeNo string `xml:"out_trade_no"`
- OutRefundNo string `xml:"out_refund_no"`
- RefundFee int `xml:"refund_fee"`
- TotalFee int `xml:"total_fee"`
- FeeType int `xml:"fee_type"`
- CashFee int `xml:"cash_fee"`
- }
- func PayRefundCheck(reply WePayRefundReply) (ok bool, err error) {
-
- if reply.ReturnCode == "FAIL" {
- err = errors.New(reply.ReturnMsg)
- return
- }
-
- if reply.ResultCode == "FAIL" {
- err = errors.New(reply.ErrCodeDes)
- return
- }
- ok = true
- return
- }
- func PayRefund(config WePayConfig, order WePayRefund) (reply WePayRefundReply, err error) {
- var data []byte
- m := make(map[string]interface{})
- m["appid"] = order.AppID
- m["mch_id"] = order.MchID
- m["nonce_str"] = order.NonceStr
- m["total_fee"] = order.TotalFee
- m["out_trade_no"] = order.OutTradeNo
- m["out_refund_no"] = order.OutRefundNo
- m["total_fee"] = order.TotalFee
- m["refund_fee"] = order.RefundFee
- m["notify_url"] = order.NotifyURL
-
- order.Sign = Sign(m, config.Key)
- if data, err = xml.Marshal(order); err != nil {
- return
- }
- fmt.Println(string(data))
- url := WePayHost + WePayURLPayRefund
- reply, err = postRefund(url, config.SSLCert, config.SSLKey, data)
- bs, _ := xml.Marshal(reply)
- fmt.Println(string(bs))
- return
- }
- func postRefund(url, cert, key string, data []byte) (reply WePayRefundReply, err error) {
- var (
- msg util.Message
- headers = make(map[string]string)
- )
- headers["Accept"] = "application/xml"
- headers["Content-Type"] = "application/xml; charset=utf-8"
- msg, err = util.Post(url, cert, key, headers, bytes.NewReader(data))
- if err != nil {
- fmt.Println("util.Post", err)
- return
- }
- fmt.Println(string(msg.Body))
- err = xml.Unmarshal(msg.Body, &reply)
- return
- }
|