Skip to content

Commit

Permalink
Merge pull request #15 from iGoogle-ink/gopay/remove-gorequest
Browse files Browse the repository at this point in the history
fix 去掉第三方http请求库
  • Loading branch information
iGoogle-ink authored Dec 9, 2019
2 parents 3c08518 + daf18e9 commit d074c71
Show file tree
Hide file tree
Showing 14 changed files with 453 additions and 289 deletions.
29 changes: 13 additions & 16 deletions alipay_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"strings"
"sync"
"time"

"github.com/parnurzeal/gorequest"
)

type AliPayClient struct {
Expand Down Expand Up @@ -520,12 +518,10 @@ func (a *AliPayClient) AliPayUserCertifyOpenQuery(body BodyMap) (aliRsp *AliPayU
}

// 向支付宝发送请求
func (a *AliPayClient) doAliPay(body BodyMap, method string) (bytes []byte, err error) {
func (a *AliPayClient) doAliPay(body BodyMap, method string) (bs []byte, err error) {
var (
bodyStr, sign, url, urlParam string
bodyBs []byte
res gorequest.Response
errs []error
bodyStr, sign, url string
bodyBs []byte
)
if body != nil {
if bodyBs, err = json.Marshal(body); err != nil {
Expand Down Expand Up @@ -590,31 +586,32 @@ func (a *AliPayClient) doAliPay(body BodyMap, method string) (bytes []byte, err
return
}
pubBody.Set("sign", sign)
urlParam = FormatAliPayURLParam(pubBody)
param := FormatAliPayURLParam(pubBody)
if method == "alipay.trade.app.pay" {
return []byte(urlParam), nil
return []byte(param), nil
}
if method == "alipay.user.certify.open.certify" {
if !a.IsProd {
return []byte(zfbSandboxBaseUrl + "?" + urlParam), nil
return []byte(zfbSandboxBaseUrl + "?" + param), nil
} else {
return []byte(zfbBaseUrl + "?" + urlParam), nil
return []byte(zfbBaseUrl + "?" + param), nil
}
}
if method == "alipay.trade.page.pay" {
if !a.IsProd {
return []byte(zfbSandboxBaseUrl + "?" + urlParam), nil
return []byte(zfbSandboxBaseUrl + "?" + param), nil
} else {
return []byte(zfbBaseUrl + "?" + urlParam), nil
return []byte(zfbBaseUrl + "?" + param), nil
}
}
agent := HttpAgent()
httpClient := NewHttpClient()
if !a.IsProd {
url = zfbSandboxBaseUrlUtf8
} else {
url = zfbBaseUrlUtf8
}
if res, bytes, errs = agent.Post(url).Type("form-data").SendString(urlParam).EndBytes(); len(errs) > 0 {
res, bs, errs := httpClient.Type(TypeForm).Post(url).SendString(param).EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
Expand All @@ -626,7 +623,7 @@ func (a *AliPayClient) doAliPay(body BodyMap, method string) (bytes []byte, err
}
return []byte(res.Request.URL.String()), nil
}
return
return bs, nil
}

func getSignData(bs []byte) (signData string) {
Expand Down
3 changes: 1 addition & 2 deletions alipay_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,5 @@ func FormatAliPayURLParam(body BodyMap) (urlParam string) {
for key, value := range body {
v.Add(key, value.(string))
}
urlParam = v.Encode()
return
return v.Encode()
}
16 changes: 7 additions & 9 deletions alipay_service_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func AliPaySystemOauthToken(appId, privateKey, grantType, codeOrToken string) (r
}

// aliPaySystemOauthToken 向支付宝发送请求
func aliPaySystemOauthToken(appId, privateKey string, body BodyMap, method string, isProd bool) (bytes []byte, err error) {
func aliPaySystemOauthToken(appId, privateKey string, body BodyMap, method string, isProd bool) (bs []byte, err error) {
body.Set("app_id", appId)
body.Set("method", method)
body.Set("format", "JSON")
Expand All @@ -447,22 +447,20 @@ func aliPaySystemOauthToken(appId, privateKey string, body BodyMap, method strin
body.Set("timestamp", time.Now().Format(TimeLayout))
body.Set("version", "1.0")
var (
sign, url string
errs []error
sign string
url = zfbBaseUrlUtf8
)
pKey := FormatPrivateKey(privateKey)
if sign, err = getRsaSign(body, "RSA2", pKey); err != nil {
return
return nil, err
}
body.Set("sign", sign)
agent := HttpAgent()
if !isProd {
url = zfbSandboxBaseUrlUtf8
} else {
url = zfbBaseUrlUtf8
}
if _, bytes, errs = agent.Post(url).Type("form-data").SendString(FormatAliPayURLParam(body)).EndBytes(); len(errs) > 0 {
_, bs, errs := NewHttpClient().Type(TypeForm).Post(url).SendString(FormatAliPayURLParam(body)).EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
return
return bs, nil
}
153 changes: 153 additions & 0 deletions body_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package gopay

import (
"encoding/json"
"encoding/xml"
"io"
"reflect"
"sort"
"strings"
)

type BodyMap map[string]interface{}

// 设置参数
func (bm BodyMap) Set(key string, value interface{}) {
bm[key] = value
}

// 获取参数
func (bm BodyMap) Get(key string) string {
if bm == nil {
return null
}
var (
value interface{}
ok bool
v string
)
if value, ok = bm[key]; !ok {
return null
}
if v, ok = value.(string); ok {
return v
}
return convertToString(value)
}

func convertToString(v interface{}) (str string) {
if v == nil {
return null
}
var (
bs []byte
err error
)
if bs, err = json.Marshal(v); err != nil {
return null
}
str = string(bs)
return
}

// 删除参数
func (bm BodyMap) Remove(key string) {
delete(bm, key)
}

type xmlMapEntry struct {
XMLName xml.Name
Value string `xml:",chardata"`
}

func (bm BodyMap) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
if len(bm) == 0 {
return nil
}
var (
value string
vKind reflect.Kind
)
if err = e.EncodeToken(start); err != nil {
return
}
for k, v := range bm {
vKind = reflect.ValueOf(v).Kind()
switch vKind {
case reflect.String:
value = v.(string)
case reflect.Int:
value = Int2String(v.(int))
case reflect.Int64:
value = Int642String(v.(int64))
case reflect.Float32:
value = Float32ToString(v.(float32))
case reflect.Float64:
value = Float64ToString(v.(float64))
default:
value = ""
}
e.Encode(xmlMapEntry{XMLName: xml.Name{Local: k}, Value: value})
}
return e.EncodeToken(start.End())
}

func (bm *BodyMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for {
var e xmlMapEntry
err = d.Decode(&e)
if err == io.EOF {
break
} else if err != nil {
return
}
bm.Set(e.XMLName.Local, e.Value)
}
return
}

// ("bar=baz&foo=quux") sorted by key.
func (bm BodyMap) EncodeWeChatSignParams(apiKey string) string {
var (
buf strings.Builder
keyList []string
)
for k := range bm {
keyList = append(keyList, k)
}
sort.Strings(keyList)
for _, k := range keyList {
if v := bm.Get(k); v != null {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
buf.WriteByte('&')
}
}
buf.WriteString("key")
buf.WriteByte('=')
buf.WriteString(apiKey)
return buf.String()
}

// ("bar=baz&foo=quux") sorted by key.
func (bm BodyMap) EncodeAliPaySignParams() string {
var (
buf strings.Builder
keyList []string
)
keyList = make([]string, 0, len(bm))
for k := range bm {
keyList = append(keyList, k)
}
sort.Strings(keyList)
for _, k := range keyList {
if v := bm.Get(k); v != null {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
buf.WriteByte('&')
}
}
return buf.String()[:buf.Len()-1]
}
2 changes: 1 addition & 1 deletion constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const (
null string = ""
TimeLayout string = "2006-01-02 15:04:05"
DateLayout string = "2006-01-02"
Version string = "1.4.5"
Version string = "1.4.6"
// 微信
// ===========================================================================================

Expand Down
9 changes: 0 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
module github.com/iGoogle-ink/gopay

go 1.13

require (
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 // indirect
github.com/parnurzeal/gorequest v0.2.16
github.com/pkg/errors v0.8.1 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5 // indirect
moul.io/http2curl v1.0.0 // indirect
)
26 changes: 0 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,26 +0,0 @@
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 h1:pEtiCjIXx3RvGjlUJuCNxNOw0MNblyR9Wi+vJGBFh+8=
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/parnurzeal/gorequest v0.2.16 h1:T/5x+/4BT+nj+3eSknXmCTnEVGSzFzPGdpqmUVVZXHQ=
github.com/parnurzeal/gorequest v0.2.16/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5 h1:bHNaocaoJxYBo5cw41UyTMLjYlb8wPY7+WFrnklbHOM=
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
Loading

0 comments on commit d074c71

Please sign in to comment.