更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -29,34 +29,13 @@ import (
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
)
|
||||
|
||||
// WechatChannel 微信支付(JSAPI / Native 扫码)
|
||||
//
|
||||
// 渠道参数(config_json):
|
||||
//
|
||||
// mch_id 商户号
|
||||
// appid 公众号 / 小程序 APPID
|
||||
// cert_serial_no 商户 API 证书序列号
|
||||
// api_v3_key APIv3 密钥
|
||||
// pub_key_id 微信支付公钥 ID(PUB_KEY_ID_ 开头;公钥模式必填,与 pub_key 成对)
|
||||
// pub_key 微信支付公钥 PEM 内容(公钥模式必填)
|
||||
//
|
||||
// 证书(cert_json):
|
||||
//
|
||||
// key_path apiclient_key.pem 上传后的服务端路径(不入库内容,见渠道证书上传接口)
|
||||
type WechatChannel struct{}
|
||||
|
||||
// connectTestOutTradeNo 测试连接用的探测单号:查询不存在的订单,返回 404 即视为鉴权链路通过
|
||||
const connectTestOutTradeNo = "CONNECT_TEST_NO_0000000000000001"
|
||||
|
||||
func (c *WechatChannel) Code() string { return ChannelWechat }
|
||||
func (c *WechatChannel) Name() string { return "微信支付" }
|
||||
|
||||
// buildClient 创建微信支付 APIv3 客户端(带自动证书更新与加解密能力)
|
||||
//
|
||||
// 初始化模式自动选择:
|
||||
// - 配置了「微信支付公钥」(pub_key_id + pub_key)时走公钥模式:
|
||||
// 2024 年后新开通商户默认使用公钥,平台证书下载接口对其停用(403 NOT_ENOUGH);
|
||||
// - 否则回退平台证书模式(自动下载并轮换平台证书,老商户适用)。
|
||||
func (c *WechatChannel) buildClient(cfg *ChannelConfig) (*core.Client, string, error) {
|
||||
mchID := cfg.Get("mch_id")
|
||||
serial := cfg.Get("cert_serial_no")
|
||||
@@ -117,7 +96,6 @@ func (c *WechatChannel) Prepay(ctx context.Context, order *models.PlatformPaymen
|
||||
notifyURL := cfg.CallbackURL
|
||||
amount := &native.Amount{Total: core.Int64(order.Amount), Currency: core.String("CNY")}
|
||||
|
||||
// 公众号内支付
|
||||
if strings.EqualFold(opt.PayType, PayTypeJSAPI) {
|
||||
if opt.OpenID == "" {
|
||||
return nil, fmt.Errorf("微信 JSAPI 支付需要 OpenID")
|
||||
@@ -139,7 +117,6 @@ func (c *WechatChannel) Prepay(ctx context.Context, order *models.PlatformPaymen
|
||||
return nil, fmt.Errorf("微信 JSAPI 下单未返回 prepay_id")
|
||||
}
|
||||
|
||||
// 按微信规范自行生成 JSAPI 调起参数(timeStamp/nonceStr/paySign)
|
||||
ts := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
nonce, _ := randomNonce(16)
|
||||
message := appID + "\n" + ts + "\n" + nonce + "\n" + "prepay_id=" + *resp.PrepayId + "\n"
|
||||
@@ -161,7 +138,6 @@ func (c *WechatChannel) Prepay(ctx context.Context, order *models.PlatformPaymen
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 默认:Native 扫码(PC 收银台)
|
||||
req := native.PrepayRequest{
|
||||
Appid: core.String(appID),
|
||||
Mchid: core.String(mchID),
|
||||
@@ -184,7 +160,6 @@ func (c *WechatChannel) Prepay(ctx context.Context, order *models.PlatformPaymen
|
||||
return &PayParams{Channel: ChannelWechat, PayType: PayTypeQR, CodeURL: *resp.CodeUrl}, nil
|
||||
}
|
||||
|
||||
// normalizeWechatState 微信交易状态 -> 归一化状态
|
||||
func normalizeWechatState(state string) string {
|
||||
switch state {
|
||||
case "SUCCESS":
|
||||
@@ -241,7 +216,6 @@ func (c *WechatChannel) ParseNotify(ctx context.Context, r *http.Request, cfg *C
|
||||
mchID := cfg.Get("mch_id")
|
||||
apiV3Key := cfg.Get("api_v3_key")
|
||||
|
||||
// 验签器按初始化模式选择:公钥模式用微信支付公钥,平台证书模式用下载器维护的证书访问器
|
||||
var verifier auth.Verifier
|
||||
pubKeyID := cfg.Get("pub_key_id")
|
||||
pubKeyPEM := cfg.Get("pub_key")
|
||||
@@ -301,7 +275,7 @@ func (c *WechatChannel) Refund(ctx context.Context, order *models.PlatformPaymen
|
||||
"out_trade_no": order.PayNo,
|
||||
"out_refund_no": refundNo,
|
||||
"reason": reason,
|
||||
"notify_url": "", // 退回结果以主动查询为准;如需退回回调,配置后填入
|
||||
"notify_url": "",
|
||||
"amount": map[string]any{
|
||||
"refund": amount,
|
||||
"total": order.Amount,
|
||||
@@ -329,7 +303,6 @@ func (c *WechatChannel) TestConnect(ctx context.Context, cfg *ChannelConfig) (st
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 公钥模式:/v3/certificates 已对其停用,改查一笔不存在的订单验证鉴权(纯读,不产生交易)
|
||||
if cfg.Get("pub_key_id") != "" && cfg.Get("pub_key") != "" {
|
||||
_, qerr := client.Get(ctx,
|
||||
consts.WechatPayAPIServer+"/v3/pay/transactions/out-trade-no/"+connectTestOutTradeNo+"?mchid="+mchID)
|
||||
@@ -338,12 +311,10 @@ func (c *WechatChannel) TestConnect(ctx context.Context, cfg *ChannelConfig) (st
|
||||
}
|
||||
var apiErr *core.APIError
|
||||
if errors.As(qerr, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
|
||||
// 订单不存在属预期结果:说明请求签名与应答验签链路均已通过
|
||||
return "连接成功:商户私钥签名与微信支付公钥验签均通过(公钥模式)", nil
|
||||
}
|
||||
return "", fmt.Errorf("微信凭证校验失败: %w", qerr)
|
||||
}
|
||||
// 平台证书模式:平台证书接口只做签名校验,不产生任何交易
|
||||
result, err := client.Get(ctx, consts.WechatPayAPIServer+"/v3/certificates")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("微信凭证校验失败: %w", err)
|
||||
@@ -352,8 +323,6 @@ func (c *WechatChannel) TestConnect(ctx context.Context, cfg *ChannelConfig) (st
|
||||
return fmt.Sprintf("连接成功:商户证书与 APIv3 密钥校验通过(平台证书响应 %d 字节)", len(body)), nil
|
||||
}
|
||||
|
||||
/* ---------------- 小工具 ---------------- */
|
||||
|
||||
func valueOrEmpty(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
@@ -361,7 +330,6 @@ func valueOrEmpty(v *string) string {
|
||||
return *v
|
||||
}
|
||||
|
||||
// randomNonce 生成 n 字节随机数的十六进制串
|
||||
func randomNonce(n int) (string, error) {
|
||||
buf := make([]byte, n)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
|
||||
Reference in new issue
Block a user