flamingo.me/flamingo-commerce/v3@v3.11.0/payment/interfaces/controller/paymentapicontroller.go (about) 1 package controller 2 3 import ( 4 "context" 5 "fmt" 6 7 // cart type is referenced in swag comment and requires empty import 8 _ "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 9 // domain type is referenced in swag comment and requires empty import 10 _ "flamingo.me/flamingo-commerce/v3/payment/domain" 11 12 "flamingo.me/flamingo-commerce/v3/checkout/application" 13 14 "flamingo.me/flamingo/v3/framework/flamingo" 15 "flamingo.me/flamingo/v3/framework/web" 16 ) 17 18 type ( 19 // PaymentAPIController for payment api 20 PaymentAPIController struct { 21 responder *web.Responder 22 orderService *application.OrderService 23 logger flamingo.Logger 24 } 25 26 resultError struct { 27 Message string 28 Code string 29 } // @name paymentResultError 30 ) 31 32 // Inject dependencies 33 func (pc *PaymentAPIController) Inject( 34 responder *web.Responder, 35 Logger flamingo.Logger, 36 orderService *application.OrderService, 37 ) { 38 pc.responder = responder 39 pc.logger = Logger.WithField("category", "PaymentApiController") 40 pc.orderService = orderService 41 } 42 43 // Status Get Payment Status 44 // @Summary Get the payment status of current cart (or last placed cart) 45 // @Tags Payment 46 // @Produce json 47 // @Success 200 {object} domain.FlowStatus{data=cart.Cart} 48 // @Failure 500 {object} resultError 49 // @Router /api/v1/payment/status [get] 50 func (pc *PaymentAPIController) Status(ctx context.Context, r *web.Request) web.Result { 51 decoratedCart, err := pc.orderService.LastPlacedOrCurrentCart(ctx) 52 53 if err != nil { 54 pc.logger.Warn("Error when getting last used cart", err) 55 return pc.responder.Data(resultError{ 56 Message: fmt.Sprintf("Cart not found: %v", err), 57 Code: "status.polling.cart.error", 58 }) 59 } 60 61 if decoratedCart.Cart.PaymentSelection == nil { 62 pc.logger.Warn("Error because payment selection is empty") 63 return pc.responder.Data(resultError{ 64 Message: "Payment selection is empty", 65 Code: "status.polling.paymentselection.error", 66 }) 67 } 68 69 gateway, err := pc.orderService.GetPaymentGateway(ctx, decoratedCart.Cart.PaymentSelection.Gateway()) 70 if err != nil { 71 pc.logger.Warn("Error because payment gateway is not set", err) 72 return pc.responder.Data(resultError{ 73 Message: fmt.Sprintf("Payment Gateway is not set: %v", err), 74 Code: "status.polling.paymentgateway.error", 75 }) 76 } 77 78 flowStatus, err := gateway.FlowStatus(ctx, &decoratedCart.Cart, application.PaymentFlowStandardCorrelationID) 79 if err != nil { 80 pc.logger.Warn("Error because flow status is unknown", err) 81 return pc.responder.Data(resultError{ 82 Message: fmt.Sprintf("Flow status unknown: %v", err), 83 Code: "status.polling.flowstatus.error", 84 }) 85 } 86 87 return pc.responder.Data(flowStatus) 88 }