Skip to content

Commit

Permalink
1、add new interface
Browse files Browse the repository at this point in the history
2、fix bug
  • Loading branch information
iGoogle-ink committed Dec 31, 2020
1 parent f0045f4 commit cf80797
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 71 deletions.
1 change: 1 addition & 0 deletions body_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (bm BodyMap) EncodeAliPaySignParams() string {
return buf.String()[:buf.Len()-1]
}

// ("bar=baz&foo=quux")
func (bm BodyMap) EncodeGetParams() string {
var (
buf strings.Builder
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/iGoogle-ink/gopay
go 1.13

require (
github.com/iGoogle-ink/gotil v1.0.14-0.20201227112208-5bb46f75fce5
github.com/iGoogle-ink/gotil v1.0.14-0.20201231083614-08a507cfc2b3
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,16 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iGoogle-ink/gotil v1.0.14-0.20201227112208-5bb46f75fce5 h1:QK3Bo95W5pz7lcrik09HUcu20UsLtyLRnrxTSUc0jB4=
github.com/iGoogle-ink/gotil v1.0.14-0.20201227112208-5bb46f75fce5/go.mod h1:mMNhhc9QSCZVtnfs/DXJBcUvE11ngkcTPNA0K9KREpI=
github.com/iGoogle-ink/gotil v1.0.14-0.20201231083614-08a507cfc2b3 h1:oiCYpwDeDM9fI7MhHz7cXexq6LrpOd6dI/Mp10uZKJs=
github.com/iGoogle-ink/gotil v1.0.14-0.20201231083614-08a507cfc2b3/go.mod h1:I7xZY7ewakSnpIZyREMGfjrG2m6YPJuBCZCpc9Ouffs=
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -700,6 +701,9 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.0.3/go.mod h1:twGxftLBlFgNVNakL7F+P/x9oYqoymG3YYT8cAfI9oI=
gorm.io/gorm v1.20.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.9/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion wechat/v3/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *ClientV3) GetPlatformCerts() (certs *PlatformCertRsp, err error) {
if err != nil {
return nil, err
}
certs = &PlatformCertRsp{StatusCode: res.StatusCode, SignInfo: si}
certs = &PlatformCertRsp{Code: res.StatusCode, SignInfo: si}
if res.StatusCode != http.StatusOK {
certs.Error = string(bs)
return certs, nil
Expand Down
94 changes: 83 additions & 11 deletions wechat/v3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ func TestGetPlatformCerts(t *testing.T) {
xlog.Error(err)
return
}
xlog.Debug("certs.StatusCode", certs.StatusCode)
xlog.Debug("certs.SignInfo", certs.SignInfo)

for _, v := range certs.Certs {
xlog.Debug("cert:", v)
if certs.Code == Success {
for _, v := range certs.Certs {
xlog.Debug("cert:", v)
}
return
}
xlog.Errorf("certs:%s", certs.Error)
}

func TestV3VerifySign(t *testing.T) {
Expand Down Expand Up @@ -94,7 +95,11 @@ func TestV3Jsapi(t *testing.T) {
xlog.Error(err)
return
}
xlog.Debug("wxRsp:", wxRsp)
if wxRsp.Code == Success {
xlog.Debugf("wxRsp:%#v", wxRsp.Response)
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
}

func TestV3Native(t *testing.T) {
Expand All @@ -118,9 +123,11 @@ func TestV3Native(t *testing.T) {
xlog.Error(err)
return
}
xlog.Debug("wxRsp.StatusCode:", wxRsp.StatusCode)
xlog.Debugf("wxRsp.SignInfo:%#v", wxRsp.SignInfo)
xlog.Debugf("wxRsp.Response:%#v", wxRsp.Response)
if wxRsp.Code == Success {
xlog.Debugf("wxRsp:%#v", wxRsp.Response)
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
}

func TestV3QueryOrder(t *testing.T) {
Expand All @@ -130,8 +137,73 @@ func TestV3QueryOrder(t *testing.T) {
xlog.Error(err)
return
}
if wxRsp.StatusCode == 200 {
if wxRsp.Code == Success {
xlog.Debugf("wxRsp:%#v", wxRsp.Response)
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
}

func TestV3CloseOrder(t *testing.T) {
wxRsp, err := client.V3TransactionCloseOrder("FY160932049419637602")
if err != nil {
xlog.Error(err)
return
}
if wxRsp.Code == Success {
xlog.Error("success")
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
}

func TestV3BillTradeBill(t *testing.T) {
bm := make(gopay.BodyMap)
bm.Set("bill_date", "2020-12-30").
Set("tar_type", "GZIP")

wxRsp, err := client.V3BillTradeBill(bm)
if err != nil {
xlog.Error(err)
return
}
if wxRsp.Code == Success {
xlog.Debugf("wxRsp:%#v", wxRsp.Response)
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
}

func TestV3BillFundFlowBill(t *testing.T) {
bm := make(gopay.BodyMap)
bm.Set("bill_date", "2020-12-30").
Set("tar_type", "GZIP")

wxRsp, err := client.V3BillFundFlowBill(bm)
if err != nil {
xlog.Error(err)
return
}
if wxRsp.Code == Success {
xlog.Debugf("wxRsp:%#v", wxRsp.Response)
return
}
xlog.Errorf("wxRsp:%s", wxRsp.Error)
}

func TestV3BillDownLoadBill(t *testing.T) {
url := "https://api.mch.weixin.qq.com/v3/billdownload/file?token=4MWpG4bWfL3smAe2AeB8scfp1MN0LYORxW691-jI-wL9J9fA6F0qG0q66y44xrur&tartype=gzip"
fileBytes, err := client.V3BillDownLoadBill(url)
if err != nil {
xlog.Error(err)
return
}
xlog.Debugf("wxRsp:%s", wxRsp.Error)
xlog.Debugf("fileBytes:%v", fileBytes)

// 申请账单时采用 GZIP 压缩,返回 bytes 为压缩文件
//err = ioutil.WriteFile("bill.zip", fileBytes, 0666)
//if err != nil {
// xlog.Error("ioutil.WriteFile:", err)
// return
//}
}
142 changes: 115 additions & 27 deletions wechat/v3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package wechat
type OrderNoType uint8

const (
Success = 0
SignTypeRSA = "RSA"

MethodPost = "POST"
MethodGet = "GET"
HeaderAuthorization = "Authorization"
Expand All @@ -16,14 +19,26 @@ const (

v3BaseUrlCh = "https://api.mch.weixin.qq.com" // 中国国内

// 基础H支付
v3GetCerts = "/v3/certificates"
v3ApiPayApp = "/v3/pay/transactions/app"
v3ApiJsapi = "/v3/pay/transactions/jsapi"
v3ApiNative = "/v3/pay/transactions/native"
v3ApiH5 = "/v3/pay/transactions/h5"
v3ApiQueryOrderTransactionId = "/v3/pay/transactions/id/%s" // transaction_id
v3ApiQueryOrderOutTradeNo = "/v3/pay/transactions/out-trade-no/%s" // out_trade_no
v3ApiCloseOrder = "/v3/pay/transactions/out-trade-no/%s/close" // out_trade_no
v3ApiPayApp = "/v3/pay/transactions/app" // APP 下单
v3ApiJsapi = "/v3/pay/transactions/jsapi" // JSAPI 下单
v3ApiNative = "/v3/pay/transactions/native" // Native 下单
v3ApiH5 = "/v3/pay/transactions/h5" // H5 下单
v3ApiQueryOrderTransactionId = "/v3/pay/transactions/id/%s" // transaction_id 查询订单
v3ApiQueryOrderOutTradeNo = "/v3/pay/transactions/out-trade-no/%s" // out_trade_no 查询订单
v3ApiCloseOrder = "/v3/pay/transactions/out-trade-no/%s/close" // out_trade_no 关闭订单
v3ApiTradeBill = "/v3/bill/tradebill" // 申请交易账单
v3ApiFundFlowBill = "/v3/bill/fundflowbill" // 申请资金账单
v3ApiLevel2FundFlowBill = "/v3/ecommerce/bill/fundflowbill" // 申请二级商户资金账单

// 合单支付
v3CombinePayApp = "/v3/combine-transactions/app"
v3CombinePayH5 = "/v3/combine-transactions/h5"
v3CombinePayJsapi = "/v3/combine-transactions/jsapi"
v3CombineNative = "/v3/combine-transactions/native"
v3CombineQuery = "/v3/combine-transactions/out-trade-no/%s"
v3CombineClose = "/v3/combine-transactions/out-trade-no/%s/close"

// 订单号类型,1-微信订单号,2-商户订单号
TransactionId OrderNoType = 1
Expand All @@ -40,42 +55,95 @@ const (
)

type PlatformCertRsp struct {
StatusCode int `json:"-"`
SignInfo *SignInfo `json:"-"`
Certs []*PlatformCertItem `json:"certs"`
Error string `json:"-"`
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Certs []*PlatformCertItem `json:"certs"`
Error string `json:"-"`
}

// Prepay 支付Rsp
type PrepayRsp struct {
StatusCode int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *Prepay `json:"response,omitempty"`
Error string `json:"-"`
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *Prepay `json:"response,omitempty"`
Error string `json:"-"`
}

// H5 支付Rsp
type H5Rsp struct {
StatusCode int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *H5Url `json:"response,omitempty"`
Error string `json:"-"`
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *H5Url `json:"response,omitempty"`
Error string `json:"-"`
}

// Native 支付Rsp
type NativeRsp struct {
StatusCode int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *Native `json:"response,omitempty"`
Error string `json:"-"`
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *Native `json:"response,omitempty"`
Error string `json:"-"`
}

// 查询订单 Rsp
type QueryOrderRsp struct {
StatusCode int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *QueryOrder `json:"response,omitempty"`
Error string `json:"-"`
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *QueryOrder `json:"response,omitempty"`
Error string `json:"-"`
}

// 关闭订单 Rsp
type CloseOrderRsp struct {
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Error string `json:"-"`
}

// 交易、资金账单 Rsp
type BillRsp struct {
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *TradeBill `json:"response,omitempty"`
Error string `json:"-"`
}

// 二级商户资金账单 Rsp
type Level2FundFlowBillRsp struct {
Code int `json:"-"`
SignInfo *SignInfo `json:"-"`
Response *DownloadBill `json:"response,omitempty"`
Error string `json:"-"`
}

// ==================================分割==================================

type JSAPIPayParams struct {
AppId string `json:"appId"`
TimeStamp string `json:"timeStamp"`
NonceStr string `json:"nonceStr"`
Package string `json:"package"`
SignType string `json:"signType"`
PaySign string `json:"paySign"`
}

type AppPayParams struct {
Appid string `json:"appid"`
Partnerid string `json:"partnerid"`
Prepayid string `json:"prepayid"`
Package string `json:"package"`
Noncestr string `json:"noncestr"`
Timestamp string `json:"timestamp"`
PaySign string `json:"paySign"`
}

type AppletParams struct {
AppId string `json:"appId"`
TimeStamp string `json:"timeStamp"`
NonceStr string `json:"nonceStr"`
Package string `json:"package"`
SignType string `json:"signType"`
PaySign string `json:"paySign"`
}

// ==================================分割==================================
Expand Down Expand Up @@ -178,3 +246,23 @@ type QueryOrder struct {
SceneInfo *SceneInfo `json:"scene_info,omitempty"` // 支付场景描述
PromotionDetail []*PromotionDetail `json:"promotion_detail,omitempty"` // 优惠功能,享受优惠时返回该字段
}

type TradeBill struct {
HashType string `json:"hash_type"`
HashValue string `json:"hash_value"`
DownloadUrl string `json:"download_url"`
}

type BillDetail struct {
BillSequence int `json:"bill_sequence"` // 商户将多个文件按账单文件序号的顺序合并为完整的资金账单文件,起始值为1
HashType string `json:"hash_type"`
HashValue string `json:"hash_value"`
DownloadUrl string `json:"download_url"`
EncryptKey string `json:"encrypt_key"` // 加密账单文件使用的加密密钥。密钥用商户证书的公钥进行加密,然后进行Base64编码
Nonce string `json:"nonce"` // 加密账单文件使用的随机字符串
}

type DownloadBill struct {
DownloadBillCount int `json:"download_bill_count"`
DownloadBillList []*BillDetail `json:"download_bill_list"`
}
Loading

0 comments on commit cf80797

Please sign in to comment.