Files
observer/server/biz/controller/payment.go
T

56 lines
1.8 KiB
Go

package controller
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"observer-server/biz/model/dto"
"observer-server/biz/service"
"observer-server/common"
)
// cPayment 支付回调接口:按渠道应答格式直接返回,不做统一响应包装
type cPayment struct{}
var Payment = &cPayment{}
// WechatNotify 微信支付回调:验签+落授权在回调池执行,结果决定 SUCCESS/FAIL 应答;
// 应答由本 controller 直接写 JSON(微信要求的格式),返回 nil res
func (c *cPayment) WechatNotify(ctx context.Context, req *dto.WechatNotifyReq) (*dto.WechatNotifyRes, error) {
r := g.RequestFromCtx(ctx)
res := &dto.WechatNotifyRes{Code: "SUCCESS"}
err := common.CallbackPoolInstance().Submit(ctx, func(ctx context.Context) error {
return service.Payment.WechatNotify(ctx, r.Request.Header, r.GetBody())
})
if err != nil {
g.Log().Warningf(ctx, "微信回调处理失败: %+v", err)
res.Code = "FAIL"
res.Message = err.Error()
}
r.Response.WriteJson(res)
return nil, nil
}
// AlipayNotify 支付宝回调:直接写 "success"/"failure" 文本(支付宝约定的应答格式),
// 返回 nil res(框架对 nil 不再写响应体)
func (c *cPayment) AlipayNotify(ctx context.Context, req *dto.AlipayNotifyReq) (*dto.AlipayNotifyRes, error) {
r := g.RequestFromCtx(ctx)
if err := r.Request.ParseForm(); err != nil {
g.Log().Warningf(ctx, "支付宝回调表单解析失败: %+v", err)
r.Response.Write("failure")
return nil, nil
}
values := r.Request.PostForm
err := common.CallbackPoolInstance().Submit(ctx, func(ctx context.Context) error {
return service.Payment.AlipayNotify(ctx, values)
})
if err != nil {
g.Log().Warningf(ctx, "支付宝回调处理失败: %+v", err)
r.Response.Write("failure")
return nil, nil
}
r.Response.Write("success")
return nil, nil
}