71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package srequest
|
|
|
|
import (
|
|
"github.com/tech/sendico/pkg/merrors"
|
|
)
|
|
|
|
type PaymentBase struct {
|
|
IdempotencyKey string `json:"idempotencyKey"`
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
}
|
|
|
|
func (b *PaymentBase) Validate() error {
|
|
if b.IdempotencyKey == "" {
|
|
return merrors.InvalidArgument("idempotencyKey is required", "idempotencyKey")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type InitiatePayment struct {
|
|
PaymentBase `json:",inline"`
|
|
Intent *PaymentIntent `json:"intent,omitempty"`
|
|
QuoteRef string `json:"quoteRef,omitempty"`
|
|
}
|
|
|
|
func (r InitiatePayment) Validate() error {
|
|
// 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")
|
|
case hasIntent && hasQuote:
|
|
return merrors.DataConflict("intent and quoteRef are mutually exclusive")
|
|
}
|
|
|
|
// if intent provided → validate it
|
|
if hasIntent {
|
|
if err := r.Intent.Validate(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|