Try to use generated yookassa client (unsuccessful)

This commit is contained in:
2026-03-17 16:37:29 +03:00
parent 35f6dc6ca0
commit 2c19b9c29b
14 changed files with 867 additions and 698 deletions

View File

@@ -2,6 +2,9 @@ package yookassa
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"payouts/internal/models"
"payouts/internal/service/database/orm"
@@ -31,8 +34,6 @@ func NewYookassaService(conf config.YooKassa) (Service, error) {
return nil, err
}
svc.payClient = payClient
// payClient.PaymentsPost()
return svc, nil
}
@@ -50,7 +51,7 @@ func (y *yookassaService) getParams(options ...Optional) *params {
func (y *yookassaService) BasicAuth(ctx context.Context, operationName gen.OperationName) (gen.BasicAuth, error) {
return gen.BasicAuth{
Username: y.conf.ApiPaymentKey,
Password: y.conf.ApiBaseSecret,
Password: y.conf.ApiPaymentSecret,
}, nil
}
@@ -60,12 +61,51 @@ 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) error {
func (y *yookassaService) CreatePayout(req models.PayoutReq, userSession *orm.User, idempotenceKey string, opts ...Optional) (models.PayoutResp, error) {
params := y.getParams(opts...)
y.payClient.PayoutsPost(params.ctx, &gen.PayoutRequest{}, gen.PayoutsPostParams{})
var payoutDestination gen.PayoutRequestPayoutDestinationData
return nil
switch req.PayoutType {
case models.TypeSBP:
payoutDestination = gen.NewSbpPayoutRequestPayoutDestinationData(gen.Sbp{
Type: gen.PayoutDestinationDataTypeSbp,
Phone: userSession.Phone,
})
case models.TypeYooMoney:
payoutDestination = gen.NewYooMoneyPayoutRequestPayoutDestinationData(gen.YooMoney{
Type: gen.PayoutDestinationDataTypeYooMoney,
AccountNumber: req.AccountNumber,
})
default:
return models.PayoutResp{Result: "failed", ErrorReason: "unsupported payout type"}, errors.New("unsupported payout type")
}
payoutOpt := gen.NewOptPayoutRequestPayoutDestinationData(payoutDestination)
postResp, err := y.payClient.PayoutsPost(params.ctx, &gen.PayoutRequest{
Amount: gen.PayoutRequestAmount{
Value: fmt.Sprintf("%.2f", req.Amount),
Currency: gen.CurrencyCodeRUB,
},
PayoutDestinationData: payoutOpt,
Test: gen.NewOptTest(gen.Test(y.conf.Test)),
}, gen.PayoutsPostParams{
IdempotenceKey: idempotenceKey,
})
slog.Debug(fmt.Sprintf("Received from yookassa: error %v; response %v", err, postResp))
if err == nil {
switch payoutResp := postResp.(type) {
case *gen.Payout:
slog.Info(fmt.Sprintf("Succeeded payout. It's id = %s", payoutResp.ID))
return models.PayoutResp{Result: string(payoutResp.Status), PayoutID: string(payoutResp.ID)}, nil
default:
return models.PayoutResp{Result: "failed", ErrorReason: fmt.Sprintf("%T", postResp)}, nil
}
}
return models.PayoutResp{Result: "failed", ErrorReason: errors.Join(errors.New("failed to call yookassa api"), err).Error()}, err
}
// GetConfig implements [Service].