重构交易相关逻辑,优化异步任务处理,更新支付状态查询逻辑
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/valyala/fasthttp/fasthttpadaptor"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
trade2 "platform/web/domains/trade"
|
||||
@@ -13,13 +14,25 @@ import (
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"github.com/valyala/fasthttp/fasthttpadaptor"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||||
)
|
||||
|
||||
// region TradeCheckSSE
|
||||
|
||||
func TradeCheckSSE(c *fiber.Ctx) error {
|
||||
// 设置响应头
|
||||
c.Set("Content-Type", "text/event-stream")
|
||||
c.Set("Cache-Control", "no-cache")
|
||||
c.Set("Connection", "keep-alive")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region AlipayCallback
|
||||
|
||||
func AlipayCallback(c *fiber.Ctx) error {
|
||||
func AlipayCallback(c *fiber.Ctx) (err error) {
|
||||
|
||||
// 解析请求
|
||||
httpRequest := new(http.Request)
|
||||
@@ -36,21 +49,34 @@ func AlipayCallback(c *fiber.Ctx) error {
|
||||
|
||||
slog.Debug("支付宝支付回调", "notification", fmt.Sprintf("%+v", notification))
|
||||
|
||||
// todo 退款通知
|
||||
if isRefund(notification) {
|
||||
return act(c)
|
||||
}
|
||||
|
||||
// 查询交易信息
|
||||
trade, err := q.Q.Trade.Where(q.Trade.InnerNo.Eq(notification.OutTradeNo)).Take()
|
||||
if err != nil {
|
||||
// 跳过测试通知
|
||||
return act(c)
|
||||
return c.SendString("success")
|
||||
}
|
||||
switch notification.NotifyType {
|
||||
switch alipay.TradeStatus(notification.NotifyType) {
|
||||
|
||||
// 等待支付
|
||||
case alipay.TradeStatusWaitBuyerPay:
|
||||
// 不需要处理
|
||||
|
||||
// 支付关闭
|
||||
case alipay.TradeStatusClosed:
|
||||
switch trade2.Type(trade.Type) {
|
||||
|
||||
// 购买产品
|
||||
case trade2.TypePurchase:
|
||||
err = s.Resource.CancelResource(notification.OutTradeNo, time.Now(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 余额充值
|
||||
case trade2.TypeRecharge:
|
||||
}
|
||||
|
||||
// 支付成功
|
||||
case string(alipay.TradeStatusSuccess):
|
||||
case alipay.TradeStatusSuccess:
|
||||
|
||||
// 收集交易状态
|
||||
payment, err := decimal.NewFromString(notification.TotalAmount)
|
||||
@@ -61,7 +87,7 @@ func AlipayCallback(c *fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
verified := &s.TransactionVerifyResult{
|
||||
verified := &s.TradeSuccessResult{
|
||||
TransId: notification.TradeNo,
|
||||
Payment: payment,
|
||||
Time: paidAt,
|
||||
@@ -83,48 +109,18 @@ func AlipayCallback(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
// 支付关闭
|
||||
case string(alipay.TradeStatusClosed):
|
||||
switch trade2.Type(trade.Type) {
|
||||
|
||||
// 购买产品
|
||||
case trade2.TypePurchase:
|
||||
err = s.Resource.CancelResource(notification.OutTradeNo, time.Now(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
}
|
||||
// 交易结束
|
||||
case alipay.TradeStatusFinished:
|
||||
// 结束交易状态
|
||||
}
|
||||
|
||||
return act(c)
|
||||
}
|
||||
|
||||
type AdapterWriter struct {
|
||||
c *fiber.Ctx
|
||||
}
|
||||
|
||||
func (a AdapterWriter) Header() http.Header {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterWriter) Write(bytes []byte) (int, error) {
|
||||
return a.c.Write(bytes)
|
||||
}
|
||||
|
||||
func (a AdapterWriter) WriteHeader(statusCode int) {
|
||||
a.c.Status(statusCode)
|
||||
return c.SendString("success")
|
||||
}
|
||||
|
||||
func isRefund(notification *alipay.Notification) bool {
|
||||
return notification.OutBizNo != "" || notification.RefundFee != "" || notification.GmtRefund != ""
|
||||
}
|
||||
|
||||
func act(c *fiber.Ctx) error {
|
||||
g.Alipay.ACKNotification(AdapterWriter{c: c})
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region WechatPayCallback
|
||||
@@ -162,7 +158,7 @@ func WechatPayCallback(c *fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
verified := &s.TransactionVerifyResult{
|
||||
verified := &s.TradeSuccessResult{
|
||||
TransId: *content.TransactionId,
|
||||
Payment: payment,
|
||||
Time: paidAt,
|
||||
|
||||
Reference in New Issue
Block a user