-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
42 lines (34 loc) · 993 Bytes
/
service.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
package pay
import (
"context"
"github.com/opendigitalpay-io/open-pay/internal/domain"
"github.com/opendigitalpay-io/open-pay/internal/factory"
"github.com/opendigitalpay-io/open-pay/internal/port/api"
)
type Service interface {
PayOrder(context.Context, uint64, api.PayOrderRequest) error
}
type service struct {
strategyFactory factory.OrderStrategyFactory
}
func NewService(strategyFactory factory.OrderStrategyFactory) Service {
return &service{
strategyFactory: strategyFactory,
}
}
func (s *service) PayOrder(ctx context.Context, orderID uint64, request api.PayOrderRequest) error {
paymentSource := domain.PaymentSource{
Type: domain.PaymentSourceType(request.PaymentSource.Type),
ID: request.PaymentSource.ID,
}
orderStrategy, err := s.strategyFactory.Create(ctx, orderID, paymentSource)
if err != nil {
return err
}
tryErr := orderStrategy.Try(ctx)
if tryErr != nil {
orderStrategy.Cancel(ctx)
return tryErr
}
return orderStrategy.Commit(ctx)
}