优化结构
This commit is contained in:
@@ -5,12 +5,11 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kra/internal/biz/system"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
"github.com/go-pay/gopay/paypal"
|
||||
)
|
||||
@@ -32,7 +31,7 @@ func (a *paypalAdapter) client(c map[string]any) (*paypal.Client, error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (a *paypalAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *paypalAdapter) Create(ctx context.Context, req *system.PaymentRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("PayPal 下单参数为空")
|
||||
}
|
||||
@@ -75,7 +74,7 @@ func (a *paypalAdapter) Create(ctx context.Context, req *biz.PaymentRequest, c m
|
||||
return paypalCreateResult(req, order, c)
|
||||
}
|
||||
|
||||
func (a *paypalAdapter) Query(ctx context.Context, orderID string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *paypalAdapter) Query(ctx context.Context, orderID string, c map[string]any) (*system.PaymentResult, error) {
|
||||
orderID = strings.TrimSpace(orderID)
|
||||
if orderID == "" {
|
||||
return nil, errors.New("PayPal 查单缺少 order ID")
|
||||
@@ -154,7 +153,7 @@ func paypalAutoCaptureEnabled(c map[string]any) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *paypalAdapter) Refund(ctx context.Context, req *biz.PaymentRefundRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *paypalAdapter) Refund(ctx context.Context, req *system.PaymentRefundRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("PayPal 退款请求为空")
|
||||
}
|
||||
@@ -202,7 +201,7 @@ func (a *paypalAdapter) Refund(ctx context.Context, req *biz.PaymentRefundReques
|
||||
return paypalRefundResult(req, rsp.Response, c)
|
||||
}
|
||||
|
||||
func paypalRefundResult(req *biz.PaymentRefundRequest, refund *paypal.PaymentCaptureRefund, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func paypalRefundResult(req *system.PaymentRefundRequest, refund *paypal.PaymentCaptureRefund, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil || refund == nil {
|
||||
return nil, errors.New("PayPal 退款响应为空")
|
||||
}
|
||||
@@ -216,8 +215,8 @@ func paypalRefundResult(req *biz.PaymentRefundRequest, refund *paypal.PaymentCap
|
||||
return nil, errors.New("PayPal 退款响应缺少 refund ID")
|
||||
}
|
||||
currency := strings.ToUpper(strings.TrimSpace(req.Currency))
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentPayPal,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentPayPal,
|
||||
Status: normalizePayPalRefundState(refund.Status),
|
||||
TradeNo: req.TradeNo,
|
||||
ProviderTradeNo: providerRefundID,
|
||||
@@ -247,7 +246,7 @@ func paypalRefundResult(req *biz.PaymentRefundRequest, refund *paypal.PaymentCap
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func paypalCreateResult(req *biz.PaymentRequest, order *paypal.OrderDetail, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func paypalCreateResult(req *system.PaymentRequest, order *paypal.OrderDetail, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil || order == nil {
|
||||
return nil, errors.New("PayPal 下单响应为空")
|
||||
}
|
||||
@@ -278,7 +277,7 @@ func paypalCreateResult(req *biz.PaymentRequest, order *paypal.OrderDetail, c ma
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *paypalAdapter) Callback(ctx context.Context, callback *biz.PaymentCallback, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *paypalAdapter) Callback(ctx context.Context, callback *system.PaymentCallback, c map[string]any) (*system.PaymentResult, error) {
|
||||
if callback == nil || len(callback.Body) == 0 {
|
||||
return nil, errors.New("PayPal 回调为空")
|
||||
}
|
||||
@@ -326,7 +325,7 @@ func (a *paypalAdapter) Callback(ctx context.Context, callback *biz.PaymentCallb
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func paypalApplicationContext(req *biz.PaymentRequest, c map[string]any) map[string]any {
|
||||
func paypalApplicationContext(req *system.PaymentRequest, c map[string]any) map[string]any {
|
||||
applicationContext := map[string]any{}
|
||||
if configured, ok := req.Extra["application_context"].(map[string]any); ok {
|
||||
for key, value := range configured {
|
||||
@@ -353,7 +352,7 @@ func paypalApplicationContext(req *biz.PaymentRequest, c map[string]any) map[str
|
||||
return applicationContext
|
||||
}
|
||||
|
||||
func paypalOrderResult(order *paypal.OrderDetail, queryID, fallbackTradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func paypalOrderResult(order *paypal.OrderDetail, queryID, fallbackTradeNo string, c map[string]any) (*system.PaymentResult, error) {
|
||||
if order == nil {
|
||||
return nil, errors.New("PayPal 订单结果为空")
|
||||
}
|
||||
@@ -365,8 +364,8 @@ func paypalOrderResult(order *paypal.OrderDetail, queryID, fallbackTradeNo strin
|
||||
tradeNo = fallbackTradeNo
|
||||
}
|
||||
capture := paypalOrderCapture(order)
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentPayPal,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentPayPal,
|
||||
Status: paypalOrderCaptureState(order),
|
||||
TradeNo: tradeNo,
|
||||
QueryID: queryID,
|
||||
@@ -499,7 +498,7 @@ func paypalOrderAmount(order *paypal.OrderDetail, c map[string]any) (int64, stri
|
||||
return total, currency, found, nil
|
||||
}
|
||||
|
||||
func paypalRefundCaptureID(ctx context.Context, client *paypal.Client, req *biz.PaymentRefundRequest) (string, error) {
|
||||
func paypalRefundCaptureID(ctx context.Context, client *paypal.Client, req *system.PaymentRefundRequest) (string, error) {
|
||||
if req == nil {
|
||||
return "", errors.New("PayPal 退款请求为空")
|
||||
}
|
||||
@@ -534,7 +533,7 @@ func paypalRefundCaptureID(ctx context.Context, client *paypal.Client, req *biz.
|
||||
return capture.Id, nil
|
||||
}
|
||||
|
||||
func paypalWebhookResult(event *paypal.WebhookEvent) (*biz.PaymentResult, error) {
|
||||
func paypalWebhookResult(event *paypal.WebhookEvent) (*system.PaymentResult, error) {
|
||||
var resource map[string]any
|
||||
if err := json.Unmarshal(event.Resource, &resource); err != nil {
|
||||
return nil, fmt.Errorf("PayPal 回调 resource 无效: %w", err)
|
||||
@@ -568,8 +567,8 @@ func paypalWebhookResult(event *paypal.WebhookEvent) (*biz.PaymentResult, error)
|
||||
if tradeNo == "" || queryID == "" {
|
||||
return nil, errors.New("PayPal 回调缺少商户订单号或 order ID")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentPayPal,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentPayPal,
|
||||
Status: status,
|
||||
TradeNo: tradeNo,
|
||||
ProviderTradeNo: providerTradeNo,
|
||||
@@ -588,7 +587,7 @@ func paypalPurchaseUnitTradeNo(resource map[string]any) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func paypalCallbackHeader(callback *biz.PaymentCallback, name string) string {
|
||||
func paypalCallbackHeader(callback *system.PaymentCallback, name string) string {
|
||||
for key, value := range callback.Headers {
|
||||
if strings.EqualFold(key, name) {
|
||||
return strings.TrimSpace(value)
|
||||
|
||||
Reference in New Issue
Block a user