This commit is contained in:
jiangyong27 2024-01-24 16:34:00 +08:00
parent f23e64d20b
commit 940a627ade
2 changed files with 22 additions and 26 deletions

View File

@ -103,6 +103,24 @@ func (q *App) GetResult(rspBody []byte) (map[string]interface{}, error) {
return result, nil
}
func (q *App) GetOpenid(userid string) (string, error) {
if err := q.refreshToken(); err != nil {
return "", err
}
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=%s", q.GetToken())
rspBody, err := util.HttpPostJson(reqUrl, nil, []byte(fmt.Sprintf(`{"userid" : "%s"}`, userid)))
if err != nil {
log.Errorf("httpPost url[%s] error :%s", reqUrl, err.Error())
return "", err
}
result, err := q.GetResult(rspBody)
if err != nil {
return "", err
}
return cast.ToString(result["openid"]), nil
}
func (q *App) GetUserInfo(userid string) (*UserInfo, error) {
if err := q.refreshToken(); err != nil {
return nil, err

View File

@ -22,7 +22,7 @@ import (
type PayReq struct {
TotalAmount int64
Title string
Userid string
Openid string
BillNo string
}
@ -125,24 +125,6 @@ func (w *AppPay) initPay() error {
return nil
}
func (q *App) getOpenid(userid string) (string, error) {
if err := q.refreshToken(); err != nil {
return "", err
}
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=%s", q.GetToken())
rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte(fmt.Sprintf(`{"userid" : "%s"}`, userid)))
if err != nil {
log.Errorf("httpPost url[%s] error :%s", reqUrl, err.Error())
return "", err
}
result, err := q.GetResult(rspBody)
if err != nil {
return "", err
}
return cast.ToString(result["openid"]), nil
}
func (p *AppPay) PayMoney(req *PayReq) error {
if p.cli == nil {
if err := p.initPay(); err != nil {
@ -151,20 +133,16 @@ func (p *AppPay) PayMoney(req *PayReq) error {
}
param := butil.NewParams()
userOpenid, err := p.getOpenid(req.Userid)
if err != nil {
log.Errorf("get openid error :%s", err.Error())
return err
}
if req.BillNo == "" {
req.BillNo = fmt.Sprintf("QY%s%s", time.Now().Format("20060102150405"), butil.CutTail(req.Userid, 12))
req.BillNo = fmt.Sprintf("QY%s", time.Now().Format("20060102150405"))
}
param.Set("nonce_str", goutil.RandomStr(32))
param.Set("mch_billno", req.BillNo)
param.Set("mch_id", p.config.MchId)
param.Set("wxappid", p.config.Corpid)
param.Set("agentid", p.config.Agent)
param.Set("re_openid", userOpenid)
param.Set("re_openid", req.Openid)
param.Set("total_amount", cast.ToString(req.TotalAmount))
param.Set("wishing", req.Title)
param.Set("act_name", "企业红包")