Add payout req parsing

This commit is contained in:
2026-03-14 17:52:10 +03:00
parent 17bd6114b7
commit 35f6dc6ca0
4 changed files with 36 additions and 10 deletions

View File

@@ -12,6 +12,7 @@ import (
"go.uber.org/fx"
"payouts/internal/config"
"payouts/internal/models"
"payouts/internal/service/cache"
"payouts/internal/service/database"
"payouts/internal/service/database/orm"
@@ -106,12 +107,30 @@ func (p *payoutHandler) PayoutCreate(w http.ResponseWriter, r *http.Request) {
http.Error(w, errors.Join(errors.New(message), err).Error(), status)
}
_, err := p.getSession(r)
userSession, err := p.getSession(r)
if err != nil {
errResponse("unauthorized", err, http.StatusUnauthorized)
return
}
panic("unimplemented")
payoutReq := models.PayoutReq{
PayoutType: models.TypeSBP,
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&payoutReq)
if err != nil {
slog.Error("Failed to decode request body", slog.String("error", err.Error()))
errResponse("failed to decode request body", err, http.StatusBadRequest)
return
}
slog.Debug(fmt.Sprintf("Received create payload request: %v from user %v", payoutReq, userSession))
err = p.yooKassa.CreatePayout(payoutReq, userSession)
if err != nil {
slog.Error("Failed to create payout request", slog.String("error", err.Error()))
errResponse("failed to create payout request", err, http.StatusBadRequest)
return
}
}
// PaymentCallback implements [Handler].

View File

@@ -8,15 +8,15 @@ import (
type PayoutType int64
const (
SBP PayoutType = iota
YooMoney
TypeSBP PayoutType = iota
TypeYooMoney
)
func (r PayoutType) String() string {
switch r {
case SBP:
case TypeSBP:
return "spb"
case YooMoney:
case TypeYooMoney:
return "yoo_money"
}
return "unknown"
@@ -30,9 +30,9 @@ func (r *PayoutType) UnmarshalText(text []byte) (err error) {
s := strings.ToLower(string(text))
switch s {
case "spb":
*r = SBP
*r = TypeSBP
case "yoo_money":
*r = YooMoney
*r = TypeYooMoney
default:
err = fmt.Errorf("invalid payment type: %s", s)
}
@@ -44,3 +44,8 @@ type PayoutReq struct {
AccountNumber string `json:"account_number"`
Amount float32 `json:"amount"`
}
type PayoutResp struct {
Success bool `json:"success"`
Reason string `json:"reason,omitempty"`
}

View File

@@ -27,7 +27,7 @@ func WithContext(ctx context.Context) Optional {
}
type Service interface {
CreatePayout(models.PayoutReq, *orm.User, ...Optional)
CreatePayout(models.PayoutReq, *orm.User, ...Optional) error
GetConfig() yookassaConf.YooKassa
}

View File

@@ -60,10 +60,12 @@ func (y *yookassaService) OAuth2(ctx context.Context, operationName gen.Operatio
}
// CreatePayout implements [Service].
func (y *yookassaService) CreatePayout(req models.PayoutReq, userSession *orm.User, opts ...Optional) {
func (y *yookassaService) CreatePayout(req models.PayoutReq, userSession *orm.User, opts ...Optional) error {
params := y.getParams(opts...)
y.payClient.PayoutsPost(params.ctx, &gen.PayoutRequest{}, gen.PayoutsPostParams{})
return nil
}
// GetConfig implements [Service].