parent
81250f625a
commit
6511308ae7
@ -1,95 +0,0 @@
|
|||||||
package wechatpayapiv3
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go.dtapp.net/library/utils/golog"
|
|
||||||
"go.dtapp.net/library/utils/gomongo"
|
|
||||||
"go.dtapp.net/library/utils/gorequest"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// App 微信支付直连商户
|
|
||||||
type App struct {
|
|
||||||
appId string // 小程序或者公众号唯一凭证
|
|
||||||
appSecret string // 小程序或者公众号唯一凭证密钥
|
|
||||||
mchId string // 微信支付的商户id
|
|
||||||
aesKey string // 私钥
|
|
||||||
apiV3 string // API v3密钥
|
|
||||||
mchSslSerialNo string // pem 证书号
|
|
||||||
mchSslKey string // pem key 内容
|
|
||||||
mongo *gomongo.Client // 日志数据库
|
|
||||||
pgsql *gorm.DB // pgsql数据库
|
|
||||||
client *gorequest.App // 请求客户端
|
|
||||||
log *golog.Api // 日志服务
|
|
||||||
logTableName string // 日志表名
|
|
||||||
logStatus bool // 日志状态
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewApp 实例化
|
|
||||||
func NewApp(appId string, appSecret string, mchId string, aesKey string, apiV3 string, mchSslSerialNo string, mchSslKey string, pgsql *gorm.DB) *App {
|
|
||||||
app := &App{appId: appId, appSecret: appSecret, mchId: mchId, aesKey: aesKey, apiV3: apiV3, mchSslSerialNo: mchSslSerialNo, mchSslKey: mchSslKey}
|
|
||||||
app.client = gorequest.NewHttp()
|
|
||||||
if pgsql != nil {
|
|
||||||
app.pgsql = pgsql
|
|
||||||
app.logStatus = true
|
|
||||||
app.logTableName = "wechatpayapiv3"
|
|
||||||
app.log = golog.NewApi(&golog.ApiConfig{
|
|
||||||
Db: pgsql,
|
|
||||||
TableName: app.logTableName,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return app
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) request(url string, params map[string]interface{}, method string, commonParams bool) (resp gorequest.Response, err error) {
|
|
||||||
|
|
||||||
// 公共参数
|
|
||||||
if method == http.MethodPost {
|
|
||||||
if commonParams == true {
|
|
||||||
params["appid"] = app.appId
|
|
||||||
params["mchid"] = app.mchId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 认证
|
|
||||||
authorization, err := app.authorization(method, params, url)
|
|
||||||
if err != nil {
|
|
||||||
return gorequest.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建请求
|
|
||||||
client := app.client
|
|
||||||
|
|
||||||
// 设置请求地址
|
|
||||||
client.SetUri(url)
|
|
||||||
|
|
||||||
// 设置方式
|
|
||||||
client.SetMethod(method)
|
|
||||||
|
|
||||||
// 设置JSON格式
|
|
||||||
client.SetContentTypeJson()
|
|
||||||
|
|
||||||
// 设置参数
|
|
||||||
client.SetParams(params)
|
|
||||||
|
|
||||||
// 设置头部
|
|
||||||
client.SetHeader("Authorization", "WECHATPAY2-SHA256-RSA2048 "+authorization)
|
|
||||||
client.SetHeader("Accept", "application/json")
|
|
||||||
client.SetHeader("Accept-Language", "zh-CN")
|
|
||||||
if url == "https://api.mch.weixin.qq.com/v3/merchant-service/complaints-v2" {
|
|
||||||
client.SetHeader("Wechatpay-Serial", app.mchSslSerialNo)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发起请求
|
|
||||||
request, err := client.Request()
|
|
||||||
if err != nil {
|
|
||||||
return gorequest.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 日志
|
|
||||||
if app.logStatus == true {
|
|
||||||
go app.postgresqlLog(request)
|
|
||||||
}
|
|
||||||
|
|
||||||
return request, err
|
|
||||||
}
|
|
@ -0,0 +1,48 @@
|
|||||||
|
package wechatpayapiv3
|
||||||
|
|
||||||
|
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
|
||||||
|
AesKey string // 私钥
|
||||||
|
ApiV3 string // API v3密钥
|
||||||
|
MchSslSerialNo string // pem 证书号
|
||||||
|
MchSslKey string // pem key 内容
|
||||||
|
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 wechatpayapiv3
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiUrl = "https://api.mch.weixin.qq.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
logTable = "wechatpayapiv3"
|
||||||
|
)
|
@ -1,5 +1,17 @@
|
|||||||
package wechatpayapiv3
|
package wechatpayapiv3
|
||||||
|
|
||||||
func (app *App) GetAppId() string {
|
func (c *Client) GetAppId() string {
|
||||||
return app.appId
|
return c.config.AppId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetMchId() string {
|
||||||
|
return c.config.MchId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetMchSslKey() string {
|
||||||
|
return c.config.MchSslKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetMchSslSerialNo() string {
|
||||||
|
return c.config.MchSslSerialNo
|
||||||
}
|
}
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
package wechatpayapiv3
|
|
||||||
|
|
||||||
// Params 请求参数
|
|
||||||
type Params map[string]interface{}
|
|
||||||
|
|
||||||
func NewParams() Params {
|
|
||||||
p := make(Params)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) NewParamsWith(params ...Params) Params {
|
|
||||||
p := make(Params)
|
|
||||||
for _, v := range params {
|
|
||||||
p.SetParams(v)
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Params) Set(key string, value any) {
|
|
||||||
p[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Params) SetParams(params Params) {
|
|
||||||
for key, value := range params {
|
|
||||||
p[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,59 @@
|
|||||||
|
package wechatpayapiv3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.dtapp.net/library/utils/gorequest"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) request(url string, params map[string]interface{}, method string, commonParams bool) (gorequest.Response, error) {
|
||||||
|
|
||||||
|
// 公共参数
|
||||||
|
if method == http.MethodPost {
|
||||||
|
if commonParams == true {
|
||||||
|
params["appid"] = c.GetAppId()
|
||||||
|
params["mchid"] = c.GetMchId()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 认证
|
||||||
|
authorization, err := c.authorization(method, params, url)
|
||||||
|
if err != nil {
|
||||||
|
return gorequest.Response{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建请求
|
||||||
|
client := c.client
|
||||||
|
|
||||||
|
// 设置请求地址
|
||||||
|
client.SetUri(url)
|
||||||
|
|
||||||
|
// 设置方式
|
||||||
|
client.SetMethod(method)
|
||||||
|
|
||||||
|
// 设置JSON格式
|
||||||
|
client.SetContentTypeJson()
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
client.SetParams(params)
|
||||||
|
|
||||||
|
// 设置头部
|
||||||
|
client.SetHeader("Authorization", "WECHATPAY2-SHA256-RSA2048 "+authorization)
|
||||||
|
client.SetHeader("Accept", "application/json")
|
||||||
|
client.SetHeader("Accept-Language", "zh-CN")
|
||||||
|
if url == "https://api.mch.weixin.qq.com/v3/merchant-service/complaints-v2" {
|
||||||
|
client.SetHeader("Wechatpay-Serial", c.GetMchSslSerialNo())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
request, err := client.Request()
|
||||||
|
if err != nil {
|
||||||
|
return gorequest.Response{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 日志
|
||||||
|
if c.logStatus == true {
|
||||||
|
go c.postgresqlLog(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
return request, err
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
package wechatpayapiv3
|
package wechatpayapiv3
|
||||||
|
|
||||||
func (app *App) SetAppId(appId string) {
|
func (c *Client) SetAppId(appId string) {
|
||||||
app.appId = appId
|
c.config.AppId = appId
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) SetAppSecret(appSecret string) {
|
func (c *Client) SetAppSecret(appSecret string) {
|
||||||
app.appSecret = appSecret
|
c.config.AppSecret = appSecret
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue