Skip to content

Commit

Permalink
fix code following cherry-pick from master
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Jan 27, 2020
1 parent 51147c9 commit cf53a36
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ dependencies {
compile "com.zaxxer:HikariCP:3.3.1"
compile "org.apache.logging.log4j:log4j-api:$log4jVersion"
compile "com.stripe:stripe-java:11.7.0"
compile 'com.paypal.sdk:rest-api-sdk:1.14.0'
implementation 'com.paypal.sdk:checkout-sdk:1.0.2'
implementation 'com.google.code.gson:gson:2.8.6'
compile 'com.squareup.okhttp3:okhttp:3.13.1'
compile "org.apache.commons:commons-lang3:3.9"
compile "org.apache.commons:commons-text:1.6"
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,14 @@ public PaymentResult performPayment(PaymentSpecification spec,
return PaymentResult.failed("error.STEP2_UNABLE_TO_TRANSITION");
}

TicketReservation reservation = ticketReservationRepository.findReservationById(spec.getReservationId());
TicketReservation reservation = null;

try {
PaymentResult paymentResult;
ticketReservationRepository.lockReservationForUpdate(spec.getReservationId());
reservation = ticketReservationRepository.findReservationByIdForUpdate(spec.getReservationId());
if(reservation.getStatus() == COMPLETE) {
return PaymentResult.successful("");
}
//save billing data in case we have to go back to PENDING
ticketReservationRepository.updateBillingData(spec.getVatStatus(), reservation.getSrcPriceCts(), reservation.getFinalPriceCts(),
reservation.getVatCts(), reservation.getDiscountCts(), reservation.getCurrencyCode(), spec.getVatNr(), spec.getVatCountryCode(), spec.isInvoiceRequested(), spec.getReservationId());
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/alfio/manager/payment/PayPalManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import alfio.manager.system.ConfigurationManager;
import alfio.model.*;
import alfio.model.system.ConfigurationKeys;
import alfio.model.transaction.PaymentMethod;
import alfio.model.transaction.*;
import alfio.model.transaction.capabilities.ExtractPaymentTokenFromTransaction;
import alfio.model.transaction.capabilities.PaymentInfo;
Expand All @@ -33,7 +34,6 @@
import alfio.repository.TicketReservationRepository;
import alfio.repository.TransactionRepository;
import alfio.util.ErrorsCode;
import alfio.util.HttpUtils;
import alfio.util.Json;
import alfio.util.MonetaryUtil;
import com.paypal.core.PayPalEnvironment;
Expand All @@ -48,6 +48,7 @@
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;

Expand All @@ -65,6 +66,7 @@

import static alfio.model.system.ConfigurationKeys.PAYPAL_ENABLED;
import static alfio.util.MonetaryUtil.*;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;

@Component
Expand Down Expand Up @@ -120,7 +122,7 @@ private String createCheckoutRequest(PaymentSpecification spec) throws Exception
OrdersCreateRequest request = new OrdersCreateRequest().requestBody(orderRequest);
request.header("PayPal-Request-Id", reservation.getId());
HttpResponse<Order> response = getClient(spec.getEvent()).execute(request);
if(HttpUtils.statusCodeIsSuccessful(response.statusCode())) {
if(requireNonNull(HttpStatus.resolve(response.statusCode())).is2xxSuccessful()) {
Order order = response.result();

if(!"CREATED".equals(order.status())) {
Expand Down Expand Up @@ -172,7 +174,7 @@ private PayPalChargeDetails commitPayment(String reservationId, PayPalToken payP
request.requestBody(new OrderRequest());
HttpResponse<Order> response = getClient(event).execute(request);

if(HttpUtils.statusCodeIsSuccessful(response.statusCode())) {
if(statusCodeIsSuccessful(response)) {
var result = response.result();
// state can only be "created", "approved" or "failed".
// if we are at this stage, the only possible options are approved or failed, thus it's safe to re transition the reservation to a pending status: no payment has been made!
Expand Down Expand Up @@ -207,7 +209,7 @@ private Optional<PaymentInformation> getInfo(Transaction transaction, EventAndOr
try {
if(paymentId != null) {
var orderResponse = getClient(event).execute(new OrdersGetRequest(paymentId));
if(HttpUtils.statusCodeIsSuccessful(orderResponse.statusCode()) && orderResponse.result() != null) {
if(statusCodeIsSuccessful(orderResponse) && orderResponse.result() != null) {
var order = orderResponse.result();
var payments = order.purchaseUnits().stream()
.map(PurchaseUnit::payments)
Expand Down Expand Up @@ -261,7 +263,7 @@ public boolean refund(alfio.model.transaction.Transaction transaction, Event eve
new com.paypal.payments.RefundRequest().amount(new com.paypal.payments.Money().currencyCode(currency).value(formatCents(i, currency))))
);
var refundResponse = payPalClient.execute(refundRequest);
if(HttpUtils.statusCodeIsSuccessful(refundResponse.statusCode())) {
if(statusCodeIsSuccessful(refundResponse)) {
log.info("Paypal: refund for payment {} executed with success for amount: {}", captureId, amountOrFull);
return true;
} else {
Expand Down Expand Up @@ -371,4 +373,8 @@ private static class PayPalChargeDetails {
private final long payPalFee;
}

private static boolean statusCodeIsSuccessful(HttpResponse<?> response) {
return requireNonNull(HttpStatus.resolve(response.statusCode())).is2xxSuccessful();
}

}
3 changes: 3 additions & 0 deletions src/main/java/alfio/repository/TransactionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ int updateIfStatus(@Bind("transactionId") int id,
@Query(SELECT_VALID_BY_RESERVATION_ID)
Optional<Transaction> loadOptionalByReservationId(@Bind("reservationId") String reservationId);

@Query(SELECT_VALID_BY_RESERVATION_ID + " and status = :status")
Optional<Transaction> loadOptionalByReservationIdAndStatus(@Bind("reservationId") String reservationId, @Bind("status") Transaction.Status status);

@Query("select * from b_transaction where id = :id and status = :status")
Optional<Transaction> loadOptionalByIdAndStatus(@Bind("id") int id, @Bind("status") Transaction.Status status);

Expand Down

0 comments on commit cf53a36

Please sign in to comment.