v2.0
This commit is contained in:
		@@ -73,15 +73,7 @@ func (a *AliPayService) CreateAlipayAppOrder(amount float64, subject string, out
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateAlipayH5Order 创建支付宝H5支付订单
 | 
			
		||||
func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outTradeNo string, brand string) (string, error) {
 | 
			
		||||
	var returnURL string
 | 
			
		||||
	if brand == "tyc" {
 | 
			
		||||
		returnURL = "https://www.tianyuancha.com/report"
 | 
			
		||||
	} else if brand == "qnc" {
 | 
			
		||||
		returnURL = "https://www.quannengcha.com/report"
 | 
			
		||||
	} else {
 | 
			
		||||
		returnURL = a.config.ReturnURL
 | 
			
		||||
	}
 | 
			
		||||
func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outTradeNo string) (string, error) {
 | 
			
		||||
	client := a.AlipayClient
 | 
			
		||||
	totalAmount := lzUtils.ToAlipayAmount(amount)
 | 
			
		||||
	// 构造H5支付请求
 | 
			
		||||
@@ -92,7 +84,7 @@ func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outT
 | 
			
		||||
			TotalAmount: totalAmount,
 | 
			
		||||
			ProductCode: "QUICK_WAP_PAY",    // H5支付专用产品码
 | 
			
		||||
			NotifyURL:   a.config.NotifyUrl, // 异步回调通知地址
 | 
			
		||||
			ReturnURL:   returnURL,
 | 
			
		||||
			ReturnURL:   a.config.ReturnURL,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	// 获取H5支付请求字符串,这里会签名
 | 
			
		||||
@@ -105,7 +97,7 @@ func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outT
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateAlipayOrder 根据平台类型创建支付宝支付订单
 | 
			
		||||
func (a *AliPayService) CreateAlipayOrder(ctx context.Context, amount float64, subject string, outTradeNo string, brand string) (string, error) {
 | 
			
		||||
func (a *AliPayService) CreateAlipayOrder(ctx context.Context, amount float64, subject string, outTradeNo string) (string, error) {
 | 
			
		||||
	// 根据 ctx 中的 platform 判断平台
 | 
			
		||||
	platform, platformOk := ctx.Value("platform").(string)
 | 
			
		||||
	if !platformOk {
 | 
			
		||||
@@ -117,7 +109,7 @@ func (a *AliPayService) CreateAlipayOrder(ctx context.Context, amount float64, s
 | 
			
		||||
		return a.CreateAlipayAppOrder(amount, subject, outTradeNo)
 | 
			
		||||
	case "h5":
 | 
			
		||||
		// 调用H5支付的创建方法,并传入 returnUrl
 | 
			
		||||
		return a.CreateAlipayH5Order(amount, subject, outTradeNo, brand)
 | 
			
		||||
		return a.CreateAlipayH5Order(amount, subject, outTradeNo)
 | 
			
		||||
	default:
 | 
			
		||||
		return "", fmt.Errorf("不支持的支付平台: %s", platform)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -210,6 +210,7 @@ var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, err
 | 
			
		||||
	"G10XM02":             (*ApiRequestService).ProcessG10XM02Request,
 | 
			
		||||
	"G11BJ06":             (*ApiRequestService).ProcessG11BJ06Request,
 | 
			
		||||
	"G29BJ05":             (*ApiRequestService).ProcessG29BJ05Request,
 | 
			
		||||
	"Marriage":            (*ApiRequestService).ProcessMarriageRequest,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PreprocessRequestApi 调用指定的请求处理函数
 | 
			
		||||
@@ -1854,6 +1855,21 @@ func (a *ApiRequestService) ProcessRIS031Request(params []byte) ([]byte, error)
 | 
			
		||||
 | 
			
		||||
	return []byte(Value.Raw), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ProcessMarriageRequest 婚姻状态查询,优先使用西部数据G09XM02接口,失败时回退到羽山IDV044接口
 | 
			
		||||
func (a *ApiRequestService) ProcessMarriageRequest(params []byte) ([]byte, error) {
 | 
			
		||||
	// 首先尝试使用西部数据接口查询
 | 
			
		||||
	resp, err := a.ProcessG09XM02Request(params)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// 西部数据接口查询失败,尝试使用羽山接口
 | 
			
		||||
		logx.Infof("西部数据婚姻查询失败,尝试使用羽山接口: %v", err)
 | 
			
		||||
		resp, err = a.ProcessIDV044Request(params)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
func (a *ApiRequestService) ProcessG09XM02Request(params []byte) ([]byte, error) {
 | 
			
		||||
	idCard := gjson.GetBytes(params, "id_card")
 | 
			
		||||
	name := gjson.GetBytes(params, "name")
 | 
			
		||||
@@ -1895,7 +1911,6 @@ func (a *ApiRequestService) ProcessG09XM02Request(params []byte) ([]byte, error)
 | 
			
		||||
	response := map[string]string{
 | 
			
		||||
		"status": statusCode,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 序列化为JSON
 | 
			
		||||
	jsonResponse, err := json.Marshal(response)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -7,12 +7,13 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"encoding/pem"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/golang-jwt/jwt/v4"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/config"
 | 
			
		||||
 | 
			
		||||
	"github.com/golang-jwt/jwt/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ApplePayService 是 Apple IAP 支付服务的结构体
 | 
			
		||||
 
 | 
			
		||||
@@ -4,10 +4,11 @@ package service
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/hibiken/asynq"
 | 
			
		||||
	"github.com/zeromicro/go-zero/core/logx"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/config"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/types"
 | 
			
		||||
 | 
			
		||||
	"github.com/hibiken/asynq"
 | 
			
		||||
	"github.com/zeromicro/go-zero/core/logx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type AsynqService struct {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,14 @@ package service
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/config"
 | 
			
		||||
	"tydata-server/app/user/model"
 | 
			
		||||
	"tydata-server/common/ctxdata"
 | 
			
		||||
	"tydata-server/pkg/lzkit/lzUtils"
 | 
			
		||||
 | 
			
		||||
	"github.com/wechatpay-apiv3/wechatpay-go/core"
 | 
			
		||||
	"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
 | 
			
		||||
	"github.com/wechatpay-apiv3/wechatpay-go/core/downloader"
 | 
			
		||||
@@ -14,13 +22,6 @@ import (
 | 
			
		||||
	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
 | 
			
		||||
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
 | 
			
		||||
	"github.com/zeromicro/go-zero/core/logx"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/config"
 | 
			
		||||
	"tydata-server/app/user/model"
 | 
			
		||||
	"tydata-server/common/ctxdata"
 | 
			
		||||
	"tydata-server/pkg/lzkit/lzUtils"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -33,6 +34,14 @@ const (
 | 
			
		||||
	TradeStatePayError   = "PAYERROR"   // 支付失败(其他原因,如银行返回失败)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// InitType 初始化类型
 | 
			
		||||
type InitType string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	InitTypePlatformCert InitType = "platform_cert" // 平台证书初始化
 | 
			
		||||
	InitTypeWxPayPubKey  InitType = "wxpay_pubkey"  // 微信支付公钥初始化
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type WechatPayService struct {
 | 
			
		||||
	config        config.WxpayConfig
 | 
			
		||||
	wechatClient  *core.Client
 | 
			
		||||
@@ -40,25 +49,83 @@ type WechatPayService struct {
 | 
			
		||||
	userAuthModel model.UserAuthModel
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewWechatPayService 初始化微信支付服务
 | 
			
		||||
func NewWechatPayService(c config.Config, userAuthModel model.UserAuthModel) *WechatPayService {
 | 
			
		||||
// NewWechatPayService 创建微信支付服务实例
 | 
			
		||||
func NewWechatPayService(c config.Config, userAuthModel model.UserAuthModel, initType InitType) *WechatPayService {
 | 
			
		||||
	switch initType {
 | 
			
		||||
	case InitTypePlatformCert:
 | 
			
		||||
		return newWechatPayServiceWithPlatformCert(c, userAuthModel)
 | 
			
		||||
	case InitTypeWxPayPubKey:
 | 
			
		||||
		return newWechatPayServiceWithWxPayPubKey(c, userAuthModel)
 | 
			
		||||
	default:
 | 
			
		||||
		logx.Errorf("不支持的初始化类型: %s", initType)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %s", initType))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// newWechatPayServiceWithPlatformCert 使用平台证书初始化微信支付服务
 | 
			
		||||
func newWechatPayServiceWithPlatformCert(c config.Config, userAuthModel model.UserAuthModel) *WechatPayService {
 | 
			
		||||
	// 从配置中加载商户信息
 | 
			
		||||
	mchID := c.Wxpay.MchID
 | 
			
		||||
	mchCertificateSerialNumber := c.Wxpay.MchCertificateSerialNumber
 | 
			
		||||
	mchAPIv3Key := c.Wxpay.MchApiv3Key
 | 
			
		||||
	mchPublicKeyID := c.Wxpay.MchPublicKeyID
 | 
			
		||||
 | 
			
		||||
	// 从文件中加载商户私钥
 | 
			
		||||
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath(c.Wxpay.MchPrivateKeyPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logx.Errorf("加载商户私钥失败: %v", err)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err)) // 记录错误并停止程序
 | 
			
		||||
	}
 | 
			
		||||
	// 从文件中加载公钥
 | 
			
		||||
	mchPublicKey, err := utils.LoadPublicKeyWithPath(c.Wxpay.MchPublicKeyPath)
 | 
			
		||||
 | 
			
		||||
	// 使用商户私钥和其他参数初始化微信支付客户端
 | 
			
		||||
	opts := []core.ClientOption{
 | 
			
		||||
		option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
 | 
			
		||||
	}
 | 
			
		||||
	client, err := core.NewClient(context.Background(), opts...)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logx.Errorf("加载商户私钥失败: %v", err)
 | 
			
		||||
		logx.Errorf("创建微信支付客户端失败: %v", err)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err)) // 记录错误并停止程序
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 在初始化时获取证书访问器并创建 notifyHandler
 | 
			
		||||
	certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
 | 
			
		||||
	notifyHandler, err := notify.NewRSANotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logx.Errorf("获取证书访问器失败: %v", err)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logx.Infof("微信支付客户端初始化成功(平台证书方式)")
 | 
			
		||||
	return &WechatPayService{
 | 
			
		||||
		config:        c.Wxpay,
 | 
			
		||||
		wechatClient:  client,
 | 
			
		||||
		notifyHandler: notifyHandler,
 | 
			
		||||
		userAuthModel: userAuthModel,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// newWechatPayServiceWithWxPayPubKey 使用微信支付公钥初始化微信支付服务
 | 
			
		||||
func newWechatPayServiceWithWxPayPubKey(c config.Config, userAuthModel model.UserAuthModel) *WechatPayService {
 | 
			
		||||
	// 从配置中加载商户信息
 | 
			
		||||
	mchID := c.Wxpay.MchID
 | 
			
		||||
	mchCertificateSerialNumber := c.Wxpay.MchCertificateSerialNumber
 | 
			
		||||
	mchAPIv3Key := c.Wxpay.MchApiv3Key
 | 
			
		||||
	mchPrivateKeyPath := c.Wxpay.MchPrivateKeyPath
 | 
			
		||||
	mchPublicKeyID := c.Wxpay.MchPublicKeyID
 | 
			
		||||
	mchPublicKeyPath := c.Wxpay.MchPublicKeyPath
 | 
			
		||||
	// 从文件中加载商户私钥
 | 
			
		||||
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath(mchPrivateKeyPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logx.Errorf("加载商户私钥失败: %v", err)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 从文件中加载微信支付平台证书
 | 
			
		||||
	mchPublicKey, err := utils.LoadPublicKeyWithPath(mchPublicKeyPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logx.Errorf("加载微信支付平台证书失败: %v", err)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 使用商户私钥和其他参数初始化微信支付客户端
 | 
			
		||||
	opts := []core.ClientOption{
 | 
			
		||||
		option.WithWechatPayPublicKeyAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchPublicKeyID, mchPublicKey),
 | 
			
		||||
@@ -66,18 +133,14 @@ func NewWechatPayService(c config.Config, userAuthModel model.UserAuthModel) *We
 | 
			
		||||
	client, err := core.NewClient(context.Background(), opts...)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logx.Errorf("创建微信支付客户端失败: %v", err)
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err)) // 记录错误并停止程序
 | 
			
		||||
		panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
 | 
			
		||||
	}
 | 
			
		||||
	// 在初始化时获取证书访问器并创建 notifyHandler
 | 
			
		||||
	certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
 | 
			
		||||
	//notifyHandler, err := notify.NewRSANotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
 | 
			
		||||
	//if err != nil {
 | 
			
		||||
	//	logx.Errorf("获取证书访问器失败: %v", err)
 | 
			
		||||
	//	panic(fmt.Sprintf("初始化失败,服务停止: %v", err)) // 记录错误并停止程序
 | 
			
		||||
	//}
 | 
			
		||||
 | 
			
		||||
	// 初始化 notify.Handler
 | 
			
		||||
	notifyHandler := notify.NewNotifyHandler(
 | 
			
		||||
		mchAPIv3Key,
 | 
			
		||||
		verifiers.NewSHA256WithRSACombinedVerifier(certificateVisitor, mchPublicKeyID, *mchPublicKey))
 | 
			
		||||
		verifiers.NewSHA256WithRSAPubkeyVerifier(mchPublicKeyID, *mchPublicKey))
 | 
			
		||||
	logx.Infof("微信支付客户端初始化成功(微信支付公钥方式)")
 | 
			
		||||
	return &WechatPayService{
 | 
			
		||||
		config:        c.Wxpay,
 | 
			
		||||
		wechatClient:  client,
 | 
			
		||||
@@ -141,7 +204,6 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 返回预支付交易会话标识
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -165,6 +227,9 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
 | 
			
		||||
			return "", findAuthModelErr
 | 
			
		||||
		}
 | 
			
		||||
		prepayData, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, userAuthModel.AuthKey)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
	case "h5-weixin":
 | 
			
		||||
		userID, getUidErr := ctxdata.GetUidFromCtx(ctx)
 | 
			
		||||
		if getUidErr != nil {
 | 
			
		||||
@@ -175,6 +240,9 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
 | 
			
		||||
			return "", findAuthModelErr
 | 
			
		||||
		}
 | 
			
		||||
		prepayData, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, userAuthModel.AuthKey)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
	case "app":
 | 
			
		||||
		// 如果是 APP 平台,调用 APP 支付订单创建
 | 
			
		||||
		prepayData, err = w.CreateWechatAppOrder(ctx, amount, description, outTradeNo)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,13 +4,14 @@ import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/pkg/errors"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/config"
 | 
			
		||||
	"tydata-server/pkg/lzkit/crypto"
 | 
			
		||||
 | 
			
		||||
	"github.com/pkg/errors"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type WestResp struct {
 | 
			
		||||
 
 | 
			
		||||
@@ -9,12 +9,13 @@ import (
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/tidwall/gjson"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
	"tydata-server/app/user/cmd/api/internal/config"
 | 
			
		||||
 | 
			
		||||
	"github.com/tidwall/gjson"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type YushanService struct {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user