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

     1  package states
     2  
     3  import (
     4  	"context"
     5  
     6  	"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process"
     7  	"flamingo.me/flamingo-commerce/v3/payment/application"
     8  	"go.opencensus.io/trace"
     9  )
    10  
    11  type (
    12  	// CompletePayment state
    13  	CompletePayment struct {
    14  		paymentService *application.PaymentService
    15  	}
    16  )
    17  
    18  var _ process.State = CompletePayment{}
    19  
    20  // Inject dependencies
    21  func (c *CompletePayment) Inject(
    22  	paymentService *application.PaymentService,
    23  ) *CompletePayment {
    24  	c.paymentService = paymentService
    25  
    26  	return c
    27  }
    28  
    29  // Name get state name
    30  func (CompletePayment) Name() string {
    31  	return "CompletePayment"
    32  }
    33  
    34  // Run the state operations
    35  func (c CompletePayment) Run(ctx context.Context, p *process.Process) process.RunResult {
    36  	ctx, span := trace.StartSpan(ctx, "placeorder/state/CompletePayment/Run")
    37  	defer span.End()
    38  
    39  	cart := p.Context().Cart
    40  	paymentGateway, err := c.paymentService.PaymentGatewayByCart(cart)
    41  	if err != nil {
    42  		return process.RunResult{
    43  			Failed: process.PaymentErrorOccurredReason{Error: err.Error()},
    44  		}
    45  	}
    46  
    47  	payment, err := paymentGateway.OrderPaymentFromFlow(ctx, &cart, p.Context().UUID)
    48  	if err != nil {
    49  		return process.RunResult{
    50  			Failed: process.PaymentErrorOccurredReason{Error: err.Error()},
    51  		}
    52  	}
    53  
    54  	err = paymentGateway.ConfirmResult(ctx, &cart, payment)
    55  	if err != nil {
    56  		return process.RunResult{
    57  			Failed: process.PaymentErrorOccurredReason{Error: err.Error()},
    58  		}
    59  	}
    60  
    61  	p.UpdateState(ValidatePayment{}.Name(), nil)
    62  	return process.RunResult{}
    63  }
    64  
    65  // Rollback the state operations
    66  func (c CompletePayment) Rollback(ctx context.Context, _ process.RollbackData) error {
    67  	return nil
    68  }
    69  
    70  // IsFinal if state is a final state
    71  func (c CompletePayment) IsFinal() bool {
    72  	return false
    73  }