优化结构
This commit is contained in:
@@ -5,14 +5,13 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
gopayAlipay "github.com/go-pay/gopay/alipay"
|
||||
gopayAlipayV3 "github.com/go-pay/gopay/alipay/v3"
|
||||
@@ -26,7 +25,7 @@ type alipayV3Client struct {
|
||||
gateway *url.URL
|
||||
}
|
||||
|
||||
func (a *alipayV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayV3Adapter) Create(ctx context.Context, req *system.PaymentRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付宝 V3 下单参数为空")
|
||||
}
|
||||
@@ -94,8 +93,8 @@ func (a *alipayV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c
|
||||
if strings.TrimSpace(rsp.QrCode) == "" {
|
||||
return nil, errors.New("支付宝 V3 预创建响应缺少 qr_code")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
QueryID: outTradeNo, Payload: mustJSON(rsp),
|
||||
}, nil
|
||||
case "alipay.trade.app.pay":
|
||||
@@ -106,8 +105,8 @@ func (a *alipayV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c
|
||||
if strings.TrimSpace(orderString) == "" {
|
||||
return nil, errors.New("支付宝 V3 APP 支付参数为空")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
Payload: mustJSON(map[string]string{"order_string": orderString}),
|
||||
}, nil
|
||||
case "alipay.trade.page.pay":
|
||||
@@ -122,8 +121,8 @@ func (a *alipayV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c
|
||||
if strings.TrimSpace(payURL) == "" {
|
||||
return nil, errors.New("支付宝 V3 电脑网站支付 URL 为空")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
Payload: mustJSON(map[string]string{"pay_url": payURL}),
|
||||
}, nil
|
||||
case "alipay.trade.wap.pay":
|
||||
@@ -138,8 +137,8 @@ func (a *alipayV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c
|
||||
if strings.TrimSpace(payURL) == "" {
|
||||
return nil, errors.New("支付宝 V3 手机网站支付 URL 为空")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
Payload: mustJSON(map[string]string{"pay_url": payURL}),
|
||||
}, nil
|
||||
default:
|
||||
@@ -167,14 +166,14 @@ func (a *alipayV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c
|
||||
if providerTradeNo == "" {
|
||||
return nil, errors.New("支付宝 V3 创建响应缺少 trade_no")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "created", TradeNo: req.TradeNo,
|
||||
ProviderTradeNo: providerTradeNo, QueryID: outTradeNo, Payload: mustJSON(rsp),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *alipayV3Adapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayV3Adapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*system.PaymentResult, error) {
|
||||
tradeNo = strings.TrimSpace(tradeNo)
|
||||
if tradeNo == "" {
|
||||
return nil, errors.New("支付宝 V3 查单缺少 out_trade_no")
|
||||
@@ -197,8 +196,8 @@ func (a *alipayV3Adapter) Query(ctx context.Context, tradeNo string, c map[strin
|
||||
if outTradeNo == "" || outTradeNo != tradeNo {
|
||||
return nil, errors.New("支付宝 V3 查询响应的 out_trade_no 缺失或不匹配")
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: normalizePaymentStatus(rsp.TradeStatus, "pending"),
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: normalizePaymentStatus(rsp.TradeStatus, "pending"),
|
||||
TradeNo: outTradeNo, ProviderTradeNo: strings.TrimSpace(rsp.TradeNo),
|
||||
Currency: alipayV3Currency(c, ""), Payload: mustJSON(rsp),
|
||||
}
|
||||
@@ -222,7 +221,7 @@ func (a *alipayV3Adapter) Query(ctx context.Context, tradeNo string, c map[strin
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *alipayV3Adapter) Refund(ctx context.Context, req *biz.PaymentRefundRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayV3Adapter) Refund(ctx context.Context, req *system.PaymentRefundRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付宝 V3 退款请求为空")
|
||||
}
|
||||
@@ -273,14 +272,14 @@ func (a *alipayV3Adapter) Refund(ctx context.Context, req *biz.PaymentRefundRequ
|
||||
if refundedAmount != req.Amount {
|
||||
return nil, errors.New("支付宝 V3 退款响应金额不匹配")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "created", TradeNo: tradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "created", TradeNo: tradeNo,
|
||||
ProviderTradeNo: providerTradeNo, Amount: refundedAmount,
|
||||
Currency: alipayV3Currency(c, req.Currency), Payload: mustJSON(rsp),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *alipayV3Adapter) Callback(_ context.Context, callback *biz.PaymentCallback, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayV3Adapter) Callback(_ context.Context, callback *system.PaymentCallback, c map[string]any) (*system.PaymentResult, error) {
|
||||
if callback == nil || len(callback.Body) == 0 {
|
||||
return nil, errors.New("支付宝 V3 回调为空")
|
||||
}
|
||||
@@ -306,13 +305,13 @@ func (a *alipayV3Adapter) Callback(_ context.Context, callback *biz.PaymentCallb
|
||||
return nil, errors.New("支付宝 V3 回调缺少 out_trade_no 或 trade_no")
|
||||
}
|
||||
payload, _ := json.Marshal(fields)
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: normalizePaymentStatus(fields["trade_status"], "pending"),
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: normalizePaymentStatus(fields["trade_status"], "pending"),
|
||||
TradeNo: tradeNo, ProviderTradeNo: providerTradeNo, Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func alipayV3TradePayResult(req *biz.PaymentRequest, rsp *gopayAlipayV3.TradePayRsp) (*biz.PaymentResult, error) {
|
||||
func alipayV3TradePayResult(req *system.PaymentRequest, rsp *gopayAlipayV3.TradePayRsp) (*system.PaymentResult, error) {
|
||||
if req == nil || rsp == nil {
|
||||
return nil, errors.New("支付宝 V3 付款码支付响应为空")
|
||||
}
|
||||
@@ -331,8 +330,8 @@ func alipayV3TradePayResult(req *biz.PaymentRequest, rsp *gopayAlipayV3.TradePay
|
||||
if amount != req.Amount {
|
||||
return nil, errors.New("支付宝 V3 付款码支付响应金额不匹配")
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipayV3, Status: "success", TradeNo: outTradeNo,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentAlipayV3, Status: "success", TradeNo: outTradeNo,
|
||||
ProviderTradeNo: providerTradeNo, Amount: amount,
|
||||
Currency: strings.ToUpper(strings.TrimSpace(req.Currency)), Payload: mustJSON(rsp),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user