优化结构
This commit is contained in:
@@ -8,20 +8,19 @@ import (
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
gopayWechatV3 "github.com/go-pay/gopay/wechat/v3"
|
||||
)
|
||||
|
||||
type wechatV3Adapter struct{}
|
||||
|
||||
func (a *wechatV3Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV3Adapter) Create(ctx context.Context, req *system.PaymentRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("微信支付 v3 下单请求为空")
|
||||
}
|
||||
@@ -124,7 +123,7 @@ func normalizeWechatV3TradeType(value string) string {
|
||||
return strings.NewReplacer(".", "", "-", "_", " ", "_").Replace(strings.ToLower(strings.TrimSpace(value)))
|
||||
}
|
||||
|
||||
func wechatV3CodePayResult(tradeNo string, rsp *gopayWechatV3.CodePayRsp) (*biz.PaymentResult, error) {
|
||||
func wechatV3CodePayResult(tradeNo string, rsp *gopayWechatV3.CodePayRsp) (*system.PaymentResult, error) {
|
||||
if rsp == nil {
|
||||
return nil, errors.New("微信支付 v3 付款码响应为空")
|
||||
}
|
||||
@@ -140,8 +139,8 @@ func wechatV3CodePayResult(tradeNo string, rsp *gopayWechatV3.CodePayRsp) (*biz.
|
||||
} else if returnedTradeNo != tradeNo {
|
||||
return nil, errors.New("微信支付 v3 付款码响应的商户订单号不匹配")
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV3,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV3,
|
||||
Status: normalizePaymentStatus(order.TradeState, "pending"),
|
||||
TradeNo: tradeNo,
|
||||
ProviderTradeNo: strings.TrimSpace(order.TransactionId),
|
||||
@@ -180,7 +179,7 @@ func wechatV3CodePayResult(tradeNo string, rsp *gopayWechatV3.CodePayRsp) (*biz.
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *wechatV3Adapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV3Adapter) 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")
|
||||
@@ -205,8 +204,8 @@ func (a *wechatV3Adapter) Query(ctx context.Context, tradeNo string, c map[strin
|
||||
|
||||
order := rsp.Response
|
||||
payload := wechatV3ResponsePayload(rsp.SignInfo, order)
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV3,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV3,
|
||||
Status: normalizePaymentStatus(order.TradeState, "pending"),
|
||||
TradeNo: order.OutTradeNo,
|
||||
ProviderTradeNo: order.TransactionId,
|
||||
@@ -237,7 +236,7 @@ func (a *wechatV3Adapter) Query(ctx context.Context, tradeNo string, c map[strin
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *wechatV3Adapter) Refund(ctx context.Context, req *biz.PaymentRefundRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV3Adapter) Refund(ctx context.Context, req *system.PaymentRefundRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("微信支付 v3 退款请求为空")
|
||||
}
|
||||
@@ -280,8 +279,8 @@ func (a *wechatV3Adapter) Refund(ctx context.Context, req *biz.PaymentRefundRequ
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV3,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV3,
|
||||
Status: normalizeRefundState(refund.Status),
|
||||
TradeNo: tradeNo,
|
||||
ProviderTradeNo: refundID,
|
||||
@@ -337,7 +336,7 @@ func validateWechatV3RefundIdentity(refund *gopayWechatV3.RefundOrderResponse, t
|
||||
return refundID, nil
|
||||
}
|
||||
|
||||
func (a *wechatV3Adapter) Callback(ctx context.Context, callback *biz.PaymentCallback, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV3Adapter) Callback(ctx context.Context, callback *system.PaymentCallback, c map[string]any) (*system.PaymentResult, error) {
|
||||
if callback == nil {
|
||||
return nil, errors.New("微信支付 v3 回调为空")
|
||||
}
|
||||
@@ -396,8 +395,8 @@ func (a *wechatV3Adapter) Callback(ctx context.Context, callback *biz.PaymentCal
|
||||
return nil, errors.New("微信支付 v3 回调 appid 不匹配")
|
||||
}
|
||||
plain, _ := json.Marshal(pay)
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV3,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV3,
|
||||
Status: status,
|
||||
TradeNo: pay.OutTradeNo,
|
||||
ProviderTradeNo: pay.TransactionId,
|
||||
@@ -491,22 +490,22 @@ func wechatV3PlatformSerial(certificate string) (string, error) {
|
||||
return strings.ToUpper(cert.SerialNumber.Text(16)), nil
|
||||
}
|
||||
|
||||
func wechatV3CreateResult(tradeNo string, code int, signInfo *gopayWechatV3.SignInfo, response any, errResponse gopayWechatV3.ErrResponse, rawError string) (*biz.PaymentResult, error) {
|
||||
func wechatV3CreateResult(tradeNo string, code int, signInfo *gopayWechatV3.SignInfo, response any, errResponse gopayWechatV3.ErrResponse, rawError string) (*system.PaymentResult, error) {
|
||||
if code != gopayWechatV3.Success {
|
||||
return nil, wechatV3APIError("下单", code, errResponse, rawError)
|
||||
}
|
||||
if response == nil {
|
||||
return nil, errors.New("微信支付 v3 下单响应缺少预支付信息")
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV3,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV3,
|
||||
Status: "created",
|
||||
TradeNo: tradeNo,
|
||||
Payload: wechatV3ResponsePayload(signInfo, response),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func wechatV3PrepayCreateResult(client *gopayWechatV3.ClientV3, appID, tradeType, tradeNo string, rsp *gopayWechatV3.PrepayRsp) (*biz.PaymentResult, error) {
|
||||
func wechatV3PrepayCreateResult(client *gopayWechatV3.ClientV3, appID, tradeType, tradeNo string, rsp *gopayWechatV3.PrepayRsp) (*system.PaymentResult, error) {
|
||||
if rsp == nil {
|
||||
return nil, errors.New("微信支付 v3 下单响应为空")
|
||||
}
|
||||
@@ -553,7 +552,7 @@ func wechatV3ResponsePayload(signInfo *gopayWechatV3.SignInfo, response any) []b
|
||||
return payload
|
||||
}
|
||||
|
||||
func populateWechatV3Breakdown(result *biz.PaymentResult, object map[string]any) error {
|
||||
func populateWechatV3Breakdown(result *system.PaymentResult, object map[string]any) error {
|
||||
if result == nil || result.Status != "success" {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user