+ quotation provider
This commit is contained in:
@@ -4,43 +4,54 @@ import (
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
type QuotePayment struct {
|
||||
type PaymentBase struct {
|
||||
IdempotencyKey string `json:"idempotencyKey"`
|
||||
Intent PaymentIntent `json:"intent"`
|
||||
PreviewOnly bool `json:"previewOnly"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type InitiatePayment struct {
|
||||
IdempotencyKey string `json:"idempotencyKey"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
Intent *PaymentIntent `json:"intent,omitempty"`
|
||||
QuoteRef string `json:"quoteRef,omitempty"`
|
||||
}
|
||||
|
||||
func (r QuotePayment) Validate() error {
|
||||
if r.IdempotencyKey == "" {
|
||||
func (b *PaymentBase) Validate() error {
|
||||
if b.IdempotencyKey == "" {
|
||||
return merrors.InvalidArgument("idempotencyKey is required", "idempotencyKey")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if validator, ok := any(r.Intent).(interface{ Validate() error }); ok {
|
||||
if err := validator.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
type QuotePayment struct {
|
||||
PaymentBase `json:",inline"`
|
||||
Intent PaymentIntent `json:"intent"`
|
||||
PreviewOnly bool `json:"previewOnly"`
|
||||
}
|
||||
|
||||
func (r *QuotePayment) Validate() error {
|
||||
// base checks
|
||||
if err := r.PaymentBase.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// intent is mandatory, so validate always
|
||||
if err := r.Intent.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate проверяет базовые инварианты запроса на инициацию платежа.
|
||||
type InitiatePayment struct {
|
||||
PaymentBase `json:",inline"`
|
||||
Intent *PaymentIntent `json:"intent,omitempty"`
|
||||
QuoteRef string `json:"quoteRef,omitempty"`
|
||||
}
|
||||
|
||||
func (r InitiatePayment) Validate() error {
|
||||
if r.IdempotencyKey == "" {
|
||||
return merrors.InvalidArgument("idempotencyKey is required", "idempotencyKey")
|
||||
// base checks
|
||||
if err := r.PaymentBase.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasIntent := r.Intent != nil
|
||||
hasQuote := r.QuoteRef != ""
|
||||
|
||||
// must be exactly one
|
||||
switch {
|
||||
case !hasIntent && !hasQuote:
|
||||
return merrors.NoData("either intent or quoteRef must be provided")
|
||||
@@ -48,11 +59,10 @@ func (r InitiatePayment) Validate() error {
|
||||
return merrors.DataConflict("intent and quoteRef are mutually exclusive")
|
||||
}
|
||||
|
||||
// if intent provided → validate it
|
||||
if hasIntent {
|
||||
if validator, ok := any(*r.Intent).(interface{ Validate() error }); ok {
|
||||
if err := validator.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.Intent.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user