-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PAYMENT] Implemented beta controller and service, added swagger to the controller #16
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6c00b43
added base stucture for paymentController, added Payment dtos and sam…
ivan0dyatlyukkk 736530f
Merge branch 'main' into feat-payment
ivan0dyatlyukkk ab6a03d
Merge branch 'main' into feat-payment
ivan0dyatlyukkk 7578376
added stategy for choosing payment type, created initial structures f…
ivan0dyatlyukkk 04bea7c
testing creating payment session
ivan0dyatlyukkk 93cf475
implemented create payment and create stripe session endpoint, adjust…
ivan0dyatlyukkk 17640a5
improved payment controller, service and repository, adjusted stripe …
ivan0dyatlyukkk 62657c5
added swagger to the payment controller and fixed checkstyle issues
ivan0dyatlyukkk 0b203a5
added an option for easy testing
ivan0dyatlyukkk 855d029
Merge branch 'main' into feat-payment
ivan0dyatlyukkk 0a8b36c
adjusted test properties file
ivan0dyatlyukkk 95b7be5
Merge remote-tracking branch 'origin/feat-payment' into feat-payment
ivan0dyatlyukkk 422d6d0
Update application.properties
ivan0dyatlyukkk 425ad4e
Updated PaymentAmountHandlerStrategy.java
ivan0dyatlyukkk 1506abd
Updated PaymentService.java
ivan0dyatlyukkk 6d1bcba
Merge branch 'main' into feat-payment
ivan0dyatlyukkk 5731675
fixed small mistakes
ivan0dyatlyukkk 81328bc
Merge remote-tracking branch 'origin/feat-payment' into feat-payment
ivan0dyatlyukkk 3aa6990
resolved all of conflicts and updated the branch for implementing pay…
ivan0dyatlyukkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of using empty class?