package wxh5pay import ( "encoding/base64" "encoding/xml" "io/ioutil" "log" "net/http" "recook/internal/api/mobile/pay/public" "recook/tools" "strings" "github.com/gin-gonic/gin" "github.com/nanjishidu/gomini/gocrypto" ) type RefundCallbackNotification struct { XMLName xml.Name `xml:"xml"` AppID string `xml:"appid"` MchId string `xml:"mch_id"` // 商户号 NonceStr string `xml:"nonce_str"` // 随机字符串 ReqInfo string `xml:"req_info"` } type RefundReqInfo struct { XMLName xml.Name `xml:"root"` OutRefundNo string `xml:"out_refund_no"` RefundFee uint `xml:"refund_fee"` RefundStatus string `xml:"refund_status"` SuccessTime string `xml:"success_time"` } func (r *RefundReqInfo) IsOk() bool { return r.RefundStatus == "SUCCESS" } /* */ func RefundCallback(c *gin.Context) { FAIL := gin.H{ "return_code": "FAIL", "return_msg": "OK", } SUCCESS := gin.H{ "return_code": "SUCCESS", "return_msg": "OK", } body, err := ioutil.ReadAll(c.Request.Body) if err != nil { panic(err) } var notification RefundCallbackNotification err = xml.Unmarshal(body, ¬ification) if err != nil { log.Println(err) c.XML(http.StatusOK, FAIL) return } _ = gocrypto.SetAesKey(strings.ToLower(tools.MD5(APIKey))) str, _ := base64.StdEncoding.DecodeString(notification.ReqInfo) deBody, err := gocrypto.AesECBDecrypt(str) if err != nil { log.Println(err) c.XML(http.StatusOK, FAIL) return } var info RefundReqInfo err = xml.Unmarshal(deBody, &info) if err != nil { log.Println(err) c.XML(http.StatusOK, FAIL) return } if info.IsOk() == false { log.Println(info) c.XML(http.StatusOK, FAIL) return } if err = public.AsyncRefundSuccessCallback(info.OutRefundNo); err != nil { log.Println(err) c.XML(http.StatusOK, FAIL) return } c.XML(http.StatusOK, SUCCESS) }