添加微信支付支持,重构资源创建逻辑,更新支付宝相关配置,移除账单状态字段
This commit is contained in:
117
web/handlers/trade.go
Normal file
117
web/handlers/trade.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
g "platform/web/globals"
|
||||
s "platform/web/services"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
)
|
||||
|
||||
// region AlipayCallback
|
||||
|
||||
func AlipayCallback(c *fiber.Ctx) error {
|
||||
|
||||
// 解析请求
|
||||
req := make(map[string][]string)
|
||||
c.Context().QueryArgs().VisitAll(func(key, value []byte) {
|
||||
req[string(key)] = append(req[string(key)], string(value))
|
||||
})
|
||||
c.Context().PostArgs().VisitAll(func(key, value []byte) {
|
||||
req[string(key)] = append(req[string(key)], string(value))
|
||||
})
|
||||
|
||||
notification, err := g.Alipay.DecodeNotification(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch notification.NotifyType {
|
||||
|
||||
// 支付成功
|
||||
case string(alipay.TradeStatusSuccess):
|
||||
|
||||
if isRefund(notification) {
|
||||
break
|
||||
}
|
||||
|
||||
payment, err := strconv.ParseFloat(notification.TotalAmount, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
paidAt, err := time.Parse("2006-01-02 15:04:05", notification.GmtPayment)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.Resource.CreateResourcePrepared(
|
||||
c.Context(),
|
||||
notification.OutTradeNo,
|
||||
notification.TradeNo,
|
||||
payment,
|
||||
paidAt,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 支付关闭
|
||||
case string(alipay.TradeStatusClosed):
|
||||
|
||||
if isRefund(notification) {
|
||||
break
|
||||
}
|
||||
|
||||
cancelAt, err := time.Parse("2006-01-02 15:04:05", notification.GmtClose)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.Resource.CancelResource(c.Context(), notification.OutTradeNo, cancelAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
default:
|
||||
}
|
||||
|
||||
g.Alipay.ACKNotification(AdapterWriter{
|
||||
c: c,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func isRefund(notification *alipay.Notification) bool {
|
||||
return notification.OutBizNo != "" || notification.RefundFee != "" || notification.GmtRefund != ""
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region WechatPayCallback
|
||||
|
||||
func WechatPayCallback(c *fiber.Ctx) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
Reference in New Issue
Block a user