42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
package weixin
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
|
)
|
|
|
|
type Server struct {
|
|
MchID string `json:"mch_id"`
|
|
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"`
|
|
MchAPIv3Key string `json:"mch_ap_iv_3_key"`
|
|
PrivateKey string `json:"private_key"`
|
|
PrivateKeyPath string `json:"private_key_path"`
|
|
CertificatePath string `json:"certificate_path"`
|
|
}
|
|
|
|
func newClient(ctx context.Context, c *Server) (*core.Client, error) {
|
|
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(c.PrivateKeyPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("MchID[%s] load merchant private key error:%v", c.MchID, err)
|
|
}
|
|
opts := []core.ClientOption{
|
|
option.WithWechatPayAutoAuthCipher(c.MchID, c.MchCertificateSerialNumber, mchPrivateKey, c.MchAPIv3Key),
|
|
}
|
|
if c.CertificatePath != "" {
|
|
payCert, err := utils.LoadCertificateWithPath(c.CertificatePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("MchID[%s] load pay certificate error:%v", c.MchID, err)
|
|
}
|
|
opts = append(opts, option.WithWechatPayCertificate([]*x509.Certificate{payCert}))
|
|
}
|
|
client, err := core.NewClient(ctx, opts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("MchID[%s] %v", c.MchID, err)
|
|
}
|
|
return client, nil
|
|
}
|