优化结构
This commit is contained in:
@@ -7,14 +7,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
gopayAlipay "github.com/go-pay/gopay/alipay"
|
||||
"github.com/go-pay/gopay/pkg/xhttp"
|
||||
@@ -39,7 +38,7 @@ type alipayResponseBody struct {
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayAdapter) Create(ctx context.Context, req *system.PaymentRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付宝下单参数为空")
|
||||
}
|
||||
@@ -92,8 +91,8 @@ func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
if status == "success" && strings.TrimSpace(response.TradeNo) == "" {
|
||||
return nil, errors.New("支付宝付款码支付成功响应缺少 trade_no")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: status, TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: status, TradeNo: req.TradeNo,
|
||||
ProviderTradeNo: strings.TrimSpace(response.TradeNo), Payload: sdk.payload(rsp),
|
||||
}, nil
|
||||
case "alipay.trade.precreate":
|
||||
@@ -113,8 +112,8 @@ func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
}
|
||||
// Precreate has no Alipay trade number yet. Keep the QR code in Payload
|
||||
// and query later by the merchant out_trade_no.
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
QueryID: queryID, Payload: sdk.payload(rsp),
|
||||
}, nil
|
||||
case "alipay.trade.app.pay":
|
||||
@@ -125,8 +124,8 @@ func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
if strings.TrimSpace(orderString) == "" {
|
||||
return nil, errors.New("支付宝 APP 支付参数为空")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
Payload: mustMarshalAlipayPayload(map[string]string{"order_string": orderString}),
|
||||
}, nil
|
||||
case "alipay.trade.page.pay":
|
||||
@@ -141,8 +140,8 @@ func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
Payload: mustMarshalAlipayPayload(map[string]string{"pay_url": payURL}),
|
||||
}, nil
|
||||
case "alipay.trade.wap.pay":
|
||||
@@ -157,8 +156,8 @@ func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
Payload: mustMarshalAlipayPayload(map[string]string{"pay_url": payURL}),
|
||||
}, nil
|
||||
default:
|
||||
@@ -174,14 +173,14 @@ func (a *alipayAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
if providerTradeNo == "" {
|
||||
return nil, errors.New("支付宝创建响应缺少 trade_no")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: "created", TradeNo: req.TradeNo,
|
||||
ProviderTradeNo: providerTradeNo, Payload: sdk.payload(rsp),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *alipayAdapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayAdapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*system.PaymentResult, error) {
|
||||
tradeNo = strings.TrimSpace(tradeNo)
|
||||
if tradeNo == "" {
|
||||
return nil, errors.New("支付宝查单缺少 out_trade_no")
|
||||
@@ -196,8 +195,8 @@ func (a *alipayAdapter) Query(ctx context.Context, tradeNo string, c map[string]
|
||||
return nil, err
|
||||
}
|
||||
response := rsp.Response
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: normalizePaymentStatus(response.TradeStatus, "pending"),
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: normalizePaymentStatus(response.TradeStatus, "pending"),
|
||||
TradeNo: strings.TrimSpace(response.OutTradeNo), ProviderTradeNo: strings.TrimSpace(response.TradeNo),
|
||||
Payload: sdk.payload(rsp),
|
||||
}
|
||||
@@ -230,7 +229,7 @@ func (a *alipayAdapter) Query(ctx context.Context, tradeNo string, c map[string]
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *alipayAdapter) Refund(ctx context.Context, req *biz.PaymentRefundRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayAdapter) Refund(ctx context.Context, req *system.PaymentRefundRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付宝退款请求为空")
|
||||
}
|
||||
@@ -258,7 +257,7 @@ func (a *alipayAdapter) Refund(ctx context.Context, req *biz.PaymentRefundReques
|
||||
return alipayRefundResult(req, response, sdk.payload(rsp))
|
||||
}
|
||||
|
||||
func alipayRefundResult(req *biz.PaymentRefundRequest, response *gopayAlipay.TradeRefund, payload []byte) (*biz.PaymentResult, error) {
|
||||
func alipayRefundResult(req *system.PaymentRefundRequest, response *gopayAlipay.TradeRefund, payload []byte) (*system.PaymentResult, error) {
|
||||
if req == nil || response == nil {
|
||||
return nil, errors.New("支付宝退款响应为空")
|
||||
}
|
||||
@@ -298,13 +297,13 @@ func alipayRefundResult(req *biz.PaymentRefundRequest, response *gopayAlipay.Tra
|
||||
if currency == "" {
|
||||
currency = "CNY"
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: "created", TradeNo: tradeNo,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: "created", TradeNo: tradeNo,
|
||||
ProviderTradeNo: providerTradeNo, Amount: amount, Currency: currency, Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *alipayAdapter) Callback(_ context.Context, callback *biz.PaymentCallback, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *alipayAdapter) Callback(_ context.Context, callback *system.PaymentCallback, c map[string]any) (*system.PaymentResult, error) {
|
||||
if callback == nil || len(callback.Body) == 0 {
|
||||
return nil, errors.New("支付宝回调为空")
|
||||
}
|
||||
@@ -320,8 +319,8 @@ func (a *alipayAdapter) Callback(_ context.Context, callback *biz.PaymentCallbac
|
||||
status = "success"
|
||||
}
|
||||
payload, _ := json.Marshal(fields)
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentAlipay, Status: status,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentAlipay, Status: status,
|
||||
TradeNo: first(fields, "out_trade_no"), ProviderTradeNo: first(fields, "trade_no"),
|
||||
Payload: payload,
|
||||
}, nil
|
||||
@@ -482,7 +481,7 @@ func firstNonEmptyAlipay(values ...string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func populateAlipayBreakdown(result *biz.PaymentResult, response map[string]any) error {
|
||||
func populateAlipayBreakdown(result *system.PaymentResult, response map[string]any) error {
|
||||
if result == nil || result.Status != "success" {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user