Commit db7167e0 authored by appsuser's avatar appsuser

添加微信关闭订单接口

parent 18b05f52
......@@ -11,6 +11,7 @@ import (
)
var payGateway = "https://api.mch.weixin.qq.com/pay/unifiedorder"
var closeGateway = "https://api.mch.weixin.qq.com/pay/closeorder"
// Pay struct extends context
type Pay struct {
......@@ -276,3 +277,77 @@ func (pcf *Pay) PreNative(p *Params) (*PayResult, error) {
}
return nil, errors.New("[msg : xmlUnmarshalError] [rawReturn : " + string(rawRet) + "] [params : " + str + "] [sign : " + sign + "]")
}
type closeOrderReq struct{
AppID string `xml:"appid"`
MchID string `xml:"mch_id"`
SubMchID string `xml:"sub_mch_id"`
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
OutTradeNo string `xml:"out_trade_no"` //商户订单号
}
type CloseOrderResult struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
AppId string `xml:"appid"`
MCHId string `xml:"mch_id"`
DeviceInfo string `xml:"device_info"`
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
ResultCode string `xml:"result_code"`
ErrCode string `xml:"err_code"`
ErrCodeDes string `xml:"err_code_des"`
}
func (pcf *Pay) CloseOrder(outTradeNo string) (*CloseOrderResult, error){
var str string = ""
var sign string = ""
var nonceStr = util.RandomStr(32)
var request closeOrderReq
if pcf.PaySubMchID == "" {
template := "appid=%s&mch_id=%s&nonce_str=%s&out_trade_no=%s&key=%s"
str = fmt.Sprintf(template, pcf.AppID, pcf.PayMchID, nonceStr, outTradeNo, pcf.PayKey)
sign = util.MD5Sum(str)
request = closeOrderReq{
AppID: pcf.AppID,
MchID: pcf.PayMchID,
NonceStr: nonceStr,
Sign: sign,
OutTradeNo: outTradeNo,
}
}else{
template := "appid=%s&mch_id=%s&nonce_str=%s&out_trade_no=%s&sub_mch_id=%s&key=%s"
str = fmt.Sprintf(template, pcf.AppID, pcf.PayMchID, nonceStr, outTradeNo, pcf.PaySubMchID, pcf.PayKey)
sign = util.MD5Sum(str)
request = closeOrderReq{
AppID: pcf.AppID,
MchID: pcf.PayMchID,
SubMchID: pcf.PaySubMchID,
NonceStr: nonceStr,
Sign: sign,
OutTradeNo: outTradeNo,
}
}
rawRet, err := util.PostXML(closeGateway, request)
if err != nil {
return nil, errors.New(err.Error() + " parameters : " + str)
}
closeOrderRet := CloseOrderResult{}
err = xml.Unmarshal(rawRet, &closeOrderRet)
s, _ := json.Marshal(closeOrderRet)
fmt.Println("return close order msg:", string(s))
if err != nil {
return nil, errors.New(err.Error())
}
if closeOrderRet.ReturnCode == "SUCCESS" {
//pay success
if closeOrderRet.ResultCode == "SUCCESS" {
return &closeOrderRet, nil
}
return nil, errors.New(closeOrderRet.ErrCode + closeOrderRet.ErrCodeDes)
}
return nil, errors.New("[msg : xmlUnmarshalError] [rawReturn : " + string(rawRet) + "] [params : " + str + "] [sign : " + sign + "]")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment