优化结构
This commit is contained in:
@@ -8,13 +8,12 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
"github.com/go-pay/gopay/pkg/xhttp"
|
||||
gopayWechat "github.com/go-pay/gopay/wechat"
|
||||
@@ -49,7 +48,7 @@ func wechatV2Sign(values map[string]string, key, signType string) string {
|
||||
return gopayWechat.GetReleaseSign(key, wechatV2SignType(signType), body)
|
||||
}
|
||||
|
||||
func (a *wechatV2Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV2Adapter) Create(ctx context.Context, req *system.PaymentRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("微信支付 v2 下单请求为空")
|
||||
}
|
||||
@@ -99,15 +98,15 @@ func (a *wechatV2Adapter) Create(ctx context.Context, req *biz.PaymentRequest, c
|
||||
if err = verifyWechatV2Response(resultValues, client.ApiKey, values["sign_type"]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV2,
|
||||
return &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV2,
|
||||
Status: "created",
|
||||
TradeNo: req.TradeNo,
|
||||
Payload: wechatV2Payload(resultValues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *wechatV2Adapter) createMicropay(ctx context.Context, client *gopayWechat.Client, req *biz.PaymentRequest, c map[string]any, values map[string]string) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV2Adapter) createMicropay(ctx context.Context, client *gopayWechat.Client, req *system.PaymentRequest, c map[string]any, values map[string]string) (*system.PaymentResult, error) {
|
||||
authCode := firstAny(req.Extra, "auth_code", "authcode", "barcode", "pay_code", "payment_code")
|
||||
if authCode == "" {
|
||||
authCode = firstAny(c, "auth_code", "authcode", "barcode", "pay_code", "payment_code")
|
||||
@@ -146,14 +145,14 @@ func (a *wechatV2Adapter) createMicropay(ctx context.Context, client *gopayWecha
|
||||
return nil, errors.New("微信支付 v2 付款码支付响应的 out_trade_no 不匹配")
|
||||
}
|
||||
if strings.EqualFold(resultValues["err_code"], "USERPAYING") {
|
||||
return &biz.PaymentResult{Provider: biz.PaymentWechatV2, Status: "pending", TradeNo: req.TradeNo, Payload: wechatV2Payload(resultValues)}, nil
|
||||
return &system.PaymentResult{Provider: system.PaymentWechatV2, Status: "pending", TradeNo: req.TradeNo, Payload: wechatV2Payload(resultValues)}, nil
|
||||
}
|
||||
providerTradeNo := strings.TrimSpace(resultValues["transaction_id"])
|
||||
if providerTradeNo == "" {
|
||||
return nil, errors.New("微信支付 v2 付款码支付响应缺少 transaction_id")
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV2, Status: "success", TradeNo: req.TradeNo,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV2, Status: "success", TradeNo: req.TradeNo,
|
||||
ProviderTradeNo: providerTradeNo, Payload: wechatV2Payload(resultValues),
|
||||
}
|
||||
amount := strings.TrimSpace(resultValues["total_fee"])
|
||||
@@ -202,7 +201,7 @@ func wechatV2CreateMethod(extra, config map[string]any) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *wechatV2Adapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV2Adapter) Query(ctx context.Context, tradeNo string, c map[string]any) (*system.PaymentResult, error) {
|
||||
tradeNo = strings.TrimSpace(tradeNo)
|
||||
if tradeNo == "" {
|
||||
return nil, errors.New("微信支付 v2 查单缺少 out_trade_no")
|
||||
@@ -227,8 +226,8 @@ func (a *wechatV2Adapter) Query(ctx context.Context, tradeNo string, c map[strin
|
||||
if err = verifyWechatV2Response(values, client.ApiKey, body.GetString("sign_type")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV2,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV2,
|
||||
Status: normalizePaymentStatus(values["trade_state"], "pending"),
|
||||
TradeNo: first(values, "out_trade_no"),
|
||||
Payload: wechatV2Payload(values),
|
||||
@@ -259,7 +258,7 @@ func (a *wechatV2Adapter) Query(ctx context.Context, tradeNo string, c map[strin
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *wechatV2Adapter) Refund(ctx context.Context, req *biz.PaymentRefundRequest, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV2Adapter) Refund(ctx context.Context, req *system.PaymentRefundRequest, c map[string]any) (*system.PaymentResult, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("微信支付 v2 退款请求为空")
|
||||
}
|
||||
@@ -306,8 +305,8 @@ func (a *wechatV2Adapter) Refund(ctx context.Context, req *biz.PaymentRefundRequ
|
||||
if err = validateWechatV2RefundIdentity(tradeNo, refundNo, rsp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &biz.PaymentResult{
|
||||
Provider: biz.PaymentWechatV2,
|
||||
result := &system.PaymentResult{
|
||||
Provider: system.PaymentWechatV2,
|
||||
Status: "created",
|
||||
TradeNo: tradeNo,
|
||||
ProviderTradeNo: strings.TrimSpace(rsp.RefundId),
|
||||
@@ -352,7 +351,7 @@ func validateWechatV2RefundIdentity(tradeNo, refundNo string, rsp *gopayWechat.R
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *wechatV2Adapter) Callback(_ context.Context, callback *biz.PaymentCallback, c map[string]any) (*biz.PaymentResult, error) {
|
||||
func (a *wechatV2Adapter) Callback(_ context.Context, callback *system.PaymentCallback, c map[string]any) (*system.PaymentResult, error) {
|
||||
if callback == nil || len(callback.Body) == 0 {
|
||||
return nil, errors.New("微信支付 v2 回调为空")
|
||||
}
|
||||
@@ -376,7 +375,7 @@ func (a *wechatV2Adapter) Callback(_ context.Context, callback *biz.PaymentCallb
|
||||
status = "success"
|
||||
}
|
||||
payload, _ := json.Marshal(values)
|
||||
return &biz.PaymentResult{Provider: biz.PaymentWechatV2, Status: status, TradeNo: first(values, "out_trade_no"), ProviderTradeNo: values["transaction_id"], Payload: payload}, nil
|
||||
return &system.PaymentResult{Provider: system.PaymentWechatV2, Status: status, TradeNo: first(values, "out_trade_no"), ProviderTradeNo: values["transaction_id"], Payload: payload}, nil
|
||||
}
|
||||
|
||||
func newWechatV2Client(c map[string]any, endpoint string) (*gopayWechat.Client, error) {
|
||||
@@ -467,7 +466,7 @@ func wechatV2Payload(values map[string]string) []byte {
|
||||
return []byte(gopayWechat.GenerateXml(toGoPayBodyMap(values)))
|
||||
}
|
||||
|
||||
func populateWechatV2Breakdown(result *biz.PaymentResult, values map[string]string) error {
|
||||
func populateWechatV2Breakdown(result *system.PaymentResult, values map[string]string) error {
|
||||
if result == nil || result.Status != "success" {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user