Files
sundynix-micro-be/app/plant/api/internal/logic/callback/wechatPayCallbackLogic.go
T
2026-05-23 13:55:05 +08:00

80 lines
2.3 KiB
Go

// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package callback
import (
"context"
"encoding/json"
"io"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"sundynix-micro-go/app/plant/api/internal/svc"
)
type WechatPayCallbackLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 微信支付回调
func NewWechatPayCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WechatPayCallbackLogic {
return &WechatPayCallbackLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// wechatNotify 微信支付回调通知结构
type wechatNotify struct {
Id string `json:"id"`
EventType string `json:"event_type"`
Summary string `json:"summary"`
ResourceType string `json:"resource_type"`
Resource struct {
Algorithm string `json:"algorithm"`
Ciphertext string `json:"ciphertext"`
AssociatedData string `json:"associated_data"`
Nonce string `json:"nonce"`
} `json:"resource"`
}
// WechatPayCallback 微信支付回调处理
// TODO: 接入 wechatpay-go SDK 完成验签 + 解密 + 订单状态更新
//
// 完整接入步骤:
// 1. go get github.com/wechatpay-apiv3/wechatpay-go
// 2. 在 config 中添加 WechatPay{ MchId, MchAPIv3Key, PrivateKeyPath, PublicKeyId, PublicKeyPath }
// 3. 使用 notify.NewRSANotifyHandler 验签解密
// 4. 解析 payments.Transaction 后根据 TradeState == "SUCCESS" 更新 sundynix_exchange_order 状态
func (l *WechatPayCallbackLogic) WechatPayCallback(r *http.Request) error {
// 1. 读取请求体
body, err := io.ReadAll(r.Body)
if err != nil {
l.Logger.Errorf("[微信支付回调] 读取请求体失败: %v", err)
return err
}
defer r.Body.Close()
// 2. 解析通知基本结构(不验签,记录日志)
var notify wechatNotify
if err = json.Unmarshal(body, &notify); err != nil {
l.Logger.Errorf("[微信支付回调] 解析通知失败: %v, body: %s", err, string(body))
return err
}
l.Logger.Infof("[微信支付回调] 收到通知 id=%s, event=%s, summary=%s",
notify.Id, notify.EventType, notify.Summary)
// TODO: 完成验签 + 解密 + 业务处理
// if notify.EventType == "TRANSACTION.SUCCESS" {
// transaction := decrypt(notify.Resource, mchAPIv3Key)
// updateOrderStatus(transaction.OutTradeNo, "SUCCESS")
// }
return nil
}