diff --git a/controller/user.go b/controller/user.go index 447a2d3..cb3f51c 100644 --- a/controller/user.go +++ b/controller/user.go @@ -437,8 +437,7 @@ func UpgradeMember(c *gin.Context) { RespJson(c, status.InternalServerError, nil) return } - //totalFee = (totalFee / 100) * 100 - + if req.UserCouponId != 0 { var coupon model.UserCoupon err = model.NewUserCouponQuerySet(model.DB).IDEq(req.UserCouponId).One(&coupon) @@ -879,6 +878,7 @@ func UserMemberExpireDelayingPay(c *gin.Context) { return } + //userMemberExpireDelay.DelayAmount = userMemberExpireDelay.DelayAmount / 100 // 测试TODO //webPay, err := wxpay.WebPay(orderSn, uint32(userMemberExpireDelay.DelayAmount), user.WxOpenID, "N", wxpay.WxPayMemberExpireDelay, configInfo.NotifyUrl) //if err != nil { // logger.Error(errors.New("WebPay err")) diff --git a/controller/user_share_card.go b/controller/user_share_card.go index 352e60b..2a4cb76 100644 --- a/controller/user_share_card.go +++ b/controller/user_share_card.go @@ -558,13 +558,20 @@ func ShareCardRetrieveCancel(c *gin.Context) { orderRefundJson, _ := json.Marshal(&orderRefund) fmt.Println("订单取消 orderRefundJson:", string(orderRefundJson)) //err = wxpay.WxPayOrderRefund(orderRefund) - err = wxpay.TransactionOrderRefund(orderRefund) + err = wxpay.HmRefundTransaction(orderRefund) if err != nil { logger.Error("err:", err) RespJson(c, status.InternalServerError, nil) return } + //err = wxpay.TransactionOrderRefund(orderRefund) + //if err != nil { + // logger.Error("err:", err) + // RespJson(c, status.InternalServerError, nil) + // return + //} + } begin := model.DB.Begin() diff --git a/lib/wxpay/wx_pay.go b/lib/wxpay/wx_pay.go index 4910b43..26f9d2f 100644 --- a/lib/wxpay/wx_pay.go +++ b/lib/wxpay/wx_pay.go @@ -60,7 +60,7 @@ const ( HmPayMerchantId = "664403000030115" HmWxSubMerchantId = "546017470" //HmPayUnifiedOrderUrl = "https://hmpay.sandpay.com.cn/gateway/api" - HmPayUnifiedOrderUrl = "https://hmpay.sandpay.com.cn/gateway/api" + HmPayApiUrl = "https://hmpay.sandpay.com.cn/gateway/api" ) //web 微信支付 @@ -679,6 +679,90 @@ func TransactionOrderRefund(orderRefund OrderRefund) error { return nil } +type HmRefundBizContent struct { + OutOrderNo string `json:"out_order_no"` + RefundAmount float64 `json:"refund_amount"` + RefundRequestNo string `json:"refund_request_no"` //商户退款请求号 + StoreId string `json:"store_id"` + //ExtendParams string `json:"extend_params"` +} + +func HmRefundTransaction(orderRefund OrderRefund) error { + now := time.Now().Local() + //strTime := fmt.Sprintf("%04d%02d%02d%02d%02d%02d", now.Year(), now.Month(), now.Day(), now.Hour(), + // now.Minute(), now.Second()) + //expireTime := now.Add(time.Hour * 3) + //strExpireTime := fmt.Sprintf("%04d%02d%02d%02d%02d%02d", expireTime.Year(), expireTime.Month(), + // expireTime.Day(), expireTime.Hour(), expireTime.Minute(), expireTime.Second()) + nonce := utils.GenRandStr(NonceStringLength) + + //if notifyUrl == "" { + // logger.Error("NotifyUrl is null") + // return nil, errors.New("NotifyUrl is null") + //} + + unifiedOrderReq := HmJsPayUnifiedOrderReq{} + publicPara := HmPayPublicPara{ + AppId: HmPayMerchantId, + //SubAppId: HmWxSubMerchantId, + Method: "trade.refund", + //Charset: "UTF-8", + SignType: "RSA", + Sign: "", + Timestamp: now.Format(TimeFormat), + Nonce: nonce, + //Nonce: fmt.Sprintf("%d", time.Now().UnixNano()), + //Version: "1.0.0", + //Format: "JSON", + } + + biz := HmRefundBizContent{ + //OutOrderNo: orderRefund.OutRefundNo, + OutOrderNo: orderRefund.OutTradeNo, + RefundAmount: float64(orderRefund.Amount.Refund) / 100, + //RefundRequestNo: orderRefund.OutTradeNo, + RefundRequestNo: orderRefund.OutRefundNo, + StoreId: "100001", + } + unifiedOrderReq.HmPayPublicPara = publicPara + + bizString, err := json.Marshal(&biz) + if err != nil { + logger.Error("marshal biz err:", err) + return err + } + unifiedOrderReq.HmPayPublicPara.BizContent = string(bizString) + m, err := struct2Map(unifiedOrderReq) + if err != nil { + logger.Error(err) + return err + } + + //mJson, _ := json.MarshalIndent(&m, "", " ") + //fmt.Println("mJson:", string(mJson)) + + sign, err := GenHmPaySign(m) + if err != nil { + logger.Error(err) + return err + } + unifiedOrderReq.Sign = sign + + //unifiedOrderReqJson, _ := json.Marshal(&unifiedOrderReq) + //fmt.Println("unifiedOrderReqJson:", string(unifiedOrderReqJson)) + + unifiedOrderResp, err := HmPayRefundOrder(unifiedOrderReq) + if err != nil { + logger.Errorf("hm pay refund order error %#v", err) + return err + } + + unifiedOrderRespJson, _ := json.Marshal(&unifiedOrderResp) + fmt.Println("unifiedOrderRespJson:", string(unifiedOrderRespJson)) + + return nil +} + func PayOrderRefund(orderRefund OrderRefund) error { para, err := json.Marshal(&orderRefund) if err != nil { @@ -1073,7 +1157,7 @@ func HmJsPayUnifiedOrder(orderId string, totalFee uint32, openId, notifyUrl stri } biz := HmPayBizContent{ - Body: "会员费", + Body: "服务费", MerAppId: WxAppId, MerBuyerId: openId, CreateIp: clientIp, @@ -1369,7 +1453,53 @@ func HmPayUnifiedOrder(r HmJsPayUnifiedOrderReq) (HmPayUnifiedOrderRsp, error) { fmt.Println("data json:", string(data)) client := http.Client{} - req, err := http.NewRequest("POST", HmPayUnifiedOrderUrl, bytes.NewBuffer(data)) + req, err := http.NewRequest("POST", HmPayApiUrl, bytes.NewBuffer(data)) + if err != nil { + logger.Error(err) + return hmPayUnifiedOrderRsp, err + } + req.Header.Set("Content-Type", "application/json; charset=utf-8") + //req.Header.Set("Content-Type", "application/json") + + resp, err := client.Do(req) + if err != nil { + logger.Error(err) + return hmPayUnifiedOrderRsp, err + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + logger.Error(err) + return hmPayUnifiedOrderRsp, err + } + fmt.Println("body:", string(body)) + + defer resp.Body.Close() + + err = json.Unmarshal(body, &hmPayUnifiedOrderRsp) + if err != nil { + logger.Error("hmPayUnifiedOrderRsp err:", err) + return hmPayUnifiedOrderRsp, err + } + if hmPayUnifiedOrderRsp.Code != "200" { + return hmPayUnifiedOrderRsp, errors.New(hmPayUnifiedOrderRsp.Msg) + } + + return hmPayUnifiedOrderRsp, nil +} + +func HmPayRefundOrder(r HmJsPayUnifiedOrderReq) (HmPayUnifiedOrderRsp, error) { + var hmPayUnifiedOrderRsp HmPayUnifiedOrderRsp + + data, err := json.Marshal(r) + if err != nil { + logger.Error(err) + return hmPayUnifiedOrderRsp, err + } + + fmt.Println("data json:", string(data)) + client := http.Client{} + req, err := http.NewRequest("POST", HmPayApiUrl, bytes.NewBuffer(data)) if err != nil { logger.Error(err) return hmPayUnifiedOrderRsp, err diff --git a/lib/wxpay/wx_pay_test.go b/lib/wxpay/wx_pay_test.go index 5fb82a7..2d1afec 100644 --- a/lib/wxpay/wx_pay_test.go +++ b/lib/wxpay/wx_pay_test.go @@ -52,10 +52,14 @@ func TestWxPayTransactionOrderClose(t *testing.T) { } func TestHmJsPayUnifiedOrder(t *testing.T) { - order, err := HmJsPayUnifiedOrder("84FDC15BCC", 2, "ohuHh4riVVPxwKHrYHsWwZRpxVMk", "N", WxPayRentCard, "https://dev.switch.deovo.com:8004/api/v1/wxpay/notice") + order, err := HmJsPayUnifiedOrder("84FDC15BCC", 2, "ohuHh4riVVPxwKHrYHsWwZRpxVMk", "https://dev.switch.deovo.com:8004/api/v1/wxpay/notice") if err != nil { fmt.Println("err:", err) } fmt.Println("order:", order) } + +func TestHmRefundTransaction(t *testing.T) { + +} \ No newline at end of file diff --git a/model/model_test.go b/model/model_test.go index 4a6d8c5..e98c71d 100644 --- a/model/model_test.go +++ b/model/model_test.go @@ -2007,12 +2007,13 @@ func TestNameScanInvite(t *testing.T) { } +// 模拟邀请数据 func TestNewUser(t *testing.T) { InitTestDB() DB = DBDev InviteUid := uint32(63192613) - Uid := uint32(45321263) + Uid := uint32(41186164) user := GetUserByUid(Uid) nowTime := time.Now() @@ -2124,6 +2125,20 @@ func TestHmJsPayUnifiedOrder(t *testing.T) { fmt.Println("order:", order) } +func TestHmRefundTransaction(t *testing.T) { + refund := wxpay.OrderRefund{ + OutTradeNo: "84FDC15BCE", + OutRefundNo: "861C878AB8", + NotifyUrl: "", + Amount: wxpay.OrderRefundAmount{ + Refund: 15, + Total: 15, + Currency: "", + }, + } + wxpay.HmRefundTransaction(refund) +} + func TestUnicode(t *testing.T) { str := `{"msg":"\u8be5\u5355\u53f7\u6682\u65e0\u7269\u6d41\u8fdb\u5c55\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff0c\u6216\u68c0\u67e5\u516c\u53f8\u548c\u5355\u53f7\u662f\u5426\u6709\u8bef\u3002","status":"-3","error_code":"13","data":{"info":{"status":"0","msg":"\u8be5\u5355\u53f7\u6682\u65e0\u7269\u6d41\u8fdb\u5c55\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff0c\u6216\u68c0\u67e5\u516c\u53f8\u548c\u5355\u53f7\u662f\u5426\u6709\u8bef\u3002","_source_com":""},"com":"zhongtong","company":{"url":"http:\/\/www.zto.com\/?from=openv","fullname":"\u4e2d\u901a\u5feb\u9012","shortname":"\u4e2d\u901a","icon":{"id":"29","smallurl":"https:\/\/ss0.baidu.com\/6ONWsjip0QIZ8tyhnq\/it\/u=3682653099,2524883494&fm=58","smallpos":"0,496","middleurl":"https:\/\/ss2.baidu.com\/6ONYsjip0QIZ8tyhnq\/it\/u=1078213688,3146076104&fm=58","middlepos":"0,324","normal":"https:\/\/ss0.baidu.com\/6ONWsjip0QIZ8tyhnq\/it\/u=1022514261,1855787563&fm=58"},"website":{"title":"www.zto.com","url":"http:\/\/www.zto.com"},"tel":"95311","auxiliary":[{"title":"\u7f51\u70b9\u67e5\u8be2","url":"http:\/\/www.zto.com\/GuestService\/SiteQuery"},{"title":"\u7f51\u4e0a\u5bc4\u4ef6","url":"http:\/\/my.zto.com\/order"},{"title":"\u4ef7\u683c\u67e5\u8be2","url":"http:\/\/www.zto.com\/GuestService\/PriceQuery"},{"title":"\u4e2d\u901a\u4f18\u9009","url":"http:\/\/www.ztbest.com"}]},"source":{"logo":"https:\/\/ss2.baidu.com\/6ONYsjip0QIZ8tyhnq\/it\/u=1429564979,1787167512&fm=58","title":"\u6570\u636e\u6765\u81ea\u5feb\u9012100","url":"http:\/\/www.kuaidi100.com\/?from=baidu_ala","name":"\u5feb\u9012100","showName":"\u5feb\u9012100"}}}` data := make(map[string]interface{}) diff --git a/model/user.go b/model/user.go index 5843311..388842f 100644 --- a/model/user.go +++ b/model/user.go @@ -428,11 +428,17 @@ func (m *UserOpenMemberRecord) Refund(outTradeNo string, amount uint32) error { Currency: "CNY", }, } - err = wxpay.TransactionOrderRefund(orderRefund) + + err = wxpay.HmRefundTransaction(orderRefund) if err != nil { logger.Error("err:", err) return err } + //err = wxpay.TransactionOrderRefund(orderRefund) + //if err != nil { + // logger.Error("err:", err) + // return err + //} //FundRecord{ // Uid: m.Uid,