Skip to content

Commit

Permalink
PayPal manager
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Sep 6, 2018
1 parent a655e98 commit 54efab1
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ public String handleReservation(@PathVariable("eventName") String eventName,

OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);

PaymentSpecification spec = new PaymentSpecification( reservationId, paymentForm.getToken(), reservationCost.getPriceWithVAT(),
PaymentSpecification spec = new PaymentSpecification(reservationId, paymentForm.getToken(), reservationCost.getPriceWithVAT(),
event, ticketReservation.getEmail(), customerName, ticketReservation.getBillingAddress(), ticketReservation.getCustomerReference(),
paymentForm.getPaypalPayerID(), locale, ticketReservation.isInvoiceRequested(), !ticketReservation.isDirectAssignmentRequested(),
orderSummary, ticketReservation.getVatCountryCode(), ticketReservation.getVatNr(), ticketReservation.getVatStatus(),
Boolean.TRUE.equals(paymentForm.getTermAndConditionsAccepted()), Boolean.TRUE.equals(paymentForm.getPrivacyPolicyAccepted()));
Boolean.TRUE.equals(paymentForm.getTermAndConditionsAccepted()), Boolean.TRUE.equals(paymentForm.getPrivacyPolicyAccepted()), paymentForm.getHmac());

final PaymentResult status = ticketReservationManager.performPayment(spec, reservationCost, SessionUtil.retrieveSpecialPriceSessionId(request),
Optional.ofNullable(paymentForm.getPaymentMethod()));
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/alfio/controller/form/PaymentForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public void validate(BindingResult bindingResult, Event event, TotalPrice reserv
|| (StringUtils.isNotEmpty(event.getPrivacyPolicyUrl()) && (Objects.isNull(privacyPolicyAccepted) || !privacyPolicyAccepted))) {
bindingResult.reject(ErrorsCode.STEP_2_TERMS_NOT_ACCEPTED);
}

if (hasPaypalTokens() /*&& !PaypalManager.isValidHMAC(new CustomerName(fullName, firstName, lastName, event), email, billingAddress, hmac, event)*/) {
bindingResult.reject(ErrorsCode.STEP_2_INVALID_HMAC);
}
}

