|
|
|
|
@@ -2,6 +2,7 @@ package paymentapiimp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
@@ -18,24 +19,33 @@ import (
|
|
|
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
|
|
|
"github.com/tech/sendico/pkg/mservice"
|
|
|
|
|
orchestratorv1 "github.com/tech/sendico/pkg/proto/payments/orchestration/v1"
|
|
|
|
|
quotationv1 "github.com/tech/sendico/pkg/proto/payments/quotation/v1"
|
|
|
|
|
eapi "github.com/tech/sendico/server/interface/api"
|
|
|
|
|
mutil "github.com/tech/sendico/server/internal/mutil/param"
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type paymentClient interface {
|
|
|
|
|
QuotePayment(ctx context.Context, req *quotationv1.QuotePaymentRequest) (*quotationv1.QuotePaymentResponse, error)
|
|
|
|
|
QuotePayments(ctx context.Context, req *quotationv1.QuotePaymentRequest) (*quotationv1.QuotePaymentsResponse, error)
|
|
|
|
|
type executionClient interface {
|
|
|
|
|
InitiatePayments(ctx context.Context, req *orchestratorv1.InitiatePaymentsRequest) (*orchestratorv1.InitiatePaymentsResponse, error)
|
|
|
|
|
InitiatePayment(ctx context.Context, req *orchestratorv1.InitiatePaymentRequest) (*orchestratorv1.InitiatePaymentResponse, error)
|
|
|
|
|
ListPayments(ctx context.Context, req *orchestratorv1.ListPaymentsRequest) (*orchestratorv1.ListPaymentsResponse, error)
|
|
|
|
|
Close() error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type quotationClient interface {
|
|
|
|
|
QuotePayment(ctx context.Context, req *quotationv1.QuotePaymentRequest) (*quotationv1.QuotePaymentResponse, error)
|
|
|
|
|
QuotePayments(ctx context.Context, req *quotationv1.QuotePaymentsRequest) (*quotationv1.QuotePaymentsResponse, error)
|
|
|
|
|
Close() error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PaymentAPI struct {
|
|
|
|
|
logger mlogger.Logger
|
|
|
|
|
client paymentClient
|
|
|
|
|
execution executionClient
|
|
|
|
|
quotation quotationClient
|
|
|
|
|
enf auth.Enforcer
|
|
|
|
|
oph mutil.ParamHelper
|
|
|
|
|
discovery *discovery.Client
|
|
|
|
|
@@ -49,11 +59,16 @@ type PaymentAPI struct {
|
|
|
|
|
func (a *PaymentAPI) Name() mservice.Type { return mservice.Payments }
|
|
|
|
|
|
|
|
|
|
func (a *PaymentAPI) Finish(ctx context.Context) error {
|
|
|
|
|
if a.client != nil {
|
|
|
|
|
if err := a.client.Close(); err != nil {
|
|
|
|
|
if a.execution != nil {
|
|
|
|
|
if err := a.execution.Close(); err != nil {
|
|
|
|
|
a.logger.Warn("Failed to close payment orchestrator client", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if a.quotation != nil {
|
|
|
|
|
if err := a.quotation.Close(); err != nil {
|
|
|
|
|
a.logger.Warn("Failed to close payment quotation client", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if a.discovery != nil {
|
|
|
|
|
a.discovery.Close()
|
|
|
|
|
}
|
|
|
|
|
@@ -103,15 +118,15 @@ func (a *PaymentAPI) initPaymentClient(cfg *eapi.PaymentOrchestratorConfig, quot
|
|
|
|
|
return merrors.InvalidArgument("payment orchestrator configuration is not provided")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
address := strings.TrimSpace(cfg.Address)
|
|
|
|
|
if address == "" {
|
|
|
|
|
address = strings.TrimSpace(os.Getenv(cfg.AddressEnv))
|
|
|
|
|
}
|
|
|
|
|
if address == "" {
|
|
|
|
|
return merrors.InvalidArgument(fmt.Sprintf("payment orchestrator address is not specified and address env %s is empty", cfg.AddressEnv))
|
|
|
|
|
address, err := resolveClientAddress("payment orchestrator", cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quoteAddress := address
|
|
|
|
|
quoteInsecure := cfg.Insecure
|
|
|
|
|
quoteDialTimeout := cfg.DialTimeoutSeconds
|
|
|
|
|
quoteCallTimeout := cfg.CallTimeoutSeconds
|
|
|
|
|
if quoteCfg != nil {
|
|
|
|
|
if addr := strings.TrimSpace(quoteCfg.Address); addr != "" {
|
|
|
|
|
quoteAddress = addr
|
|
|
|
|
@@ -120,25 +135,133 @@ func (a *PaymentAPI) initPaymentClient(cfg *eapi.PaymentOrchestratorConfig, quot
|
|
|
|
|
quoteAddress = resolved
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
quoteInsecure = quoteCfg.Insecure
|
|
|
|
|
quoteDialTimeout = quoteCfg.DialTimeoutSeconds
|
|
|
|
|
quoteCallTimeout = quoteCfg.CallTimeoutSeconds
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clientCfg := orchestratorclient.Config{
|
|
|
|
|
Address: address,
|
|
|
|
|
QuoteAddress: quoteAddress,
|
|
|
|
|
DialTimeout: time.Duration(cfg.DialTimeoutSeconds) * time.Second,
|
|
|
|
|
CallTimeout: time.Duration(cfg.CallTimeoutSeconds) * time.Second,
|
|
|
|
|
Insecure: cfg.Insecure,
|
|
|
|
|
Address: address,
|
|
|
|
|
DialTimeout: time.Duration(cfg.DialTimeoutSeconds) * time.Second,
|
|
|
|
|
CallTimeout: time.Duration(cfg.CallTimeoutSeconds) * time.Second,
|
|
|
|
|
Insecure: cfg.Insecure,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := orchestratorclient.New(context.Background(), clientCfg)
|
|
|
|
|
execution, err := orchestratorclient.New(context.Background(), clientCfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.client = client
|
|
|
|
|
quotation, err := newQuotationClient(context.Background(), quotationClientConfig{
|
|
|
|
|
Address: quoteAddress,
|
|
|
|
|
DialTimeout: time.Duration(quoteDialTimeout) * time.Second,
|
|
|
|
|
CallTimeout: time.Duration(quoteCallTimeout) * time.Second,
|
|
|
|
|
Insecure: quoteInsecure,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = execution.Close()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.execution = execution
|
|
|
|
|
a.quotation = quotation
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveClientAddress(service string, cfg *eapi.PaymentOrchestratorConfig) (string, error) {
|
|
|
|
|
if cfg == nil {
|
|
|
|
|
return "", merrors.InvalidArgument(strings.TrimSpace(service) + " configuration is not provided")
|
|
|
|
|
}
|
|
|
|
|
address := strings.TrimSpace(cfg.Address)
|
|
|
|
|
if address != "" {
|
|
|
|
|
return address, nil
|
|
|
|
|
}
|
|
|
|
|
if env := strings.TrimSpace(cfg.AddressEnv); env != "" {
|
|
|
|
|
if resolved := strings.TrimSpace(os.Getenv(env)); resolved != "" {
|
|
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
return "", merrors.InvalidArgument(fmt.Sprintf("%s address is not specified and address env %s is empty", strings.TrimSpace(service), env))
|
|
|
|
|
}
|
|
|
|
|
return "", merrors.InvalidArgument(strings.TrimSpace(service) + " address is not specified")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type quotationClientConfig struct {
|
|
|
|
|
Address string
|
|
|
|
|
DialTimeout time.Duration
|
|
|
|
|
CallTimeout time.Duration
|
|
|
|
|
Insecure bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *quotationClientConfig) setDefaults() {
|
|
|
|
|
if c.DialTimeout <= 0 {
|
|
|
|
|
c.DialTimeout = 5 * time.Second
|
|
|
|
|
}
|
|
|
|
|
if c.CallTimeout <= 0 {
|
|
|
|
|
c.CallTimeout = 3 * time.Second
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type grpcQuotationClient struct {
|
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
|
client quotationv1.QuotationServiceClient
|
|
|
|
|
callTimeout time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newQuotationClient(ctx context.Context, cfg quotationClientConfig, opts ...grpc.DialOption) (quotationClient, error) {
|
|
|
|
|
cfg.setDefaults()
|
|
|
|
|
if strings.TrimSpace(cfg.Address) == "" {
|
|
|
|
|
return nil, merrors.InvalidArgument("payment quotation: address is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dialCtx, cancel := context.WithTimeout(ctx, cfg.DialTimeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
dialOpts := make([]grpc.DialOption, 0, len(opts)+1)
|
|
|
|
|
dialOpts = append(dialOpts, opts...)
|
|
|
|
|
if cfg.Insecure {
|
|
|
|
|
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
|
} else {
|
|
|
|
|
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(dialCtx, cfg.Address, dialOpts...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, merrors.InternalWrap(err, fmt.Sprintf("payment-quotation: dial %s", cfg.Address))
|
|
|
|
|
}
|
|
|
|
|
return &grpcQuotationClient{
|
|
|
|
|
conn: conn,
|
|
|
|
|
client: quotationv1.NewQuotationServiceClient(conn),
|
|
|
|
|
callTimeout: cfg.CallTimeout,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *grpcQuotationClient) Close() error {
|
|
|
|
|
if c == nil || c.conn == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return c.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *grpcQuotationClient) QuotePayment(ctx context.Context, req *quotationv1.QuotePaymentRequest) (*quotationv1.QuotePaymentResponse, error) {
|
|
|
|
|
callCtx, cancel := c.callContext(ctx)
|
|
|
|
|
defer cancel()
|
|
|
|
|
return c.client.QuotePayment(callCtx, req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *grpcQuotationClient) QuotePayments(ctx context.Context, req *quotationv1.QuotePaymentsRequest) (*quotationv1.QuotePaymentsResponse, error) {
|
|
|
|
|
callCtx, cancel := c.callContext(ctx)
|
|
|
|
|
defer cancel()
|
|
|
|
|
return c.client.QuotePayments(callCtx, req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *grpcQuotationClient) callContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
|
|
|
timeout := c.callTimeout
|
|
|
|
|
if timeout <= 0 {
|
|
|
|
|
timeout = 3 * time.Second
|
|
|
|
|
}
|
|
|
|
|
return context.WithTimeout(ctx, timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *PaymentAPI) initDiscoveryClient(cfg *eapi.Config) error {
|
|
|
|
|
if cfg == nil || cfg.Mw == nil {
|
|
|
|
|
return nil
|
|
|
|
|
|