package pay_service import ( "errors" core2 "github.com/chanxuehong/wechat/mch/core" "github.com/chanxuehong/wechat/mch/pay" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/models" "hudongzhuanjia/utils" "time" ) const CALLBACK_REFUND_URL = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackRefund" func Refund(reason, outTradeNo, openId string, goodType, totalFee, refundFee int, userId, activityId int64) (*pay.RefundResponse, error) { client, err := Client() outRefundNo := utils.RandomStr(64) nonceStr := utils.RandomStr(32) res, err := pay.Refund2(client, &pay.RefundRequest{ TransactionId: "", OutTradeNo: outTradeNo, OutRefundNo: outRefundNo, TotalFee: int64(totalFee), RefundFee: int64(refundFee), NonceStr: nonceStr, SignType: core2.SignType_MD5, RefundFeeType: "CNY", RefundDesc: reason, }) // if err != nil { return nil, err } refund := new(models.UserRefund) refund.OutTradeNo = outTradeNo refund.Status = 0 refund.TotalFee = totalFee refund.GoodType = goodType refund.RefundDesc = reason refund.FeeType = "CNY" refund.OpenId = openId refund.UserId = userId refund.ActivityId = activityId refund.IsDelete = false refund.CreatedAt = time.Now() refund.UpdatedAt = time.Now() _, err = refund.AddUserRefund() if err != nil { return nil, err } return res, err } func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) { refund := new(models.UserRefund) exist, err := refund.GetByOutTradeNo(outTradeNo) if err != nil { return nil, err } if !exist { return nil, errors.New("不存在改笔退款") } client, err := Client() res, err := pay.RefundQuery2(client, &pay.RefundQueryRequest{ OutTradeNo: outTradeNo, NonceStr: utils.RandomStr(32), SignType: core2.SignType_MD5, }) //请求申请退款 if err != nil { return nil, err } refund.RefundAccount = res.RefundList[0].RefundAccount refund.RefundRecvAccount = res.RefundList[0].RefundRecvAccout refund.RefundStatus = res.RefundList[0].RefundStatus refund.Status = 1 refund.SuccessTime = res.RefundList[0].RefundSuccessTime refund.WxRefundId = res.RefundList[0].RefundId refund.TransactionId = res.TransactionId _, err = refund.QueryUpdate() if err != nil { return nil, err } return res, nil } func BatchQueryRefundByUserId(userId int64) error { refunds := make([]*models.UserRefund, 0) if err := core.GetXormAuto().Where("status = 0 and user_id=?", userId).Find(&refunds); err != nil { return err } if err := batchQueryRefund(refunds); err != nil { return err } return nil } func BatchQueryRefundByActivityId(activityId int64) error { refunds := make([]*models.UserRefund, 0) if err := core.GetXormAuto().Where("status = 0 and user_id=?", activityId).Find(&refunds); err != nil { return err } if err := batchQueryRefund(refunds); err != nil { return err } return nil } func batchQueryRefund(refunds []*models.UserRefund) error { for _, refund := range refunds { _, err := QueryRefund(refund.OutTradeNo) if err != nil { return err } } return nil }