微信支付退款

This commit is contained in:
gazebo
2019-11-21 09:20:56 +08:00
parent 75caa1fffd
commit db9e80212f
8 changed files with 207 additions and 25 deletions

View File

@@ -9,6 +9,7 @@ import (
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
@@ -20,7 +21,7 @@ func pay4OrderByWX(ctx *jxcontext.Context, order *model.GoodsOrder, vendorPayTyp
payCreatedAt := time.Now()
param := &wxpay.CreateOrderParam{
Body: "",
NotifyURL: "http://callback.test.jxc4.com/wxpay/msg/",
NotifyURL: globals.WxpayNotifyURL,
OutTradeNo: order.VendorOrderID,
SpbillCreateIP: ctx.GetRealRemoteIP(),
TradeType: vendorPayType2WxpayType(vendorPayType),
@@ -40,14 +41,19 @@ func pay4OrderByWX(ctx *jxcontext.Context, order *model.GoodsOrder, vendorPayTyp
PayCreatedAt: payCreatedAt,
PrepayID: result.PrepayID,
CodeURL: result.CodeURL,
TotalFee: int(order.ActualPayPrice),
}
dao.WrapAddIDCULDEntity(orderPay, ctx.GetUserName())
err = dao.CreateEntity(dao.GetDB(), orderPay)
}
return orderPay, err
}
func OnWxPayCallback(msg *wxpay.CallbackMsg) (err error) {
switch msg.MsgType {
case wxpay.MsgTypePay:
err = onWxpayFinished(msg.Data.(*wxpay.PayResultMsg))
case wxpay.MsgTypeRefund:
err = onWxpayRefund(msg.Data.(*wxpay.RefundResultMsg))
}
return err
}
@@ -62,9 +68,59 @@ func onWxpayFinished(msg *wxpay.PayResultMsg) (err error) {
if err = dao.GetEntity(db, orderPay, "VendorOrderID", "VendorID", "PayType", "DeletedAt"); err == nil {
orderPay.VendorPayType = msg.TradeType
orderPay.PayFinishedAt = utils.Time2Pointer(wxpay.PayTime2Time(msg.TimeEnd))
orderPay.Status = model.PayStatusYes
orderPay.TransactionID = msg.TransactionID
orderPay.OriginalData = utils.Format4Output(msg, true)
if msg.ResultCode == wxpay.ResponseCodeSuccess {
orderPay.Status = model.PayStatusYes
} else {
orderPay.Status = model.PayStatusFailed
}
dao.UpdateEntity(db, orderPay)
err = OnPayFinished(orderPay)
if msg.ResultCode == wxpay.ResponseCodeSuccess {
err = OnPayFinished(orderPay)
}
}
return err
}
func onWxpayRefund(msg *wxpay.RefundResultMsg) (err error) {
orderPayRefund := &model.OrderPayRefund{
RefundID: msg.ReqInfoObj.OutRefundNo,
}
db := dao.GetDB()
if err = dao.GetEntity(db, orderPayRefund, "RefundID"); err == nil {
if msg.ResultCode == wxpay.ResponseCodeSuccess {
orderPayRefund.Status = model.PayStatusYes
} else {
orderPayRefund.Status = model.PayStatusFailed
}
orderPayRefund.OriginalData = utils.Format4Output(msg, true)
dao.UpdateEntity(db, orderPayRefund)
} else if dao.IsNoRowsError(err) {
globals.SugarLogger.Warnf("收到异常的退款事件, msg:%s", utils.Format4Output(msg, true))
}
return err
}
func refundOrderByWX(ctx *jxcontext.Context, orderPay *model.OrderPay, refundID string) (orderPayRefund *model.OrderPayRefund, err error) {
result, err := api.WxpayAPI.PayRefund(&wxpay.PayRefundParam{
OutTradeNo: orderPay.VendorOrderID,
NotifyURL: globals.WxpayNotifyURL,
OutRefundNo: refundID,
TotalFee: orderPay.TotalFee,
RefundFee: orderPay.TotalFee,
})
if err == nil {
orderPayRefund = &model.OrderPayRefund{
RefundID: refundID,
VendorRefundID: result.RefundID,
VendorOrderID: orderPay.VendorOrderID,
VendorID: orderPay.VendorID,
Status: model.PayStatusNo,
TransactionID: orderPay.TransactionID,
RefundFee: orderPay.TotalFee,
RefundCreatedAt: time.Now(),
}
}
return orderPayRefund, err
}