forked from i-love-flamingo/flamingo-commerce
/
orderService.go
478 lines (374 loc) · 19.2 KB
/
orderService.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package application
import (
"context"
"encoding/gob"
"errors"
"fmt"
"go.opencensus.io/trace"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/opencensus"
"flamingo.me/flamingo/v3/framework/web"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"flamingo.me/flamingo-commerce/v3/cart/application"
"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
"flamingo.me/flamingo-commerce/v3/cart/domain/placeorder"
paymentDomain "flamingo.me/flamingo-commerce/v3/payment/domain"
"flamingo.me/flamingo-commerce/v3/payment/interfaces"
priceDomain "flamingo.me/flamingo-commerce/v3/price/domain"
)
type (
// OrderService defines the order service
OrderService struct {
logger flamingo.Logger
cartService *application.CartService
cartReceiverService *application.CartReceiverService
deliveryInfoBuilder cart.DeliveryInfoBuilder
webCartPaymentGateways map[string]interfaces.WebCartPaymentGateway
decoratedCartFactory *decorator.DecoratedCartFactory
}
// PlaceOrderInfo struct defines the data of payments on placed orders
PlaceOrderInfo struct {
PaymentInfos []PlaceOrderPaymentInfo
PlacedOrders placeorder.PlacedOrderInfos
ContactEmail string
Cart cart.Cart
}
// PlaceOrderPaymentInfo holding payment infos
PlaceOrderPaymentInfo struct {
Gateway string
PaymentProvider string
Method string
CreditCardInfo *placeorder.CreditCardInfo
Amount priceDomain.Price
Title string
}
)
const (
// PaymentFlowStandardCorrelationID used as correlationid for the start of the payment (session scoped)
PaymentFlowStandardCorrelationID = "checkout"
// LastPlacedOrderSessionKey is the session key for storing the last placed order
LastPlacedOrderSessionKey = "orderservice_last_placed"
)
var (
// cartValidationFailCount counts validation failures on carts
cartValidationFailCount = stats.Int64("flamingo-commerce/checkout/orders/cart_validation_failed", "Count of failures while validating carts", stats.UnitDimensionless)
// noPaymentSelectionCount counts error for orders without payment selection
noPaymentSelectionCount = stats.Int64("flamingo-commerce/checkout/orders/no_payment_selection", "Count of carts without having a selected payment", stats.UnitDimensionless)
// paymentGatewayNotFoundCount counts errors if payment gateway for selected payment could not be found
paymentGatewayNotFoundCount = stats.Int64("flamingo-commerce/checkout/orders/payment_gateway_not_found", "The selected payment gateway could not be found", stats.UnitDimensionless)
// paymentFlowStatusErrorCount counts errors while fetching payment flow status
paymentFlowStatusErrorCount = stats.Int64("flamingo-commerce/checkout/orders/payment_flow_status_error", "Count of failures while fetching payment flow status", stats.UnitDimensionless)
// orderPaymentFromFlowErrorCount counts errors while fetching payment from flow
orderPaymentFromFlowErrorCount = stats.Int64("flamingo-commerce/checkout/orders/order_payment_from_flow_error", "Count of failures while fetching payment from flow", stats.UnitDimensionless)
// paymentFlowStatusFailedCanceledCount counts orders trying to be placed with payment status either failed or canceled
paymentFlowStatusFailedCanceledCount = stats.Int64("flamingo-commerce/checkout/orders/payment_flow_status_failed_canceled", "Count of payments with status failed or canceled", stats.UnitDimensionless)
// paymentFlowStatusAbortedCount counts orders trying to be placed with payment status aborted
paymentFlowStatusAbortedCount = stats.Int64("flamingo-commerce/checkout/orders/payment_flow_status_aborted", "Count of payments with status aborted", stats.UnitDimensionless)
// placeOrderFailCount counts failed placed orders
placeOrderFailCount = stats.Int64("flamingo-commerce/checkout/orders/place_order_failed", "Count of failures while placing orders", stats.UnitDimensionless)
// placeOrderSuccessCount counts successfully placed orders
placeOrderSuccessCount = stats.Int64("flamingo-commerce/checkout/orders/place_order_successful", "Count of successfully placed orders", stats.UnitDimensionless)
)
func init() {
gob.Register(PlaceOrderInfo{})
openCensusViews := map[string]*stats.Int64Measure{
"flamingo-commerce/checkout/orders/cart_validation_failed": cartValidationFailCount,
"flamingo-commerce/checkout/orders/no_payment_selection": noPaymentSelectionCount,
"flamingo-commerce/checkout/orders/payment_gateway_not_found": paymentGatewayNotFoundCount,
"flamingo-commerce/checkout/orders/payment_flow_status_error": paymentFlowStatusErrorCount,
"flamingo-commerce/checkout/orders/order_payment_from_flow_error": orderPaymentFromFlowErrorCount,
"flamingo-commerce/checkout/orders/payment_flow_status_failed_canceled": paymentFlowStatusFailedCanceledCount,
"flamingo-commerce/checkout/orders/payment_flow_status_aborted": paymentFlowStatusAbortedCount,
"flamingo-commerce/checkout/orders/place_order_failed": placeOrderFailCount,
"flamingo-commerce/checkout/orders/place_order_successful": placeOrderSuccessCount,
}
for name, measure := range openCensusViews {
err := opencensus.View(name, measure, view.Sum())
if err != nil {
panic(err)
}
stats.Record(context.Background(), measure.M(0))
}
}
// Inject dependencies
func (os *OrderService) Inject(
logger flamingo.Logger,
CartService *application.CartService,
CartReceiverService *application.CartReceiverService,
DeliveryInfoBuilder cart.DeliveryInfoBuilder,
webCartPaymentGatewayProvider interfaces.WebCartPaymentGatewayProvider,
decoratedCartFactory *decorator.DecoratedCartFactory,
) {
os.logger = logger.WithField(flamingo.LogKeyCategory, "checkout.OrderService").WithField(flamingo.LogKeyModule, "checkout")
os.cartService = CartService
os.cartReceiverService = CartReceiverService
os.webCartPaymentGateways = webCartPaymentGatewayProvider()
os.deliveryInfoBuilder = DeliveryInfoBuilder
os.decoratedCartFactory = decoratedCartFactory
}
// GetPaymentGateway tries to get the supplied payment gateway by code from the registered payment gateways
func (os *OrderService) GetPaymentGateway(ctx context.Context, paymentGatewayCode string) (interfaces.WebCartPaymentGateway, error) {
_, span := trace.StartSpan(ctx, "checkout/OrderService/GetPaymentGateway")
defer span.End()
gateway, ok := os.webCartPaymentGateways[paymentGatewayCode]
if !ok {
return nil, errors.New("Payment gateway " + paymentGatewayCode + " not found")
}
return gateway, nil
}
// GetAvailablePaymentGateways returns the list of registered WebCartPaymentGateway
func (os *OrderService) GetAvailablePaymentGateways(ctx context.Context) map[string]interfaces.WebCartPaymentGateway {
_, span := trace.StartSpan(ctx, "checkout/OrderService/GetAvailablePaymentGateways")
defer span.End()
return os.webCartPaymentGateways
}
// CurrentCartPlaceOrder places the current cart without additional payment processing
func (os *OrderService) CurrentCartPlaceOrder(ctx context.Context, session *web.Session, cartPayment placeorder.Payment) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CurrentCartPlaceOrder")
defer span.End()
var info *PlaceOrderInfo
var err error
web.RunWithDetachedContext(ctx, func(placeOrderContext context.Context) {
info, err = func() (*PlaceOrderInfo, error) {
decoratedCart, err := os.cartReceiverService.ViewDecoratedCart(placeOrderContext, session)
if err != nil {
os.logger.WithContext(placeOrderContext).Error("OnStepCurrentCartPlaceOrder GetDecoratedCart Error ", err)
return nil, err
}
return os.placeOrder(placeOrderContext, session, decoratedCart, cartPayment)
}()
})
return info, err
}
func (os *OrderService) placeOrder(ctx context.Context, session *web.Session, decoratedCart *decorator.DecoratedCart, payment placeorder.Payment) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/placeOrder")
defer span.End()
validationResult := os.cartService.ValidateCart(ctx, session, decoratedCart)
if !validationResult.IsValid() {
// record cartValidationFailCount metric
stats.Record(ctx, cartValidationFailCount.M(1))
os.logger.WithContext(ctx).Warn("Try to place an invalid cart")
return nil, errors.New("cart is invalid")
}
placedOrderInfos, err := os.cartService.PlaceOrderWithCart(ctx, session, &decoratedCart.Cart, &payment)
if err != nil {
// record placeOrderFailCount metric
stats.Record(ctx, placeOrderFailCount.M(1))
os.logger.WithContext(ctx).Error("Error during place Order:" + err.Error())
return nil, errors.New("error while placing the order. please contact customer support")
}
placeOrderInfo := os.preparePlaceOrderInfo(ctx, decoratedCart.Cart, placedOrderInfos, payment)
os.storeLastPlacedOrder(ctx, placeOrderInfo)
// record placeOrderSuccessCount metric
stats.Record(ctx, placeOrderSuccessCount.M(1))
return placeOrderInfo, nil
}
// CancelOrder cancels an previously placed order and returns the restored cart with the order content
func (os *OrderService) CancelOrder(ctx context.Context, session *web.Session, order *PlaceOrderInfo) (*cart.Cart, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CancelOrder")
defer span.End()
return os.cartService.CancelOrder(ctx, session, order.PlacedOrders, order.Cart)
}
// CancelOrderWithoutRestore cancels an previously placed order
func (os *OrderService) CancelOrderWithoutRestore(ctx context.Context, session *web.Session, order *PlaceOrderInfo) error {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CancelOrderWithoutRestore")
defer span.End()
return os.cartService.CancelOrderWithoutRestore(ctx, session, order.PlacedOrders)
}
// CurrentCartPlaceOrderWithPaymentProcessing places the current cart which is fetched from the context
func (os *OrderService) CurrentCartPlaceOrderWithPaymentProcessing(ctx context.Context, session *web.Session) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CurrentCartPlaceOrderWithPaymentProcessing")
defer span.End()
var info *PlaceOrderInfo
var err error
// use a background context from here on to prevent the place order canceled by context cancel
web.RunWithDetachedContext(ctx, func(placeOrderContext context.Context) {
info, err = func() (*PlaceOrderInfo, error) {
// fetch decorated cart either via cache or freshly from cart receiver service
decoratedCart, err := os.cartReceiverService.ViewDecoratedCart(placeOrderContext, session)
if err != nil {
os.logger.WithContext(placeOrderContext).Warn("Cannot create decorated cart from cart")
return nil, errors.New("cart is invalid")
}
return os.placeOrderWithPaymentProcessing(placeOrderContext, decoratedCart, session)
}()
})
return info, err
}
// CartPlaceOrderWithPaymentProcessing places the cart passed to the function
// this function enables clients to pass a cart as is, without the usage of the cartReceiverService
func (os *OrderService) CartPlaceOrderWithPaymentProcessing(ctx context.Context, decoratedCart *decorator.DecoratedCart,
session *web.Session) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CartPlaceOrderWithPaymentProcessing")
defer span.End()
var info *PlaceOrderInfo
var err error
// use a background context from here on to prevent the place order canceled by context cancel
web.RunWithDetachedContext(ctx, func(placeOrderContext context.Context) {
info, err = os.placeOrderWithPaymentProcessing(placeOrderContext, decoratedCart, session)
})
return info, err
}
// CartPlaceOrder places the cart passed to the function
// this function enables clients to pass a cart as is, without the usage of the cartReceiverService
func (os *OrderService) CartPlaceOrder(ctx context.Context, decoratedCart *decorator.DecoratedCart, payment placeorder.Payment) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CartPlaceOrder")
defer span.End()
var info *PlaceOrderInfo
var err error
web.RunWithDetachedContext(ctx, func(placeOrderContext context.Context) {
info, err = os.placeOrder(placeOrderContext, web.SessionFromContext(ctx), decoratedCart, payment)
})
return info, err
}
// storeLastPlacedOrder stores the last placed order/cart in the session
func (os *OrderService) storeLastPlacedOrder(ctx context.Context, info *PlaceOrderInfo) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/storeLastPlacedOrder")
defer span.End()
session := web.SessionFromContext(ctx)
_ = session.Store(LastPlacedOrderSessionKey, info)
}
// LastPlacedOrder returns the last placed order/cart if available
func (os *OrderService) LastPlacedOrder(ctx context.Context) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/LastPlacedOrder")
defer span.End()
session := web.SessionFromContext(ctx)
lastPlacedOrder, found := session.Load(LastPlacedOrderSessionKey)
if !found {
return nil, nil
}
placeOrderInfo, ok := lastPlacedOrder.(PlaceOrderInfo)
if !ok {
return nil, errors.New("placeOrderInfo couldn't be received from session")
}
return &placeOrderInfo, nil
}
// HasLastPlacedOrder returns if a order has been previously placed
func (os *OrderService) HasLastPlacedOrder(ctx context.Context) bool {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/HasLastPlacedOrder")
defer span.End()
lastPlaced, err := os.LastPlacedOrder(ctx)
return lastPlaced != nil && err == nil
}
// ClearLastPlacedOrder clears the last placed cart, this can be useful if an cart / order is finished
func (os *OrderService) ClearLastPlacedOrder(ctx context.Context) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/ClearLastPlacedOrder")
defer span.End()
session := web.SessionFromContext(ctx)
session.Delete(LastPlacedOrderSessionKey)
}
// LastPlacedOrCurrentCart returns the decorated cart of the last placed order if there is one if not return the current cart
func (os *OrderService) LastPlacedOrCurrentCart(ctx context.Context) (*decorator.DecoratedCart, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/LastPlacedOrCurrentCart")
defer span.End()
lastPlacedOrder, err := os.LastPlacedOrder(ctx)
if err != nil {
os.logger.Warn("couldn't get last placed order:", err)
return nil, err
}
if lastPlacedOrder != nil {
// cart has been placed early use it
return os.decoratedCartFactory.Create(ctx, lastPlacedOrder.Cart), nil
}
// cart wasn't placed early, fetch it from service
decoratedCart, err := os.cartReceiverService.ViewDecoratedCart(ctx, web.SessionFromContext(ctx))
if err != nil {
os.logger.WithContext(ctx).Error("ViewDecoratedCart Error:", err)
return nil, err
}
return decoratedCart, nil
}
// placeOrderWithPaymentProcessing after generating the decorated cart, the place order flow
// is the same for the interface functions, therefore the common flow is placed in this private helper function
func (os *OrderService) placeOrderWithPaymentProcessing(ctx context.Context, decoratedCart *decorator.DecoratedCart,
session *web.Session) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/placeOrderWithPaymentProcessing")
defer span.End()
if !decoratedCart.Cart.IsPaymentSelected() {
// record noPaymentSelectionCount metric
stats.Record(ctx, noPaymentSelectionCount.M(1))
os.logger.WithContext(ctx).Error("cart.checkoutcontroller.submitaction: Error Gateway not in carts PaymentSelection")
return nil, errors.New("no payment gateway selected")
}
validationResult := os.cartService.ValidateCart(ctx, session, decoratedCart)
if !validationResult.IsValid() {
// record cartValidationFailCount metric
stats.Record(ctx, cartValidationFailCount.M(1))
os.logger.WithContext(ctx).Warn("Try to place an invalid cart")
return nil, errors.New("cart is invalid")
}
gateway, err := os.GetPaymentGateway(ctx, decoratedCart.Cart.PaymentSelection.Gateway())
if err != nil {
// record paymentGatewayNotFoundCount metric
stats.Record(ctx, paymentGatewayNotFoundCount.M(1))
os.logger.WithContext(ctx).Error(fmt.Sprintf("cart.checkoutcontroller.submitaction: Error %v Gateway: %v", err, decoratedCart.Cart.PaymentSelection.Gateway()))
return nil, errors.New("selected gateway not available")
}
flowStatus, err := gateway.FlowStatus(ctx, &decoratedCart.Cart, PaymentFlowStandardCorrelationID)
if err != nil {
// record paymentFlowStatusErrorCount metric
stats.Record(ctx, paymentFlowStatusErrorCount.M(1))
return nil, err
}
if flowStatus.Status == paymentDomain.PaymentFlowStatusFailed || flowStatus.Status == paymentDomain.PaymentFlowStatusCancelled {
// record paymentFlowStatusFailedCanceledCount metric
stats.Record(ctx, paymentFlowStatusFailedCanceledCount.M(1))
os.logger.WithContext(ctx).Info("cart.checkoutcontroller.submitaction: PaymentFlowStatusFailed or PaymentFlowStatusCancelled: Error ", flowStatus.Error)
return nil, flowStatus.Error
}
if flowStatus.Status == paymentDomain.PaymentFlowStatusAborted {
// record paymentFlowStatusAbortedCount metric
stats.Record(ctx, paymentFlowStatusAbortedCount.M(1))
os.logger.WithContext(ctx).Info("cart.checkoutcontroller.submitaction: PaymentFlowStatusAborted: Error ", flowStatus.Error)
return nil, flowStatus.Error
}
cartPayment, err := gateway.OrderPaymentFromFlow(ctx, &decoratedCart.Cart, PaymentFlowStandardCorrelationID)
if err != nil {
// record orderPaymentFromFlowErrorCount metric
stats.Record(ctx, orderPaymentFromFlowErrorCount.M(1))
return nil, err
}
placedOrderInfos, err := os.cartService.PlaceOrderWithCart(ctx, session, &decoratedCart.Cart, cartPayment)
if err != nil {
// record placeOrderFailCount metric
stats.Record(ctx, placeOrderFailCount.M(1))
os.logger.WithContext(ctx).Error("Error during place Order: " + err.Error())
return nil, err
}
os.logger.WithContext(ctx).Info("Placed Order: ", placedOrderInfos)
placeOrderInfo := os.preparePlaceOrderInfo(ctx, decoratedCart.Cart, placedOrderInfos, *cartPayment)
os.storeLastPlacedOrder(ctx, placeOrderInfo)
if flowStatus.Status != paymentDomain.PaymentFlowStatusCompleted {
err = gateway.ConfirmResult(ctx, &decoratedCart.Cart, cartPayment)
if err != nil {
os.logger.WithContext(ctx).Error("Error during gateway.ConfirmResult: " + err.Error())
return nil, err
}
}
// record placeOrderSuccessCount metric
stats.Record(ctx, placeOrderSuccessCount.M(1))
return placeOrderInfo, nil
}
func (os *OrderService) preparePlaceOrderInfo(ctx context.Context, currentCart cart.Cart, placedOrderInfos placeorder.PlacedOrderInfos, cartPayment placeorder.Payment) *PlaceOrderInfo {
_, span := trace.StartSpan(ctx, "checkout/OrderService/preparePlaceOrderInfo")
defer span.End()
email := currentCart.GetContactMail()
placeOrderInfo := &PlaceOrderInfo{
ContactEmail: email,
PlacedOrders: placedOrderInfos,
Cart: currentCart,
}
for _, transaction := range cartPayment.Transactions {
placeOrderInfo.PaymentInfos = append(placeOrderInfo.PaymentInfos, PlaceOrderPaymentInfo{
Gateway: cartPayment.Gateway,
Method: transaction.Method,
PaymentProvider: transaction.PaymentProvider,
Title: transaction.Title,
Amount: transaction.AmountPayed,
CreditCardInfo: transaction.CreditCardInfo,
})
}
return placeOrderInfo
}