flamingo.me/flamingo-commerce/v3@v3.11.0/checkout/domain/placeorder/states/validate_payment_selection.go (about)

     1  package states
     2  
     3  import (
     4  	"context"
     5  
     6  	"go.opencensus.io/trace"
     7  
     8  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
     9  	"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
    10  	"flamingo.me/flamingo-commerce/v3/cart/domain/validation"
    11  	"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process"
    12  )
    13  
    14  type (
    15  	// ValidatePaymentSelection state
    16  	ValidatePaymentSelection struct {
    17  		validator            validation.PaymentSelectionValidator
    18  		cartDecoratorFactory *decorator.DecoratedCartFactory
    19  	}
    20  )
    21  
    22  var _ process.State = ValidatePaymentSelection{}
    23  
    24  // Inject dependencies
    25  func (v *ValidatePaymentSelection) Inject(
    26  	cartDecoratorFactory *decorator.DecoratedCartFactory,
    27  	opts *struct {
    28  		Validator validation.PaymentSelectionValidator `inject:",optional"`
    29  	},
    30  ) *ValidatePaymentSelection {
    31  	v.cartDecoratorFactory = cartDecoratorFactory
    32  	if opts != nil {
    33  		v.validator = opts.Validator
    34  	}
    35  
    36  	return v
    37  }
    38  
    39  // Name get state name
    40  func (ValidatePaymentSelection) Name() string {
    41  	return "ValidatePaymentSelection"
    42  }
    43  
    44  // Run the state operations
    45  func (v ValidatePaymentSelection) Run(ctx context.Context, p *process.Process) process.RunResult {
    46  	_, span := trace.StartSpan(ctx, "placeorder/state/ValidatePaymentSelection/Run")
    47  	defer span.End()
    48  
    49  	paymentSelection := p.Context().Cart.PaymentSelection
    50  	if paymentSelection == nil {
    51  		return process.RunResult{
    52  			Failed: process.PaymentErrorOccurredReason{
    53  				Error: cart.ErrPaymentSelectionNotSet.Error(),
    54  			},
    55  		}
    56  	}
    57  
    58  	if v.validator != nil {
    59  		decoratedCart := v.cartDecoratorFactory.Create(ctx, p.Context().Cart)
    60  		err := v.validator.Validate(ctx, decoratedCart, paymentSelection)
    61  		if err != nil {
    62  			return process.RunResult{
    63  				Failed: process.PaymentErrorOccurredReason{
    64  					Error: err.Error(),
    65  				},
    66  			}
    67  		}
    68  	}
    69  
    70  	p.UpdateState(CreatePayment{}.Name(), nil)
    71  	return process.RunResult{}
    72  }
    73  
    74  // Rollback the state operations
    75  func (v ValidatePaymentSelection) Rollback(context.Context, process.RollbackData) error {
    76  	return nil
    77  }
    78  
    79  // IsFinal if state is a final state
    80  func (v ValidatePaymentSelection) IsFinal() bool {
    81  	return false
    82  }