-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jv-jun23-team5/feat-payment
[PAYMENT] Implemented beta controller and service, added swagger to the controller
- Loading branch information
Showing
26 changed files
with
357 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/project/carsharingapp/controller/PaymentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.project.carsharingapp.controller; | ||
|
||
import com.project.carsharingapp.dto.payment.CreatePaymentSessionRequestDto; | ||
import com.project.carsharingapp.model.Payment; | ||
import com.project.carsharingapp.service.PaymentService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Payment management", description = "Endpoint for managing payments") | ||
@RestController | ||
@RequestMapping("/payments") | ||
@RequiredArgsConstructor | ||
public class PaymentController { | ||
private final PaymentService paymentService; | ||
|
||
@GetMapping | ||
@Operation(summary = "Get all user's payments") | ||
public List<Payment> getAll(Pageable pageable) { | ||
return paymentService.getAll(pageable); | ||
} | ||
|
||
@PostMapping | ||
@Operation(summary = "Create a new payment session") | ||
public Payment create( | ||
@RequestBody @Valid CreatePaymentSessionRequestDto requestDto | ||
) { | ||
return paymentService.create(requestDto); | ||
} | ||
|
||
@GetMapping("/success") | ||
@Operation(summary = "Redirect endpoint in case of successful payment") | ||
private String redirectToSuccessPage(@RequestParam String sessionId) { | ||
paymentService.updateStatus(sessionId); | ||
return "success"; | ||
} | ||
|
||
@GetMapping("/cancel") | ||
@Operation(summary = "Redirect endpoint in case of paused payment") | ||
private String redirectToFailedPage() { | ||
return "cancel"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/project/carsharingapp/dto/payment/CreatePaymentSessionRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.project.carsharingapp.dto.payment; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class CreatePaymentSessionRequestDto { | ||
@NotNull | ||
private Long rentalId; | ||
@NotNull | ||
private String type; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/project/carsharingapp/dto/payment/PaymentResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.project.carsharingapp.dto.payment; | ||
|
||
public class PaymentResponseDto { | ||
} |
8 changes: 5 additions & 3 deletions
8
src/main/java/com/project/carsharingapp/dto/rental/RentalSearchParametersDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.project.carsharingapp.dto.rental; | ||
|
||
public record RentalSearchParametersDto(String[] userId, | ||
String[] isActive) { | ||
} | ||
public record RentalSearchParametersDto( | ||
String[] userId, | ||
String[] isActive | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/com/project/carsharingapp/repository/PaymentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package com.project.carsharingapp.repository; | ||
|
||
import com.project.carsharingapp.model.Payment; | ||
import java.util.Optional; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PaymentRepository extends JpaRepository<Payment, Long> { | ||
Optional<Payment> findBySessionId(String sessionId); | ||
|
||
Page<Payment> findAll(Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...ain/java/com/project/carsharingapp/repository/rentals/spec/UserSpecificationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/project/carsharingapp/service/PaymentAmountHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.project.carsharingapp.service; | ||
|
||
import com.project.carsharingapp.model.Payment; | ||
import java.math.BigDecimal; | ||
|
||
public interface PaymentAmountHandler { | ||
Long getPaymentAmount(BigDecimal dailyFee, int numberOfDays); | ||
|
||
boolean isApplicable(Payment.Type type); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/project/carsharingapp/service/PaymentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.project.carsharingapp.service; | ||
|
||
import com.project.carsharingapp.dto.payment.CreatePaymentSessionRequestDto; | ||
import com.project.carsharingapp.model.Payment; | ||
import java.util.List; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface PaymentService { | ||
Payment create(CreatePaymentSessionRequestDto requestDto); | ||
|
||
Payment updateStatus(String sessionId); | ||
|
||
List<Payment> getAll(Pageable pageable); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/project/carsharingapp/service/impl/FineAmountHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.project.carsharingapp.service.impl; | ||
|
||
import com.project.carsharingapp.model.Payment; | ||
import com.project.carsharingapp.service.PaymentAmountHandler; | ||
import java.math.BigDecimal; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class FineAmountHandler implements PaymentAmountHandler { | ||
private static final BigDecimal FINE_MULTIPLIER = BigDecimal.valueOf(1.25); | ||
|
||
@Override | ||
public Long getPaymentAmount(BigDecimal dailyFee, int numberOfDays) { | ||
return dailyFee.multiply(FINE_MULTIPLIER).longValue() * numberOfDays; | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(Payment.Type type) { | ||
return type.equals(Payment.Type.FINE); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/project/carsharingapp/service/impl/PaymentAmountHandlerStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.project.carsharingapp.service.impl; | ||
|
||
import com.project.carsharingapp.model.Payment; | ||
import com.project.carsharingapp.service.PaymentAmountHandler; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PaymentAmountHandlerStrategy { | ||
@Autowired | ||
private List<PaymentAmountHandler> paymentAmountHandlers; | ||
|
||
public PaymentAmountHandler getHandler(Payment.Type type) { | ||
return paymentAmountHandlers.stream() | ||
.filter(paymentAmountHandler -> paymentAmountHandler.isApplicable(type)) | ||
.findAny() | ||
.orElseThrow(() -> | ||
new NoSuchElementException("Can't find a PaymentAmountHandler " | ||
+ "for such type as: " + type) | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/project/carsharingapp/service/impl/PaymentServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.project.carsharingapp.service.impl; | ||
|
||
import com.project.carsharingapp.dto.payment.CreatePaymentSessionRequestDto; | ||
import com.project.carsharingapp.exception.EntityNotFoundException; | ||
import com.project.carsharingapp.model.Payment; | ||
import com.project.carsharingapp.model.Rental; | ||
import com.project.carsharingapp.repository.PaymentRepository; | ||
import com.project.carsharingapp.repository.rentals.RentalRepository; | ||
import com.project.carsharingapp.service.PaymentService; | ||
import com.stripe.exception.StripeException; | ||
import com.stripe.model.checkout.Session; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PaymentServiceImpl implements PaymentService { | ||
private final RentalRepository rentalRepository; | ||
private final PaymentRepository paymentRepository; | ||
private final StripeService stripeService; | ||
|
||
@Override | ||
public Payment create(CreatePaymentSessionRequestDto requestDto) { | ||
Rental rental = rentalRepository.findById(requestDto.getRentalId()).orElseThrow( | ||
() -> new EntityNotFoundException("Can't find a Rental by id: " | ||
+ requestDto.getRentalId()) | ||
); | ||
Payment.Type type = Payment.Type.valueOf(requestDto.getType()); | ||
try { | ||
Session session = stripeService.createSession(rental, type); | ||
Payment payment = generatePayment(session, rental, type); | ||
return paymentRepository.save(payment); | ||
} catch (StripeException e) { | ||
throw new RuntimeException("Can't create a stripe checkout session!"); | ||
} | ||
} | ||
|
||
@Override | ||
public Payment updateStatus(String sessionId) { | ||
Payment payment = paymentRepository.findBySessionId(sessionId).orElseThrow( | ||
() -> new EntityNotFoundException("Can't find a Payment by the session") | ||
); | ||
payment.setStatus(Payment.Status.PAID); | ||
return paymentRepository.save(payment); | ||
} | ||
|
||
@Override | ||
public List<Payment> getAll(Pageable pageable) { | ||
return paymentRepository.findAll(pageable).stream().collect(Collectors.toList()); | ||
} | ||
|
||
private Payment generatePayment(Session session, Rental rental, Payment.Type type) { | ||
Payment payment = new Payment(); | ||
payment.setStatus(Payment.Status.PENDING); | ||
payment.setType(type); | ||
payment.setRental(rental); | ||
payment.setSessionUrl(session.getUrl()); | ||
payment.setSessionId(session.getId()); | ||
payment.setAmount(BigDecimal.valueOf(session.getAmountTotal())); | ||
return payment; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/project/carsharingapp/service/impl/RegularPaymentAmountHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.project.carsharingapp.service.impl; | ||
|
||
import com.project.carsharingapp.model.Payment; | ||
import com.project.carsharingapp.service.PaymentAmountHandler; | ||
import java.math.BigDecimal; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RegularPaymentAmountHandler implements PaymentAmountHandler { | ||
@Override | ||
public Long getPaymentAmount(BigDecimal dailyFee, int numberOfDays) { | ||
return dailyFee.longValue() * numberOfDays; | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(Payment.Type type) { | ||
return type.equals(Payment.Type.PAYMENT); | ||
} | ||
} |
Oops, something went wrong.