21
21
import alfio .controller .form .UpdateTicketOwnerForm ;
22
22
import alfio .controller .support .SessionUtil ;
23
23
import alfio .controller .support .TicketDecorator ;
24
- import alfio .manager .EventManager ;
25
- import alfio .manager .NotificationManager ;
26
- import alfio .manager .StripeManager ;
27
- import alfio .manager .TicketReservationManager ;
24
+ import alfio .manager .*;
28
25
import alfio .manager .support .OrderSummary ;
29
26
import alfio .manager .support .PaymentResult ;
30
27
import alfio .manager .system .ConfigurationManager ;
40
37
import alfio .util .TemplateManager ;
41
38
import alfio .util .TemplateManager .TemplateOutput ;
42
39
import alfio .util .ValidationResult ;
40
+ import com .paypal .base .rest .PayPalRESTException ;
43
41
import org .apache .commons .lang3 .tuple .Pair ;
44
42
import org .apache .commons .lang3 .tuple .Triple ;
45
43
import org .springframework .beans .factory .annotation .Autowired ;
@@ -74,6 +72,7 @@ public class ReservationController {
74
72
private final OrganizationRepository organizationRepository ;
75
73
76
74
private final StripeManager stripeManager ;
75
+ private final PaypalManager paypalManager ;
77
76
private final TemplateManager templateManager ;
78
77
private final MessageSource messageSource ;
79
78
private final ConfigurationManager configurationManager ;
@@ -92,7 +91,8 @@ public ReservationController(EventRepository eventRepository,
92
91
ConfigurationManager configurationManager ,
93
92
NotificationManager notificationManager ,
94
93
TicketHelper ticketHelper ,
95
- TicketFieldRepository ticketFieldRepository ) {
94
+ TicketFieldRepository ticketFieldRepository ,
95
+ PaypalManager paypalManager ) {
96
96
this .eventRepository = eventRepository ;
97
97
this .eventManager = eventManager ;
98
98
this .ticketReservationManager = ticketReservationManager ;
@@ -104,22 +104,44 @@ public ReservationController(EventRepository eventRepository,
104
104
this .notificationManager = notificationManager ;
105
105
this .ticketHelper = ticketHelper ;
106
106
this .ticketFieldRepository = ticketFieldRepository ;
107
+ this .paypalManager = paypalManager ;
107
108
}
108
109
109
110
@ RequestMapping (value = "/event/{eventName}/reservation/{reservationId}/book" , method = RequestMethod .GET )
110
111
public String showPaymentPage (@ PathVariable ("eventName" ) String eventName ,
111
112
@ PathVariable ("reservationId" ) String reservationId ,
113
+ //paypal related parameters
114
+ @ RequestParam (value = "paymentId" , required = false ) String paypalPaymentId ,
115
+ @ RequestParam (value = "PayerID" , required = false ) String paypalPayerID ,
116
+ @ RequestParam (value = "paypal-success" , required = false ) Boolean isPaypalSuccess ,
117
+ @ RequestParam (value = "paypal-error" , required = false ) Boolean isPaypalError ,
118
+ @ RequestParam (value = "fullName" , required = false ) String fullName ,
119
+ @ RequestParam (value = "email" , required = false ) String email ,
120
+ @ RequestParam (value = "billingAddress" , required = false ) String billingAddress ,
121
+ @ RequestParam (value = "hmac" , required = false ) String hmac ,
112
122
Model model ,
113
123
Locale locale ) {
114
124
115
125
return eventRepository .findOptionalByShortName (eventName )
116
126
.map (event -> ticketReservationManager .findById (reservationId )
117
127
.map (reservation -> {
118
128
119
- if (reservation .getStatus () != TicketReservationStatus .PENDING ) {
129
+ if (reservation .getStatus () != TicketReservationStatus .PENDING ) {
120
130
return redirectReservation (Optional .of (reservation ), eventName , reservationId );
121
131
}
122
132
133
+ if (Boolean .TRUE .equals (isPaypalSuccess ) && paypalPayerID != null && paypalPaymentId != null ) {
134
+ model .addAttribute ("paypalPaymentId" , paypalPaymentId )
135
+ .addAttribute ("paypalPayerID" , paypalPayerID )
136
+ .addAttribute ("paypalCheckoutConfirmation" , true )
137
+ .addAttribute ("fullName" , fullName )
138
+ .addAttribute ("email" , email )
139
+ .addAttribute ("billingAddress" , billingAddress )
140
+ .addAttribute ("hmac" , hmac );
141
+ } else {
142
+ model .addAttribute ("paypalCheckoutConfirmation" , false );
143
+ }
144
+
123
145
OrderSummary orderSummary = ticketReservationManager .orderSummaryForReservationId (reservationId , event , locale );
124
146
model .addAttribute ("orderSummary" , orderSummary );
125
147
model .addAttribute ("reservationId" , reservationId );
@@ -335,13 +357,28 @@ public String handleReservation(@PathVariable("eventName") String eventName,
335
357
bindingResult .reject (ErrorsCode .STEP_2_ORDER_EXPIRED );
336
358
}
337
359
final TicketReservationManager .TotalPrice reservationCost = ticketReservationManager .totalReservationCostWithVAT (reservationId );
338
- paymentForm .validate (bindingResult , reservationCost , event . getAllowedPaymentProxies () );
360
+ paymentForm .validate (bindingResult , reservationCost , event );
339
361
if (bindingResult .hasErrors ()) {
340
362
SessionUtil .addToFlash (bindingResult , redirectAttributes );
341
363
return redirectReservation (ticketReservation , eventName , reservationId );
342
364
}
365
+
366
+ //handle paypal redirect!
367
+ if (paymentForm .getPaymentMethod () == PaymentProxy .PAYPAL && !paymentForm .hasPaypalTokens ()) {
368
+ OrderSummary orderSummary = ticketReservationManager .orderSummaryForReservationId (reservationId , event , locale );
369
+ try {
370
+ String checkoutUrl = paypalManager .createCheckoutRequest (event , reservationId , orderSummary , paymentForm .getFullName (), paymentForm .getEmail (), paymentForm .getBillingAddress (), locale );
371
+ return "redirect:" + checkoutUrl ;
372
+ } catch (Exception e ) {
373
+ bindingResult .reject (ErrorsCode .STEP_2_PAYMENT_REQUEST_CREATION );
374
+ return redirectReservation (ticketReservation , eventName , reservationId );
375
+ }
376
+ }
377
+ //
378
+
379
+
343
380
boolean directTicketAssignment = Optional .ofNullable (paymentForm .getExpressCheckoutRequested ()).map (b -> Boolean .logicalAnd (b , isExpressCheckoutEnabled (event , ticketReservationManager .orderSummaryForReservationId (reservationId , event , locale )))).orElse (false );
344
- final PaymentResult status = ticketReservationManager .confirm (paymentForm .getStripeToken (), event , reservationId , paymentForm .getEmail (),
381
+ final PaymentResult status = ticketReservationManager .confirm (paymentForm .getToken (), paymentForm . getPaypalPayerID (), event , reservationId , paymentForm .getEmail (),
345
382
paymentForm .getFullName (), locale , paymentForm .getBillingAddress (), reservationCost , SessionUtil .retrieveSpecialPriceSessionId (request ),
346
383
Optional .ofNullable (paymentForm .getPaymentMethod ()), directTicketAssignment );
347
384
0 commit comments