优化结构
This commit is contained in:
@@ -10,11 +10,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/pkg/paymentkit"
|
||||
)
|
||||
|
||||
@@ -37,7 +37,7 @@ func newVendorAdapter(provider string, profile vendorProfile) Adapter {
|
||||
return &vendorPaymentAdapter{provider: provider, profile: profile}
|
||||
}
|
||||
|
||||
func (a *vendorPaymentAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *vendorPaymentAdapter) Create(ctx context.Context, req *system.PaymentRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("配置驱动支付下单请求为空")
|
||||
}
|
||||
@@ -45,16 +45,16 @@ func (a *vendorPaymentAdapter) Create(ctx context.Context, req *biz.PaymentReque
|
||||
mergeMap(payload, req.Extra, "merchant_id", "app_id", "trade_no", "subject", "amount", "currency", "notify_url", "client_ip", "timestamp", "nonce")
|
||||
return a.call(ctx, "create_url", payload, req.TradeNo, c)
|
||||
}
|
||||
func (a *vendorPaymentAdapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *vendorPaymentAdapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*system.PaymentResult, error) {
|
||||
return a.call(ctx, "query_url", map[string]any{"merchant_id": text(c, "merchant_id"), "app_id": text(c, "app_id"), "trade_no": tradeNo, "timestamp": time.Now().Unix(), "nonce": nonce()}, tradeNo, c)
|
||||
}
|
||||
func (a *vendorPaymentAdapter) Refund(ctx context.Context, req *biz.PaymentRefundRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *vendorPaymentAdapter) Refund(ctx context.Context, req *system.PaymentRefundRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付退款请求为空")
|
||||
}
|
||||
return a.call(ctx, "refund_url", map[string]any{"merchant_id": text(c, "merchant_id"), "app_id": text(c, "app_id"), "trade_no": req.TradeNo, "refund_no": req.RefundNo, "amount": req.Amount, "total_amount": req.TotalAmount, "currency": req.Currency, "timestamp": time.Now().Unix(), "nonce": nonce()}, req.TradeNo, c)
|
||||
}
|
||||
func (a *vendorPaymentAdapter) Callback(_ context.Context, callback *biz.PaymentCallback, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *vendorPaymentAdapter) Callback(_ context.Context, callback *system.PaymentCallback, c map[string]any) (*system.PaymentResult, error) {
|
||||
if callback == nil || len(callback.Body) == 0 {
|
||||
return nil, errors.New("配置驱动支付回调为空")
|
||||
}
|
||||
@@ -98,9 +98,9 @@ func (a *vendorPaymentAdapter) Callback(_ context.Context, callback *biz.Payment
|
||||
status = "success"
|
||||
}
|
||||
payload, _ := json.Marshal(fields)
|
||||
return &biz.PaymentResult{Provider: a.provider, Status: status, TradeNo: tradeNo, ProviderTradeNo: providerTradeNo, Payload: payload}, nil
|
||||
return &system.PaymentResult{Provider: a.provider, Status: status, TradeNo: tradeNo, ProviderTradeNo: providerTradeNo, Payload: payload}, nil
|
||||
}
|
||||
func (a *vendorPaymentAdapter) call(ctx context.Context, endpointKey string, payload map[string]any, tradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *vendorPaymentAdapter) call(ctx context.Context, endpointKey string, payload map[string]any, tradeNo string, c map[string]any) (*system.PaymentResult, error) {
|
||||
endpoint := text(c, endpointKey)
|
||||
if endpoint == "" {
|
||||
return nil, fmt.Errorf("%s 未配置 %s", a.provider, endpointKey)
|
||||
@@ -131,7 +131,7 @@ func (a *vendorPaymentAdapter) call(ctx context.Context, endpointKey string, pay
|
||||
if response.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("%s HTTP %d", a.provider, response.StatusCode)
|
||||
}
|
||||
result := &biz.PaymentResult{Provider: a.provider, Status: "created", TradeNo: tradeNo, Payload: ensureJSON(body)}
|
||||
result := &system.PaymentResult{Provider: a.provider, Status: "created", TradeNo: tradeNo, Payload: ensureJSON(body)}
|
||||
if endpointKey != "query_url" {
|
||||
return result, nil
|
||||
}
|
||||
@@ -167,7 +167,7 @@ func (a *vendorPaymentAdapter) call(ctx context.Context, endpointKey string, pay
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func populateVendorBreakdown(result *biz.PaymentResult, object map[string]any, c map[string]any) error {
|
||||
func populateVendorBreakdown(result *system.PaymentResult, object map[string]any, c map[string]any) error {
|
||||
if result == nil || result.Status != "success" {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user