|
|
package pay_service
import ( "encoding/xml" "fmt" "github.com/iGoogle-ink/gopay/v2" "github.com/iGoogle-ink/gopay/v2/wechat" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/libs/wx" "hudongzhuanjia/logger" "hudongzhuanjia/models" "time" )
func TransferExtend(bm gopay.BodyMap) (*wechat.TransfersResponse, error) { err := bm.CheckEmptyError("nonce_str", "partner_trade_no", "openid", "check_name", "amount", "desc", "spbill_create_ip") if err != nil { return nil, err }
bs, err := ClientExtend(bm, wx.TransfersURL) if err != nil { return nil, err }
wxRsp := new(wechat.TransfersResponse) if err = xml.Unmarshal(bs, wxRsp); err != nil { return nil, fmt.Errorf("xml.Unmarshal(%s):%s", string(bs), err) } return wxRsp, nil }
// 企业向微信用户个人付款(不支持沙箱环境)
func Transfer(desc, ip, openId string, amount int) (*wechat.TransfersResponse, error) {
nonceStr := gopay.GetRandomString(32) partnerTradeNo := gopay.GetRandomString(32) // 初始化参数结构体
body := make(gopay.BodyMap) body.Set("nonce_str", nonceStr) body.Set("partner_trade_no", partnerTradeNo) body.Set("openid", openId) body.Set("check_name", "NO_CHECK") // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名
body.Set("amount", amount) // 企业付款金额,单位为分
body.Set("desc", desc) // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
body.Set("spbill_create_ip", ip) body.Set("mchid", wx.Mchid) body.Set("mch_appid", wx.Appid)
res, err := TransferExtend(body) if err != nil { fmt.Println("Error:", err) return nil, err } fmt.Println("wxRsp:", *res) // 保存到user_transfer
return res, nil }
func QueryTransfer(partnerTradeNo string) { body := make(gopay.BodyMap) body.Set("nonce_str", gopay.GetRandomString(32)) body.Set("partner_trade_no", partnerTradeNo) // todo: 查询, 遇到查询失败,需要原订单号重新请求
}
// 发送红包
func SendRedPack(sendName, openId, wishing, ip, actName, remark string, totalAmount, totalNum, scene int) (*wx.RedPackResult, error) { mchBillNo := gopay.GetRandomString(28)
body := make(gopay.BodyMap) body.Set("nonce_str", gopay.GetRandomString(32)) body.Set("mch_billno", mchBillNo) body.Set("send_name", sendName) body.Set("re_openid", openId) body.Set("total_amount", totalAmount) body.Set("total_num", totalNum) body.Set("wishing", wishing) body.Set("client_ip", ip) body.Set("act_name", actName) body.Set("remark", remark) body.Set("scene_id", fmt.Sprintf("PRODUCT_%d", scene)) body.Set("wxappid", wx.Appid) body.Set("mch_id", wx.Mchid)
bs, err := ClientExtend(body, wx.SendRedPackURL) if err != nil { return nil, err } res := new(wx.RedPackResult) if err = xml.Unmarshal(bs, res); err != nil { logger.Sugar.Errorf("xml.Unmarshal(%s): %s", string(bs), err) return nil, fmt.Errorf("xml.Unmarshal(%s):%s", string(bs), err) }
if res.ReturnCode != wx.CODE_SUCCESS { err = fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg) logger.Sugar.Error(err) return nil, err }
userRedPack := new(models.UserRedPack) userRedPack.ErrCode = res.ErrCode userRedPack.ErrCodeDes = res.ErrCodeDes userRedPack.MchBillno = mchBillNo userRedPack.ReOpenid = openId userRedPack.SceneId = scene userRedPack.SendListid = res.SendListid userRedPack.TotalAmount = totalAmount userRedPack.TotalNum = totalNum userRedPack.ResultCode = res.ResultCode userRedPack.IsDelete = false userRedPack.UpdatedAt = time.Now() userRedPack.CreatedAt = time.Now() if _, err := core.GetXormAuto().InsertOne(userRedPack); err != nil { logger.Sugar.Errorf("insert into user red pack error: %s", err) return nil, err } return res, nil
}
// todo: 小心谨慎查询
func QueryRedPack(mchBillno string) (*wx.QueryRedPackResult, error) { body := make(gopay.BodyMap) body.Set("nonce_str", gopay.GetRandomString(32)) body.Set("mch_billno", mchBillno) body.Set("bill_type", "MCHT") body.Set("appid", wx.Appid) body.Set("mch_id", wx.Mchid) bs, err := ClientExtend(body, wx.QueryRedPackURL) if err != nil { logger.Sugar.Errorf("client extend request wx error: %s", err) return nil, err } res := new(wx.QueryRedPackResult) if err = xml.Unmarshal(bs, res); err != nil { logger.Sugar.Errorf("xml.Unmarshal(%s):%s", string(bs), err) return nil, fmt.Errorf("xml.Unmarshal(%s):%s", string(bs), err) } if res.ReturnCode != wx.CODE_SUCCESS { err = fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg) logger.Sugar.Error(err) return nil, err }
return res, nil }
|