parent
405aa99fd0
commit
81250f625a
@ -1,76 +0,0 @@
|
|||||||
package wechatpayapiv2
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"go.dtapp.net/library/utils/golog"
|
|
||||||
"go.dtapp.net/library/utils/gomongo"
|
|
||||||
"go.dtapp.net/library/utils/gorequest"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// App 微信支付服务
|
|
||||||
type App struct {
|
|
||||||
appId string // 小程序或者公众号唯一凭证
|
|
||||||
appSecret string // 小程序或者公众号唯一凭证密钥
|
|
||||||
mchId string // 微信支付的商户id
|
|
||||||
mchKey string // 私钥
|
|
||||||
certString string
|
|
||||||
keyString string
|
|
||||||
mongo *gomongo.Client // 日志数据库
|
|
||||||
pgsql *gorm.DB // pgsql数据库
|
|
||||||
client *gorequest.App // 请求客户端
|
|
||||||
log *golog.Api // 日志服务
|
|
||||||
logTableName string // 日志表名
|
|
||||||
logStatus bool // 日志状态
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApp(appId string, appSecret string, mchId string, mchKey string, certString string, keyString string, pgsql *gorm.DB) *App {
|
|
||||||
app := &App{appId: appId, appSecret: appSecret, mchId: mchId, mchKey: mchKey, certString: certString, keyString: keyString}
|
|
||||||
app.client = gorequest.NewHttp()
|
|
||||||
if pgsql != nil {
|
|
||||||
app.pgsql = pgsql
|
|
||||||
app.logStatus = true
|
|
||||||
app.logTableName = "wechatpayapiv2"
|
|
||||||
app.log = golog.NewApi(&golog.ApiConfig{
|
|
||||||
Db: pgsql,
|
|
||||||
TableName: app.logTableName,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return app
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) request(url string, params map[string]interface{}, cert *tls.Certificate) (resp gorequest.Response, err error) {
|
|
||||||
|
|
||||||
// 创建请求
|
|
||||||
client := app.client
|
|
||||||
|
|
||||||
// 设置请求地址
|
|
||||||
client.SetUri(url)
|
|
||||||
|
|
||||||
// 设置格式
|
|
||||||
client.SetContentTypeXml()
|
|
||||||
|
|
||||||
// 设置参数
|
|
||||||
client.SetParams(params)
|
|
||||||
|
|
||||||
// 设置证书
|
|
||||||
client.SetP12Cert(cert)
|
|
||||||
|
|
||||||
// 发起请求
|
|
||||||
request, err := client.Post()
|
|
||||||
if err != nil {
|
|
||||||
return gorequest.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 日志
|
|
||||||
if app.logStatus == true {
|
|
||||||
go app.postgresqlLog(request)
|
|
||||||
}
|
|
||||||
|
|
||||||
return request, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) P12ToPem() (*tls.Certificate, error) {
|
|
||||||
pemCert, err := tls.X509KeyPair([]byte(app.certString), []byte(app.keyString))
|
|
||||||
return &pemCert, err
|
|
||||||
}
|
|
@ -0,0 +1,47 @@
|
|||||||
|
package wechatpayapiv2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.dtapp.net/library/utils/golog"
|
||||||
|
"go.dtapp.net/library/utils/gomongo"
|
||||||
|
"go.dtapp.net/library/utils/gorequest"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigClient struct {
|
||||||
|
AppId string // 小程序或者公众号唯一凭证
|
||||||
|
AppSecret string // 小程序或者公众号唯一凭证密钥
|
||||||
|
MchId string // 微信支付的商户id
|
||||||
|
MchKey string // 私钥
|
||||||
|
CertString string
|
||||||
|
KeyString string
|
||||||
|
MongoDb *gomongo.Client // 日志数据库
|
||||||
|
PgsqlDb *gorm.DB // pgsql数据库
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client 微信支付服务
|
||||||
|
type Client struct {
|
||||||
|
client *gorequest.App // 请求客户端
|
||||||
|
log *golog.ApiClient // 日志服务
|
||||||
|
logStatus bool // 日志状态
|
||||||
|
config *ConfigClient // 配置
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(config *ConfigClient) (*Client, error) {
|
||||||
|
|
||||||
|
var err error
|
||||||
|
c := &Client{config: config}
|
||||||
|
|
||||||
|
c.client = gorequest.NewHttp()
|
||||||
|
if c.config.PgsqlDb != nil {
|
||||||
|
c.logStatus = true
|
||||||
|
c.log, err = golog.NewApiClient(&golog.ConfigApiClient{
|
||||||
|
Db: c.config.PgsqlDb,
|
||||||
|
TableName: logTable,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package wechatpayapiv2
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiUrl = "https://api.mch.weixin.qq.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
logTable = "wechatpayapiv2"
|
||||||
|
)
|
@ -0,0 +1,28 @@
|
|||||||
|
package wechatpayapiv2
|
||||||
|
|
||||||
|
import "crypto/tls"
|
||||||
|
|
||||||
|
func (c *Client) GetAppId() string {
|
||||||
|
return c.config.AppId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetMchId() string {
|
||||||
|
return c.config.MchId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetMchKey() string {
|
||||||
|
return c.config.MchKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetCertString() string {
|
||||||
|
return c.config.CertString
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetKeyString() string {
|
||||||
|
return c.config.KeyString
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) P12ToPem() (*tls.Certificate, error) {
|
||||||
|
pemCert, err := tls.X509KeyPair([]byte(c.GetCertString()), []byte(c.GetKeyString()))
|
||||||
|
return &pemCert, err
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package wechatpayapiv2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"go.dtapp.net/library/utils/gorequest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) request(url string, params map[string]interface{}, cert *tls.Certificate) (gorequest.Response, error) {
|
||||||
|
|
||||||
|
// 创建请求
|
||||||
|
client := c.client
|
||||||
|
|
||||||
|
// 设置请求地址
|
||||||
|
client.SetUri(url)
|
||||||
|
|
||||||
|
// 设置格式
|
||||||
|
client.SetContentTypeXml()
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
client.SetParams(params)
|
||||||
|
|
||||||
|
// 设置证书
|
||||||
|
client.SetP12Cert(cert)
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
request, err := client.Post()
|
||||||
|
if err != nil {
|
||||||
|
return gorequest.Response{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 日志
|
||||||
|
if c.logStatus == true {
|
||||||
|
go c.postgresqlLog(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
return request, err
|
||||||
|
}
|
Loading…
Reference in new issue