支付相关
This commit is contained in:
@@ -10,6 +10,16 @@ type PaymentConfigRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Config json.RawMessage `json:"config"`
|
||||
}
|
||||
|
||||
// PaymentConfigResponse is the transport representation returned by the
|
||||
// payment configuration endpoint. Values are already masked by the usecase's
|
||||
// repository boundary.
|
||||
type PaymentConfigResponse struct {
|
||||
Provider string `json:"Provider"`
|
||||
Enabled bool `json:"Enabled"`
|
||||
Values json.RawMessage `json:"Values"`
|
||||
}
|
||||
|
||||
type PaymentRequest struct {
|
||||
Provider string `json:"provider"`
|
||||
TradeNo string `json:"tradeNo"`
|
||||
@@ -35,6 +45,44 @@ type PaymentRefundRequest struct {
|
||||
Amount int64 `json:"amount"`
|
||||
}
|
||||
|
||||
// PaymentCallbackRequest is populated by the HTTP transport and converted to
|
||||
// the biz callback object inside service.
|
||||
type PaymentCallbackRequest struct {
|
||||
Provider string
|
||||
Headers map[string]string
|
||||
Body []byte
|
||||
Query map[string]string
|
||||
}
|
||||
|
||||
type PaymentCallbackAck struct {
|
||||
StatusCode int
|
||||
ContentType string
|
||||
Body []byte
|
||||
}
|
||||
|
||||
type PaymentResultResponse struct {
|
||||
Provider string `json:"provider"`
|
||||
Status string `json:"status"`
|
||||
TradeNo string `json:"tradeNo"`
|
||||
ProviderTradeNo string `json:"providerTradeNo"`
|
||||
Amount int64 `json:"amount"`
|
||||
PayerPaidAmount int64 `json:"payerPaidAmount,omitempty"`
|
||||
CashPaidAmount int64 `json:"cashPaidAmount,omitempty"`
|
||||
PointPaidAmount int64 `json:"pointPaidAmount,omitempty"`
|
||||
DiscountAmount int64 `json:"discountAmount,omitempty"`
|
||||
ProviderDiscountAmount int64 `json:"providerDiscountAmount,omitempty"`
|
||||
MerchantDiscountAmount int64 `json:"merchantDiscountAmount,omitempty"`
|
||||
SettlementAmount int64 `json:"settlementAmount,omitempty"`
|
||||
Currency string `json:"currency"`
|
||||
PayerCurrency string `json:"payerCurrency,omitempty"`
|
||||
AmountBreakdownKnown bool `json:"amountBreakdownKnown"`
|
||||
Duplicate bool `json:"duplicate"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
OrderStatus string `json:"orderStatus,omitempty"`
|
||||
FulfillmentStatus string `json:"fulfillmentStatus,omitempty"`
|
||||
RefundStatus string `json:"refundStatus,omitempty"`
|
||||
}
|
||||
|
||||
type PaymentOrderResponse struct {
|
||||
Provider string `json:"provider"`
|
||||
TradeNo string `json:"tradeNo"`
|
||||
|
||||
+83
-12
@@ -2,6 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/service/dto"
|
||||
)
|
||||
@@ -9,11 +11,25 @@ import (
|
||||
type PaymentService struct{ uc *biz.PaymentUsecase }
|
||||
|
||||
func NewPaymentService(uc *biz.PaymentUsecase) *PaymentService { return &PaymentService{uc: uc} }
|
||||
func (s *PaymentService) Configs(ctx context.Context) ([]*biz.PaymentConfig, error) {
|
||||
return s.uc.Configs(ctx)
|
||||
func (s *PaymentService) Configs(ctx context.Context) ([]*dto.PaymentConfigResponse, error) {
|
||||
configs, err := s.uc.Configs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]*dto.PaymentConfigResponse, 0, len(configs))
|
||||
for _, config := range configs {
|
||||
if config == nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, &dto.PaymentConfigResponse{Provider: config.Provider, Enabled: config.Enabled, Values: config.Values})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
func (s *PaymentService) SaveConfig(ctx context.Context, c *biz.PaymentConfig) error {
|
||||
return s.uc.SaveConfig(ctx, c)
|
||||
func (s *PaymentService) SaveConfig(ctx context.Context, req *dto.PaymentConfigRequest) error {
|
||||
if req == nil {
|
||||
return errors.New("支付配置请求为空")
|
||||
}
|
||||
return s.uc.SaveConfig(ctx, &biz.PaymentConfig{Provider: req.Provider, Enabled: req.Enabled, Values: req.Config})
|
||||
}
|
||||
func (s *PaymentService) Order(ctx context.Context, provider, tradeNo string) (*dto.PaymentOrderResponse, error) {
|
||||
order, err := s.uc.Order(ctx, provider, tradeNo)
|
||||
@@ -35,15 +51,70 @@ func (s *PaymentService) Order(ctx context.Context, provider, tradeNo string) (*
|
||||
FulfilledAt: order.FulfilledAt, RefundedAt: order.RefundedAt,
|
||||
}, nil
|
||||
}
|
||||
func (s *PaymentService) Create(ctx context.Context, req *biz.PaymentRequest) (*biz.PaymentResult, error) {
|
||||
return s.uc.Create(ctx, req)
|
||||
func (s *PaymentService) Create(ctx context.Context, req *dto.PaymentRequest) (*dto.PaymentResultResponse, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付请求为空")
|
||||
}
|
||||
result, err := s.uc.Create(ctx, &biz.PaymentRequest{
|
||||
Provider: req.Provider, TradeNo: req.TradeNo, Subject: req.Subject,
|
||||
Amount: req.Amount, OriginalAmount: req.OriginalAmount, Currency: req.Currency,
|
||||
NotifyURL: req.NotifyURL, ReturnURL: req.ReturnURL, ClientIP: req.ClientIP,
|
||||
Extra: req.Extra, BusinessType: req.BusinessType, BusinessID: req.BusinessID,
|
||||
PaymentMode: req.PaymentMode,
|
||||
})
|
||||
return paymentResultResponse(result), err
|
||||
}
|
||||
func (s *PaymentService) Query(ctx context.Context, provider, tradeNo string) (*biz.PaymentResult, error) {
|
||||
return s.uc.Query(ctx, provider, tradeNo)
|
||||
func (s *PaymentService) Query(ctx context.Context, req *dto.PaymentQueryRequest) (*dto.PaymentResultResponse, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付查询请求为空")
|
||||
}
|
||||
result, err := s.uc.Query(ctx, req.Provider, req.TradeNo)
|
||||
return paymentResultResponse(result), err
|
||||
}
|
||||
func (s *PaymentService) Refund(ctx context.Context, provider, tradeNo string, amount int64) (*biz.PaymentResult, error) {
|
||||
return s.uc.Refund(ctx, provider, tradeNo, amount)
|
||||
func (s *PaymentService) Refund(ctx context.Context, req *dto.PaymentRefundRequest) (*dto.PaymentResultResponse, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("支付退款请求为空")
|
||||
}
|
||||
result, err := s.uc.Refund(ctx, req.Provider, req.TradeNo, req.Amount)
|
||||
return paymentResultResponse(result), err
|
||||
}
|
||||
func (s *PaymentService) Callback(ctx context.Context, callback *biz.PaymentCallback) (*biz.PaymentResult, error) {
|
||||
return s.uc.Callback(ctx, callback)
|
||||
func (s *PaymentService) Callback(ctx context.Context, req *dto.PaymentCallbackRequest) (dto.PaymentCallbackAck, error) {
|
||||
if req == nil {
|
||||
return paymentCallbackAckResponse(biz.DefaultPaymentCallbackAck("", false)), errors.New("支付回调请求为空")
|
||||
}
|
||||
result, err := s.uc.Callback(ctx, &biz.PaymentCallback{Provider: req.Provider, Headers: req.Headers, Body: req.Body, Query: req.Query})
|
||||
if err != nil {
|
||||
return paymentCallbackAckResponse(biz.CallbackFailure(err, biz.DefaultPaymentCallbackAck(req.Provider, false))), err
|
||||
}
|
||||
ack := biz.DefaultPaymentCallbackAck(req.Provider, true)
|
||||
if result != nil && result.SuccessAck.StatusCode != 0 {
|
||||
ack = result.SuccessAck
|
||||
}
|
||||
return paymentCallbackAckResponse(ack), nil
|
||||
}
|
||||
|
||||
func DefaultPaymentCallbackAck(provider string, success bool) dto.PaymentCallbackAck {
|
||||
return paymentCallbackAckResponse(biz.DefaultPaymentCallbackAck(provider, success))
|
||||
}
|
||||
|
||||
func paymentResultResponse(result *biz.PaymentResult) *dto.PaymentResultResponse {
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
return &dto.PaymentResultResponse{
|
||||
Provider: result.Provider, Status: result.Status, TradeNo: result.TradeNo,
|
||||
ProviderTradeNo: result.ProviderTradeNo, Amount: result.Amount,
|
||||
PayerPaidAmount: result.PayerPaidAmount, CashPaidAmount: result.CashPaidAmount,
|
||||
PointPaidAmount: result.PointPaidAmount, DiscountAmount: result.DiscountAmount,
|
||||
ProviderDiscountAmount: result.ProviderDiscountAmount,
|
||||
MerchantDiscountAmount: result.MerchantDiscountAmount, SettlementAmount: result.SettlementAmount,
|
||||
Currency: result.Currency, PayerCurrency: result.PayerCurrency,
|
||||
AmountBreakdownKnown: result.AmountBreakdownKnown, Duplicate: result.Duplicate,
|
||||
Payload: result.Payload, OrderStatus: result.OrderStatus,
|
||||
FulfillmentStatus: result.FulfillmentStatus, RefundStatus: result.RefundStatus,
|
||||
}
|
||||
}
|
||||
|
||||
func paymentCallbackAckResponse(ack biz.PaymentCallbackAck) dto.PaymentCallbackAck {
|
||||
return dto.PaymentCallbackAck{StatusCode: ack.StatusCode, ContentType: ack.ContentType, Body: ack.Body}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user