Skip to content

Commit

Permalink
#77 pass fullname/email/address to the reservation step
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jul 11, 2016
1 parent 4d6c6f8 commit c5b18b8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public String showPaymentPage(@PathVariable("eventName") String eventName,
@RequestParam(value = "PayerID", required = false) String paypalPayerID,
@RequestParam(value = "paypal-success", required = false) Boolean isPaypalSuccess,
@RequestParam(value = "paypal-error", required = false) Boolean isPaypalError,
@RequestParam(value = "fullName", required = false) String fullName,
@RequestParam(value = "email", required = false) String email,
@RequestParam(value = "billingAddress", required = false) String billingAddress,
Model model,
Locale locale) {

Expand All @@ -130,6 +133,9 @@ public String showPaymentPage(@PathVariable("eventName") String eventName,
model.addAttribute("paypalPaymentId", paypalPaymentId);
model.addAttribute("paypalPayerID", paypalPayerID);
model.addAttribute("paypalCheckoutConfirmation", true);
model.addAttribute("fullName", fullName);
model.addAttribute("email", email);
model.addAttribute("billingAddress", billingAddress);
} else {
model.addAttribute("paypalCheckoutConfirmation", false);
}
Expand Down Expand Up @@ -359,9 +365,9 @@ public String handleReservation(@PathVariable("eventName") String eventName,
if(paymentForm.getPaymentMethod() == PaymentProxy.PAYPAL && !paymentForm.hasPaypalTokens()) {
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);
try {
String checkoutUrl = paypalManager.createCheckoutRequest(event, reservationId, orderSummary, locale);
String checkoutUrl = paypalManager.createCheckoutRequest(event, reservationId, orderSummary, paymentForm.getFullName(), paymentForm.getEmail(), paymentForm.getBillingAddress(), locale);
return "redirect:"+checkoutUrl;
} catch (PayPalRESTException e) {
} catch (Exception e) {
//FIXME
throw new IllegalStateException(e);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/alfio/manager/PaymentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public PaymentResult processPaypalPayment(String reservationId, String token, St
ZonedDateTime.now(), price, event.getCurrency(), "FIXME DESCRIPTION", PaymentProxy.PAYPAL.name());
return PaymentResult.successful(transactionId);
} catch (Exception e) {
log.warn("errow while processing paypal payment", e);
return PaymentResult.unsuccessful("FIXME PAYPAL ERROR HANDLING");//FIXME
}
}
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/alfio/manager/PaypalManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -81,7 +82,7 @@ private List<Transaction> buildPaymentDetails(Event event, OrderSummary orderSum
return transactions;
}

public String createCheckoutRequest(Event event, String reservationId, OrderSummary orderSummary, Locale locale) throws PayPalRESTException {
public String createCheckoutRequest(Event event, String reservationId, OrderSummary orderSummary, String fullName, String email, String billingAddress, Locale locale) throws Exception {

List<Transaction> transactions = buildPaymentDetails(event, orderSummary);
String eventName = event.getShortName();
Expand All @@ -99,25 +100,39 @@ public String createCheckoutRequest(Event event, String reservationId, OrderSumm
String baseUrl = StringUtils.removeEnd(configurationManager.getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.BASE_URL)), "/");
String bookUrl = baseUrl+"/event/" + eventName + "/reservation/" + reservationId + "/book";

redirectUrls.setCancelUrl(bookUrl + "?paypal-cancel=true");
redirectUrls.setReturnUrl(bookUrl + "?paypal-success=true");
UriComponentsBuilder bookUrlBuilder = UriComponentsBuilder.fromUriString(bookUrl).queryParam("fullName", fullName).queryParam("email", email).queryParam("billingAddress", billingAddress);
String finalUrl = bookUrlBuilder.toUriString();

redirectUrls.setCancelUrl(finalUrl + "&paypal-cancel=true");
redirectUrls.setReturnUrl(finalUrl + "&paypal-success=true");
payment.setRedirectUrls(redirectUrls);

Payment createdPayment = payment.create(getApiContext(event));

//FIXME add additional 15 minutes to the reservation


if(!"created".equals(createdPayment.getState())) {
throw new Exception(createdPayment.getFailureReason());
}

//extract url for approval
return createdPayment.getLinks().stream().filter(l -> "approval_url".equals(l.getRel())).findFirst().map(Links::getHref).orElseThrow(IllegalStateException::new);

}

public String commitPayment(String token, String payerId, Event event) throws Exception {
public String commitPayment(String token, String payerId, Event event) throws Exception {

Payment payment = new Payment();
payment.setId(token);
PaymentExecution paymentExecute = new PaymentExecution();
paymentExecute.setPayerId(payerId);
Payment result = payment.execute(getApiContext(event), paymentExecute);

if(!"approved".equals(result.getState())) {
throw new Exception(result.getFailureReason());
}

//return result.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
//FIXME fetch correct id
return result.getId();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ public PaymentResult confirm(String gatewayToken, String payerId, Event event, S
break;
case PAYPAL:
paymentResult = paymentManager.processPaypalPayment(reservationId, gatewayToken, payerId, reservationCost.getPriceWithVAT(), event);
if(!paymentResult.isSuccessful()) {
reTransitionToPending(reservationId);
return paymentResult;
}
break;
case OFFLINE:
transitionToOfflinePayment(event, reservationId, email, fullName, billingAddress);
Expand Down
8 changes: 4 additions & 4 deletions src/main/webapp/WEB-INF/templates/event/reservation-page.ms
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@
<form action="{{request.contextPath}}/event/{{event.shortName}}/reservation/{{reservation.id}}" method="POST" id="payment-form">
{{#paypalCheckoutConfirmation}}

<input type="hidden" name="fullName" value="blabla">
<input type="hidden" name="email" value="[email protected]">
<input type="hidden" name="billingAddress" value="billingAddress">
<input type="hidden" name="fullName" value="{{fullName}}">
<input type="hidden" name="email" value="{{email}}">
<input type="hidden" name="billingAddress" value="{{billingAddress}}">
<input type="hidden" name="paymentMethod" value="PAYPAL">
<input type="hidden" name="paypalPaymentId" value="{{paypalPaymentId}}">
<input type="hidden" name="paypalPayerID" value="{{paypalPayerID}}">
<input type="hidden" name="termAndConditionsAccepted" value="true">

FIXME: CONFIRM PAYPAL PAYMENT
{{/paypalCheckoutConfirmation}}
FIXME: CONFIRM PAYPAL PAYMENT
{{^paypalCheckoutConfirmation}}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void reservationFlowTest() throws Exception{


// check that the payment page is shown
String reservationPage = reservationController.showPaymentPage(eventName, reservationIdentifier, null, null, null, null, new BindingAwareModelMap(), Locale.ENGLISH);
String reservationPage = reservationController.showPaymentPage(eventName, reservationIdentifier, null, null, null, null, null, null, null, new BindingAwareModelMap(), Locale.ENGLISH);
Assert.assertEquals("/event/reservation-page", reservationPage);
//

Expand Down

0 comments on commit c5b18b8

Please sign in to comment.