You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.8 KiB

package wxh5pay
import (
"encoding/base64"
"encoding/xml"
"io/ioutil"
"log"
"net/http"
"recook/internal/v2/logic/pay"
"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, &notification)
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 = pay.AsyncRefundSuccessCallback(info.OutRefundNo); err != nil {
log.Println(err)
c.XML(http.StatusOK, FAIL)
return
}
c.XML(http.StatusOK, SUCCESS)
}