flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/controller/forms/simplepaymentform.go (about) 1 package forms 2 3 import ( 4 "context" 5 "errors" 6 7 priceDomain "flamingo.me/flamingo-commerce/v3/price/domain" 8 9 "flamingo.me/form/domain" 10 11 "flamingo.me/form/application" 12 13 "flamingo.me/flamingo/v3/framework/flamingo" 14 "flamingo.me/flamingo/v3/framework/web" 15 16 cartApplication "flamingo.me/flamingo-commerce/v3/cart/application" 17 cartDomain "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 18 ) 19 20 type ( 21 // SimplePaymentForm the form for simple select of payment gateway 22 SimplePaymentForm struct { 23 Gateway string `form:"gateway" validate:"required"` 24 Method string `form:"method" validate:"required"` 25 } 26 27 // SimplePaymentFormService implements Form(Data)Provider interface of form package 28 SimplePaymentFormService struct { 29 applicationCartReceiverService *cartApplication.CartReceiverService 30 giftCardPaymentMethod string 31 } 32 33 // SimplePaymentFormController the (mini) MVC 34 SimplePaymentFormController struct { 35 responder *web.Responder 36 applicationCartService *cartApplication.CartService 37 applicationCartReceiverService *cartApplication.CartReceiverService 38 39 logger flamingo.Logger 40 41 formHandlerFactory application.FormHandlerFactory 42 simplePaymentFormService *SimplePaymentFormService 43 } 44 ) 45 46 // Inject dependencies 47 func (p *SimplePaymentFormService) Inject( 48 applicationCartReceiverService *cartApplication.CartReceiverService, 49 config *struct { 50 GiftCardPaymentMethod string `inject:"config:commerce.cart.simplePaymentForm.giftCardPaymentMethod"` 51 }, 52 ) { 53 p.applicationCartReceiverService = applicationCartReceiverService 54 if config != nil { 55 p.giftCardPaymentMethod = config.GiftCardPaymentMethod 56 } 57 } 58 59 // GetFormData from data provider 60 func (p *SimplePaymentFormService) GetFormData(ctx context.Context, req *web.Request) (interface{}, error) { 61 session := web.SessionFromContext(ctx) 62 63 cart, err := p.applicationCartReceiverService.ViewCart(ctx, session) 64 65 if err != nil { 66 return SimplePaymentForm{}, nil 67 } 68 69 if cart.PaymentSelection != nil { 70 return SimplePaymentForm{ 71 Gateway: cart.PaymentSelection.Gateway(), 72 Method: cart.PaymentSelection.MethodByType(priceDomain.ChargeTypeMain), 73 }, nil 74 } 75 76 return SimplePaymentForm{}, nil 77 } 78 79 // Inject dependencies 80 func (c *SimplePaymentFormController) Inject(responder *web.Responder, 81 applicationCartService *cartApplication.CartService, 82 applicationCartReceiverService *cartApplication.CartReceiverService, 83 logger flamingo.Logger, 84 formHandlerFactory application.FormHandlerFactory, 85 simplePaymentFormService *SimplePaymentFormService, 86 ) { 87 c.responder = responder 88 c.applicationCartReceiverService = applicationCartReceiverService 89 c.applicationCartService = applicationCartService 90 91 c.formHandlerFactory = formHandlerFactory 92 c.logger = logger.WithField(flamingo.LogKeyModule, "cart").WithField(flamingo.LogKeyCategory, "simplepaymentform") 93 c.simplePaymentFormService = simplePaymentFormService 94 } 95 96 func (c *SimplePaymentFormController) getFormHandler() (domain.FormHandler, error) { 97 builder := c.formHandlerFactory.GetFormHandlerBuilder() 98 err := builder.SetFormService(c.simplePaymentFormService) 99 if err != nil { 100 return nil, err 101 } 102 return builder.Build(), nil 103 } 104 105 // GetUnsubmittedForm returns unsubmitted 106 func (c *SimplePaymentFormController) GetUnsubmittedForm(ctx context.Context, r *web.Request) (*domain.Form, error) { 107 formHandler, err := c.getFormHandler() 108 if err != nil { 109 return nil, err 110 } 111 return formHandler.HandleUnsubmittedForm(ctx, r) 112 } 113 114 // HandleFormAction return the form or error. If the form was submitted the action is performed 115 func (c *SimplePaymentFormController) HandleFormAction(ctx context.Context, r *web.Request) (form *domain.Form, actionSuccessFull bool, err error) { 116 session := web.SessionFromContext(ctx) 117 formHandler, err := c.getFormHandler() 118 if err != nil { 119 return nil, false, err 120 } 121 // ## Handle the submitted form (validation etc) 122 form, err = formHandler.HandleSubmittedForm(ctx, r) 123 if err != nil { 124 return nil, false, err 125 } 126 simplePaymentForm, ok := form.Data.(SimplePaymentForm) 127 if !ok { 128 return form, false, errors.New("cannot convert to SimplePaymentForm ") 129 } 130 if !form.IsValidAndSubmitted() { 131 return form, false, nil 132 } 133 currentCart, err := c.applicationCartReceiverService.ViewCart(ctx, r.Session()) 134 if err != nil { 135 return nil, false, err 136 } 137 138 paymentSelection := c.simplePaymentFormService.MapFormToPaymentSelection(simplePaymentForm, currentCart) 139 140 //update cart 141 err = c.applicationCartService.UpdatePaymentSelection(ctx, session, paymentSelection) 142 if err != nil { 143 c.logger.WithContext(ctx).Error("SimplePaymentFormController UpdatePaymentSelection Error %v", err) 144 return form, false, err 145 } 146 return form, true, nil 147 } 148 149 // MapFormToPaymentSelection maps form values to a valid payment selection 150 func (p *SimplePaymentFormService) MapFormToPaymentSelection(f SimplePaymentForm, currentCart *cartDomain.Cart) cartDomain.PaymentSelection { 151 chargeTypeToPaymentMethod := map[string]string{ 152 priceDomain.ChargeTypeMain: f.Method, 153 priceDomain.ChargeTypeGiftCard: p.giftCardPaymentMethod, 154 } 155 selection, _ := cartDomain.NewDefaultPaymentSelection(f.Gateway, chargeTypeToPaymentMethod, *currentCart) 156 return selection 157 }