Skip to content

Commit 429e2bb

Browse files
committed
refactoring
1 parent 6e2c5a3 commit 429e2bb

28 files changed

+1092
-1061
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# gopay
2+
3+
Golang语言实现的支付库
4+
5+
## 支持的支付方式
6+
7+
目前支持微信公众号,微信app,微信小程序,支付宝网页版,支付宝app。要是谁有新的支付方式也可以合并。
8+
9+
## 使用方法
10+
11+
参考:pay_test.go

README.org

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,96 @@
1-
package client
1+
package alipay
22

33
import (
44
"encoding/json"
55
"errors"
66
"fmt"
77
"time"
88

9+
"github.com/webx-top/gopay/client"
910
"github.com/webx-top/gopay/common"
1011
)
1112

12-
var defaultAliAppClient *AliAppClient
13+
var defaultApp *App
1314

14-
type AliAppClient struct {
15-
*AliPayClient
15+
type App struct {
16+
*PayClient
1617
SellerID string //合作者ID
17-
//AppID string // 应用ID
18-
//PrivateKey *rsa.PrivateKey
19-
//PublicKey *rsa.PublicKey
2018
}
2119

22-
func InitAliAppClient(c *AliAppClient) {
23-
defaultAliAppClient = c
20+
func InitApp(c *App) {
21+
defaultApp = c
2422
}
2523

26-
// DefaultAliAppClient 得到默认支付宝app客户端
27-
func DefaultAliAppClient() *AliAppClient {
28-
return defaultAliAppClient
24+
// DefaultApp 得到默认支付宝app客户端
25+
func DefaultApp() *App {
26+
return defaultApp
2927
}
3028

31-
func (this *AliAppClient) Pay(charge *common.Charge) (map[string]string, error) {
29+
func NewApp() *App {
30+
return &App{PayClient: New()}
31+
}
32+
33+
func (a *App) Pay(charge *common.Charge) (map[string]string, error) {
3234
var m = make(map[string]string)
3335
var bizContent = make(map[string]string)
34-
m["app_id"] = this.AppID
36+
m["app_id"] = a.AppID
3537
m["method"] = "alipay.trade.app.pay"
3638
m["format"] = "JSON"
3739
m["charset"] = "utf-8"
3840
m["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
3941
m["version"] = "1.0"
4042
m["notify_url"] = charge.CallbackURL
41-
m["sign_type"] = this.AliPayClient.RSAType
42-
bizContent["subject"] = TruncatedText(charge.Describe, 32)
43+
m["sign_type"] = a.RSAType
44+
bizContent["subject"] = client.TruncatedText(charge.Describe, 32)
4345
bizContent["out_trade_no"] = charge.TradeNum
4446
bizContent["product_code"] = "QUICK_MSECURITY_PAY"
45-
bizContent["total_amount"] = AliyunMoneyFeeToString(charge.MoneyFee)
47+
bizContent["total_amount"] = MoneyFeeToString(charge.MoneyFee)
4648

47-
bizContentJson, err := json.Marshal(bizContent)
49+
bizContentJSON, err := json.Marshal(bizContent)
4850
if err != nil {
4951
return map[string]string{}, errors.New("json.Marshal: " + err.Error())
5052
}
51-
m["biz_content"] = string(bizContentJson)
53+
m["biz_content"] = string(bizContentJSON)
5254

53-
m["sign"] = this.GenSign(m)
55+
m["sign"] = a.GenSign(m)
5456

55-
return map[string]string{"orderString": this.ToURL(m)}, nil
57+
return map[string]string{"orderString": a.ToURL(m)}, nil
5658
}
5759

58-
func (this *AliAppClient) CloseOrder(charge *common.Charge) (map[string]string, error) {
60+
func (a *App) CloseOrder(charge *common.Charge) (map[string]string, error) {
5961
return map[string]string{}, errors.New("暂未开发该功能")
6062
}
6163

62-
func (this *AliAppClient) PayToClient(charge *common.Charge) (map[string]string, error) {
64+
func (a *App) PayToClient(charge *common.Charge) (map[string]string, error) {
6365
return map[string]string{}, errors.New("暂未开发该功能")
6466
}
6567

66-
// 订单查询
67-
func (this *AliAppClient) QueryOrder(outTradeNo string) (common.AliWebAppQueryResult, error) {
68+
// QueryOrder 订单查询
69+
func (a *App) QueryOrder(outTradeNo string) (AppQueryResult, error) {
6870
var m = make(map[string]string)
6971
m["method"] = "alipay.trade.query"
70-
m["app_id"] = this.AppID
72+
m["app_id"] = a.AppID
7173
m["format"] = "JSON"
7274
m["charset"] = "utf-8"
7375
m["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
7476
m["version"] = "1.0"
75-
m["sign_type"] = this.AliPayClient.RSAType
77+
m["sign_type"] = a.RSAType
7678
bizContent := map[string]string{"out_trade_no": outTradeNo}
77-
bizContentJson, err := json.Marshal(bizContent)
79+
bizContentJSON, err := json.Marshal(bizContent)
7880
if err != nil {
79-
return common.AliWebAppQueryResult{}, errors.New("json.Marshal: " + err.Error())
81+
return AppQueryResult{}, errors.New("json.Marshal: " + err.Error())
8082
}
81-
m["biz_content"] = string(bizContentJson)
82-
sign := this.GenSign(m)
83+
m["biz_content"] = string(bizContentJSON)
84+
sign := a.GenSign(m)
8385
m["sign"] = sign
8486

85-
url := fmt.Sprintf("%s?%s", "https://openapi.alipay.com/gateway.do", this.ToURL(m))
87+
url := fmt.Sprintf("%s?%s", a.OpenAPIURL, a.ToURL(m))
8688

87-
return GetAlipayApp(url)
89+
return GetApp(url)
8890
}
8991

9092
//// GenSign 产生签名
91-
//func (this *AliAppClient) GenSign(m map[string]string) string {
93+
//func (a *App) GenSign(m map[string]string) string {
9294
// var data []string
9395
//
9496
// for k, v := range m {
@@ -105,7 +107,7 @@ func (this *AliAppClient) QueryOrder(outTradeNo string) (common.AliWebAppQueryRe
105107
// panic(err)
106108
// }
107109
// hashByte := s.Sum(nil)
108-
// signByte, err := this.PrivateKey.Sign(rand.Reader, hashByte, crypto.SHA1)
110+
// signByte, err := a.PrivateKey.Sign(rand.Reader, hashByte, crypto.SHA1)
109111
// if err != nil {
110112
// panic(err)
111113
// }
@@ -114,7 +116,7 @@ func (this *AliAppClient) QueryOrder(outTradeNo string) (common.AliWebAppQueryRe
114116
//}
115117
//
116118
////CheckSign 检测签名
117-
//func (this *AliAppClient) CheckSign(signData, sign string) {
119+
//func (a *App) CheckSign(signData, sign string) {
118120
// signByte, err := base64.StdEncoding.DecodeString(sign)
119121
// if err != nil {
120122
// panic(err)
@@ -125,14 +127,14 @@ func (this *AliAppClient) QueryOrder(outTradeNo string) (common.AliWebAppQueryRe
125127
// panic(err)
126128
// }
127129
// hash := s.Sum(nil)
128-
// err = rsa.VerifyPKCS1v15(this.PublicKey, crypto.SHA1, hash, signByte)
130+
// err = rsa.VerifyPKCS1v15(a.PublicKey, crypto.SHA1, hash, signByte)
129131
// if err != nil {
130132
// panic(err)
131133
// }
132134
//}
133135
//
134136
//// ToURL
135-
//func (this *AliAppClient) ToURL(m map[string]string) string {
137+
//func (a *App) ToURL(m map[string]string) string {
136138
// var buf []string
137139
// for k, v := range m {
138140
// buf = append(buf, fmt.Sprintf("%s=%s", k, url.QueryEscape(v)))

0 commit comments

Comments
 (0)