public Boolean shouldCancelReservation() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/alfio/manager/AdminReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ private Result<Triple<TicketReservation, List<Ticket>, Event>> loadReservation(S

private Result<Triple<TicketReservation, List<Ticket>, Event>> performConfirmation(String reservationId, Event event, TicketReservation original) {
try {
PaymentSpecification spec = new PaymentSpecification( reservationId, null, 0,
PaymentSpecification spec = new PaymentSpecification(reservationId, null, 0,
event, original.getEmail(), new CustomerName(original.getFullName(), original.getFirstName(), original.getLastName(), event),
original.getBillingAddress(), original.getCustomerReference(), null, Locale.forLanguageTag(original.getUserLanguage()),
false, false, null, null, null, null, false, false );
false, false, null, null, null, null, false, false, null);

ticketReservationManager.completeReservation(spec, Optional.empty(), PaymentProxy.ADMIN);
return loadReservation(reservationId);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/alfio/manager/PaymentSpecification.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class PaymentSpecification {
private final PriceContainer.VatStatus vatStatus;
private final boolean tcAccepted;
private final boolean privacyAccepted;
private final String validationToken;

public PaymentSpecification( String reservationId,
String gatewayToken,
Expand All @@ -62,7 +63,8 @@ public PaymentSpecification( String reservationId,
String vatNr,
PriceContainer.VatStatus vatStatus,
boolean tcAccepted,
boolean privacyAccepted) {
boolean privacyAccepted,
String validationToken) {
this.reservationId = reservationId;
this.gatewayToken = gatewayToken;
this.priceWithVAT = priceWithVAT;
Expand All @@ -81,9 +83,10 @@ public PaymentSpecification( String reservationId,
this.vatStatus = vatStatus;
this.tcAccepted = tcAccepted;
this.privacyAccepted = privacyAccepted;
this.validationToken = validationToken;
}

PaymentSpecification( String reservationId, String gatewayToken, int priceWithVAT, Event event, String email, CustomerName customerName ) {
this(reservationId, gatewayToken, priceWithVAT, event, email, customerName, null, null, null, null, false, false, null, null, null, null, false, false);
this(reservationId, gatewayToken, priceWithVAT, event, email, customerName, null, null, null, null, false, false, null, null, null, null, false, false, null);
}
}
19 changes: 11 additions & 8 deletions src/main/java/alfio/manager/PaypalManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public static boolean isValidHMAC(CustomerName customerName, String email, Strin
return MessageDigest.isEqual(hmac.getBytes(StandardCharsets.UTF_8), computedHmac.getBytes(StandardCharsets.UTF_8));
}

public static class HandledPaypalErrorException extends RuntimeException {
HandledPaypalErrorException(String errorMessage) {
public static class HandledPayPalErrorException extends RuntimeException {
HandledPayPalErrorException(String errorMessage) {
super(errorMessage);
}
}
Expand All @@ -216,12 +216,12 @@ public Pair<String, String> commitPayment(String reservationId, String token, St
Payment payment = new Payment().setId(token);
PaymentExecution paymentExecute = new PaymentExecution();
paymentExecute.setPayerId(payerId);
Payment result = null;
Payment result;
try {
result = payment.execute(getApiContext(event), paymentExecute);
} catch (PayPalRESTException e) {
mappedException(e).ifPresent(message -> {
throw new HandledPaypalErrorException(message);
throw new HandledPayPalErrorException(message);
});
throw e;
}
Expand Down Expand Up @@ -321,8 +321,8 @@ public boolean accept( PaymentMethod paymentMethod, Function<ConfigurationKeys,
}

@Override
public PaymentResult doPayment( PaymentSpecification spec ) {
return hasPayPalTokens( spec ) ? confirmPayment( spec ) : preparePayment( spec );
public PaymentResult doPayment(PaymentSpecification spec) {
return hasPayPalTokens(spec) ? confirmPayment(spec) : preparePayment(spec);
}

private boolean hasPayPalTokens(PaymentSpecification spec ) {
Expand All @@ -337,8 +337,11 @@ private PaymentResult preparePayment( PaymentSpecification spec ) {
}
}

private PaymentResult confirmPayment( PaymentSpecification spec ) {
private PaymentResult confirmPayment(PaymentSpecification spec) {
try {
if(!PaypalManager.isValidHMAC(spec.getCustomerName(), spec.getEmail(), spec.getBillingAddress(), spec.getValidationToken(), spec.getEvent())) {
return PaymentResult.failed(ErrorsCode.STEP_2_INVALID_HMAC);
}
Pair<String, String> captureAndPaymentId = commitPayment(spec.getReservationId(), spec.getGatewayToken(), spec.getPayerId(), spec.getEvent());
String captureId = captureAndPaymentId.getLeft();
String paymentId = captureAndPaymentId.getRight();
Expand All @@ -359,7 +362,7 @@ private PaymentResult confirmPayment( PaymentSpecification spec ) {
log.warn("errow while processing paypal payment: " + e.getMessage(), e);
if(e instanceof PayPalRESTException ) {
return PaymentResult.failed(ErrorsCode.STEP_2_PAYPAL_UNEXPECTED);
} else if(e instanceof HandledPaypalErrorException) {
} else if(e instanceof HandledPayPalErrorException) {
return PaymentResult.failed(e.getMessage());
}
throw new IllegalStateException(e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/alfio/model/transaction/PaymentProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface PaymentProvider {

boolean accept(PaymentMethod paymentMethod, Function<ConfigurationKeys, Configuration.ConfigurationPathKey> contextProvider);

PaymentResult doPayment( PaymentSpecification spec );
PaymentResult doPayment(PaymentSpecification spec);

boolean supportRefund();
}
4 changes: 0 additions & 4 deletions src/test/java/alfio/manager/EuVatCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
import java.util.Optional;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.isNull;
import static org.mockito.Mockito.*;

public class EuVatCheckerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void testTicketSelection() {

PaymentSpecification specification = new PaymentSpecification(reservationId, null, totalPrice.getPriceWithVAT(),
event, "[email protected]", new CustomerName("full name", "full", "name", event),
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);

PaymentResult confirm = ticketReservationManager.performPayment(specification, totalPrice, Optional.empty(), Optional.of(PaymentProxy.OFFLINE));
assertTrue(confirm.isSuccessful());
Expand Down Expand Up @@ -205,7 +205,7 @@ public void testTicketSelection() {

PaymentSpecification specification2 = new PaymentSpecification(reservationId2, null, totalPrice.getPriceWithVAT(),
event, "[email protected]", new CustomerName("full name", "full", "name", event),
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);

PaymentResult confirm2 = ticketReservationManager.performPayment(specification2, totalPrice, Optional.empty(), Optional.of(PaymentProxy.OFFLINE));
assertTrue(confirm2.isSuccessful());
Expand Down Expand Up @@ -360,7 +360,7 @@ public void testDeletePendingPaymentUnboundedCategory() {
TotalPrice reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
PaymentSpecification specification = new PaymentSpecification(reservationId, null, reservationCost.getPriceWithVAT(),
event, "[email protected]", new CustomerName("full name", "full", "name", event),
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);
PaymentResult result = ticketReservationManager.performPayment(specification, reservationCost, Optional.empty(), Optional.of(PaymentProxy.OFFLINE));
assertTrue(result.isSuccessful());
ticketReservationManager.deleteOfflinePayment(event, reservationId, false);
Expand All @@ -371,7 +371,7 @@ public void testDeletePendingPaymentUnboundedCategory() {
reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
PaymentSpecification specification2 = new PaymentSpecification(reservationId, null, reservationCost.getPriceWithVAT(),
event, "[email protected]", new CustomerName("full name", "full", "name", event),
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
"billing address", null, null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);
result = ticketReservationManager.performPayment(specification2, reservationCost, Optional.empty(), Optional.of(PaymentProxy.OFFLINE));
assertTrue(result.isSuccessful());
}
Expand Down
18 changes: 11 additions & 7 deletions src/test/java/alfio/manager/TicketReservationManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import alfio.repository.user.OrganizationRepository;
import alfio.repository.user.UserRepository;
import alfio.util.TemplateManager;
import alfio.util.WorkingDaysAdjusters;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.MessageSource;
Expand Down Expand Up @@ -411,14 +412,17 @@ void returnTheExpiredDateAsConfigured() {
initOfflinePaymentTest();
when(event.getBegin()).thenReturn(ZonedDateTime.now().plusDays(3));
ZonedDateTime offlinePaymentDeadline = BankTransferManager.getOfflinePaymentDeadline(event, configurationManager);
assertEquals(2L, ChronoUnit.DAYS.between(LocalDate.now(), offlinePaymentDeadline.toLocalDate()));
ZonedDateTime expectedDate = ZonedDateTime.now().plusDays(2L).truncatedTo(ChronoUnit.HALF_DAYS).with(WorkingDaysAdjusters.defaultWorkingDays());
assertEquals(expectedDate, offlinePaymentDeadline);
}

@Test
void returnTheConfiguredWaitingTime() {
initOfflinePaymentTest();
when(event.getBegin()).thenReturn(ZonedDateTime.now().plusDays(3));
assertEquals(2, BankTransferManager.getOfflinePaymentWaitingPeriod(event, configurationManager).getAsInt());
OptionalInt offlinePaymentWaitingPeriod = BankTransferManager.getOfflinePaymentWaitingPeriod(event, configurationManager);
assertTrue(offlinePaymentWaitingPeriod.isPresent());
assertEquals(2, offlinePaymentWaitingPeriod.getAsInt());
}

@Test
Expand All @@ -441,7 +445,7 @@ void neverReturnADateInThePast() {
initOfflinePaymentTest();
when(event.getBegin()).thenReturn(ZonedDateTime.now());
ZonedDateTime offlinePaymentDeadline = BankTransferManager.getOfflinePaymentDeadline(event, configurationManager);
assertEquals(true, offlinePaymentDeadline.isAfter(ZonedDateTime.now()));
assertTrue(offlinePaymentDeadline.isAfter(ZonedDateTime.now()));
}

// FIXME implement test
Expand Down Expand Up @@ -688,7 +692,7 @@ void confirmPaidReservation() {
when(stripeCreditCardManager.doPayment(any())).thenReturn(PaymentResult.successful(TRANSACTION_ID));
PaymentSpecification spec = new PaymentSpecification(RESERVATION_ID, GATEWAY_TOKEN, 100, event, "test@email",
new CustomerName("Full Name", null, null, event), "", null, null, Locale.ENGLISH,
true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);
PaymentResult result = trm.performPayment(spec, new TotalPrice(100, 0, 0, 0), Optional.empty(), Optional.of(PaymentProxy.STRIPE));
assertTrue(result.isSuccessful());
assertEquals(Optional.of(TRANSACTION_ID), result.getGatewayTransactionId());
Expand All @@ -712,7 +716,7 @@ void returnFailureCodeIfPaymentNotSuccessful() {
StripeCreditCardManager stripeCreditCardManager = mock(StripeCreditCardManager.class);
when(paymentManager.lookupProviderByMethod(eq(PaymentMethod.CREDIT_CARD), any())).thenReturn(Optional.of(stripeCreditCardManager));
when(stripeCreditCardManager.doPayment(any())).thenReturn(PaymentResult.failed("error-code"));
PaymentSpecification spec = new PaymentSpecification(RESERVATION_ID, GATEWAY_TOKEN, 100, event, "email@user", new CustomerName("Full Name", null, null, event), null, null, "", Locale.ENGLISH, true, false, null, "IT", "12345", PriceContainer.VatStatus.INCLUDED, true, false);
PaymentSpecification spec = new PaymentSpecification(RESERVATION_ID, GATEWAY_TOKEN, 100, event, "email@user", new CustomerName("Full Name", null, null, event), null, null, "", Locale.ENGLISH, true, false, null, "IT", "12345", PriceContainer.VatStatus.INCLUDED, true, false, null);
PaymentResult result = trm.performPayment(spec, new TotalPrice(100, 0, 0, 0), Optional.empty(), Optional.of(PaymentProxy.STRIPE));
assertFalse(result.isSuccessful());
assertFalse(result.getGatewayTransactionId().isPresent());
Expand All @@ -734,7 +738,7 @@ void handleOnSitePaymentMethod() {
when(onSiteManager.doPayment(any())).thenReturn(PaymentResult.successful(TicketReservationManager.NOT_YET_PAID_TRANSACTION_ID));
PaymentSpecification spec = new PaymentSpecification(RESERVATION_ID, GATEWAY_TOKEN, 100, event, "test@email",
new CustomerName("Full Name", null, null, event),
"", null, "", Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
"", null, "", Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);
when(ticketReservationRepository.updateTicketReservation(eq(RESERVATION_ID), anyString(), anyString(), anyString(), isNull(), isNull(), eq(Locale.ENGLISH.getLanguage()), isNull(), any(), any(), isNull())).thenReturn(1);
PaymentResult result = trm.performPayment(spec, new TotalPrice(100, 0, 0, 0), Optional.empty(), Optional.of(PaymentProxy.ON_SITE));
assertTrue(result.isSuccessful());
Expand All @@ -759,7 +763,7 @@ void handleOfflinePaymentMethod() {
when(bankTransferManager.doPayment(any())).thenReturn(PaymentResult.successful(TicketReservationManager.NOT_YET_PAID_TRANSACTION_ID));
PaymentSpecification spec = new PaymentSpecification(RESERVATION_ID, GATEWAY_TOKEN, 100, event, "test@email",
new CustomerName("Full Name", null, null, event),
"", null, "", Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
"", null, "", Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false, null);
PaymentResult result = trm.performPayment(spec, new TotalPrice(100, 0, 0, 0), Optional.empty(), Optional.of(PaymentProxy.OFFLINE));
assertTrue(result.isSuccessful());
assertEquals(Optional.of(TicketReservationManager.NOT_YET_PAID_TRANSACTION_ID), result.getGatewayTransactionId());
Expand Down
Loading

0 comments on commit 54efab1

Please sign in to comment.