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