新增系列配置

This commit is contained in:
yan.y
2024-10-12 14:56:25 +08:00
parent e112772826
commit ef52ecc2be
17 changed files with 586 additions and 104 deletions
+91 -11
View File
@@ -2,28 +2,48 @@ package utils
import (
"fmt"
"github.com/bwmarrin/snowflake"
"github.com/dgb8901/go-wechat-miniapp-sdk/config"
"github.com/dgb8901/go-wechat-miniapp-sdk/models/response"
"github.com/dgb8901/go-wechat-miniapp-sdk/service"
"github.com/spf13/viper"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
"go.uber.org/zap"
"golang.org/x/net/context"
"log"
"pet-house.com/core/g"
"pet-house.com/core/server/viper_server"
"pet-house.com/core/server/web"
"pet-house.com/core/server/zap_server"
)
var CONFIG = WxConfig{
AppId: "",
Secret: "",
Token: "",
AesKey: "",
MsgDataFormat: "",
AppId: "",
Secret: "",
Token: "",
AesKey: "",
MsgDataFormat: "",
MchId: "",
MchCertificateSerialNumber: "",
MchAPIv3Key: "",
PrivateKeyPath: "",
WechatCert: "",
}
type WxConfig struct {
AppId string `mapstructure:"appId" json:"appId" yaml:"appId"`
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"`
Token string `mapstructure:"token" json:"token" yaml:"token"`
AesKey string `mapstructure:"aesKey" json:"aesKey" yaml:"aesKey"`
MsgDataFormat string `mapstructure:"msgDataFormat" json:"msgDataFormat" yaml:"msgDataFormat"`
AppId string `mapstructure:"appId" json:"appId" yaml:"appId"`
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"`
Token string `mapstructure:"token" json:"token" yaml:"token"`
AesKey string `mapstructure:"aesKey" json:"aesKey" yaml:"aesKey"`
MsgDataFormat string `mapstructure:"msgDataFormat" json:"msgDataFormat" yaml:"msgDataFormat"`
MchId string `mapstructure:"mchId" json:"mchId" yaml:"mchId"`
MchCertificateSerialNumber string `mapstructure:"mchCertificateSerialNumber" json:"mchCertificateSerialNumber" yaml:"mchCertificateSerialNumber"`
MchAPIv3Key string `mapstructure:"mchAPIv3Key" json:"mchAPIv3Key" yaml:"mchAPIv3Key"`
PrivateKeyPath string `mapstructure:"privateKeyPath" json:"privateKeyPath" yaml:"privateKeyPath"`
WechatCert string `mapstructure:"wechatCert" json:"wechatCert" yaml:"wechatCert"`
}
// getViperConfig get viper config
@@ -49,12 +69,18 @@ func getViperConfig() viper_server.ViperConfig {
"token": "` + CONFIG.Token + `",
"aesKey": "` + CONFIG.AesKey + `",
"msgDataFormat": ` + CONFIG.MsgDataFormat + `
"mchId": ` + CONFIG.MchId + `
"mchCertificateSerialNumber": ` + CONFIG.MchCertificateSerialNumber + `
"mchAPIv3Key": ` + CONFIG.MchAPIv3Key + `
"privateKeyPath": ` + CONFIG.PrivateKeyPath + `
"wechatCert": ` + CONFIG.WechatCert + `
}`),
}
}
type WechatHelper struct {
wechatService *service.WxaService
wechatService *service.WxaService
wechatPayClient *core.Client
}
var wxHelper = &WechatHelper{}
@@ -71,12 +97,36 @@ func WechatInit() {
wxaConfig := config.NewInMemory(cfg)
wxaService := service.NewService(wxaConfig)
wxHelper.wechatService = wxaService
getPayApi()
}
func getPayApi() {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(CONFIG.PrivateKeyPath)
if err != nil {
log.Fatal("load merchant private key error")
}
ctx := context.Background()
// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(CONFIG.MchId, CONFIG.MchCertificateSerialNumber, mchPrivateKey, CONFIG.MchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Fatalf("new wechat pay client err:%s", err)
}
wxHelper.wechatPayClient = client
}
func getWxaService() *service.WxaService {
return wxHelper.wechatService
}
func GetWxPayService() *core.Client {
return wxHelper.wechatPayClient
}
type WxUserInfo struct {
OpenId string
UnionId string
@@ -89,3 +139,33 @@ func GetWxUserInfo(code string) (*response.JsCode2SessionResult, error) {
}
return info, nil
}
func WxPayRefund(payId string, payOrderId string, orderAmount int64, ctx context.Context) {
ras := refunddomestic.RefundsApiService{Client: wxHelper.wechatPayClient}
var refundNo = new(string)
reason := "取消订单"
currency := "CNY"
notifyUrl := web.CONFIG.System.Domain + "/pet-house/pay/payNotify"
amount := refunddomestic.AmountReq{
Refund: &orderAmount,
Total: &orderAmount,
Currency: &currency,
}
var NextId, _ = snowflake.NewNode(1)
*refundNo = NextId.Generate().String()
var req = refunddomestic.CreateRequest{
TransactionId: &payId,
OutTradeNo: &payOrderId,
OutRefundNo: refundNo,
Reason: &reason,
NotifyUrl: &notifyUrl,
Amount: &amount,
}
createResult, _, err := ras.Create(ctx, req)
if err != nil {
return
}
zap_server.ZAPLOG.Info("WxPayRefund", zap.Any("request", req), zap.Any("response", createResult))
}