diff --git a/platform/build.gradle b/platform/build.gradle index 902994aa..4e587284 100644 --- a/platform/build.gradle +++ b/platform/build.gradle @@ -27,7 +27,8 @@ dependencies { implementation group: 'io.github.boostchicken', name: 'spring-data-dynamodb', version: '5.2.5' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-graphql' - testImplementation 'junit:junit:4.13.2' + implementation group: 'com.stripe', name: 'stripe-java', version: '24.0.0' + testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/platform/src/main/java/com/flickmatch/platform/dynamodb/model/SportsVenues.java b/platform/src/main/java/com/flickmatch/platform/dynamodb/model/SportsVenues.java index f7e0a875..22abd93c 100644 --- a/platform/src/main/java/com/flickmatch/platform/dynamodb/model/SportsVenues.java +++ b/platform/src/main/java/com/flickmatch/platform/dynamodb/model/SportsVenues.java @@ -96,6 +96,7 @@ public void setAvailableSportsIds(List availableSportsIds) { @Builder @NoArgsConstructor @AllArgsConstructor + public static class StripePaymentLink { Double amount; diff --git a/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportRepository.java b/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportRepository.java index dc33576f..0ccc32e3 100644 --- a/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportRepository.java +++ b/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportRepository.java @@ -9,5 +9,4 @@ public interface SportRepository extends CrudRepository { Optional findById(String sportId); - } diff --git a/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportsVenueRepository.java b/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportsVenueRepository.java index 5ae64ed5..4416200a 100644 --- a/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportsVenueRepository.java +++ b/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/SportsVenueRepository.java @@ -4,10 +4,8 @@ import org.socialsignin.spring.data.dynamodb.repository.EnableScan; import org.springframework.data.repository.CrudRepository; -import java.util.List; -import java.util.Optional; @EnableScan public interface SportsVenueRepository extends CrudRepository { - + SportsVenues findByCityIdAndSportsVenueId(String cityId, String sportsVenueId); } diff --git a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java index fbb68401..04e2530c 100644 --- a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java +++ b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java @@ -1,21 +1,26 @@ package com.flickmatch.platform.graphql.builder; +import com.flickmatch.platform.dynamodb.model.City; import com.flickmatch.platform.dynamodb.model.Event; +import com.flickmatch.platform.dynamodb.repository.CityRepository; import com.flickmatch.platform.dynamodb.repository.EventRepository; +import com.flickmatch.platform.dynamodb.repository.SportsVenueRepository; import com.flickmatch.platform.graphql.input.CreateEventInput; import com.flickmatch.platform.graphql.input.JoinEventInput; import com.flickmatch.platform.graphql.type.Player; import com.flickmatch.platform.graphql.type.SportsVenue; import com.flickmatch.platform.graphql.type.StripePaymentLink; import com.flickmatch.platform.graphql.util.DateUtil; +import com.stripe.Stripe; +import com.stripe.exception.StripeException; +import com.stripe.model.PaymentLink; +import com.stripe.param.PaymentLinkCreateParams; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; - +import com.flickmatch.platform.dynamodb.model.SportsVenues; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.time.LocalDate; -import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -28,6 +33,10 @@ public class EventBuilder { EventRepository eventRepository; + SportsVenueRepository sportsVenueRepository; + + CityRepository cityRepository; + @Autowired SportsVenueBuilder sportsVenueBuilder; public EventBuilder(EventRepository eventRepository) { @@ -184,11 +193,50 @@ private String getPaymentUrlForEvent(SportsVenue sportsVenue, Double amount) { if (stripePaymentLinkForAmount.isEmpty()) { //Todo: For now keeping it soft dependency, we need to throw exception in future log.error("Can't find stripe payment link in venue"); - return null; + return createPaymentLink(amount); } return stripePaymentLinkForAmount.get().getUrl(); } + private String createPaymentLink(Double amount) { + Stripe.apiKey = "sk_test_tR3PYbcVNZZ796tH88S4VQ2u";; + try { + PaymentLinkCreateParams params = + PaymentLinkCreateParams.builder() + .addLineItem( + PaymentLinkCreateParams.LineItem.builder() + .setPrice(String.valueOf(amount)) + .setQuantity(1L) + .build() + ) + .build(); + PaymentLink paymentLink = PaymentLink.create(params); + String paymentLinkUrl = paymentLink.getUrl(); + SportsVenues sportsVenues = new SportsVenues(); + String cityId = String.valueOf(Long.valueOf(sportsVenues.getCityId())); + + SportsVenues.SportsVenue sportsVenue1 = new SportsVenues.SportsVenue(); + String sportsVenueId = String.valueOf(Long.valueOf(sportsVenue1.getSportsVenueId())); + + SportsVenues sportsVenue = sportsVenueRepository.findByCityIdAndSportsVenueId(cityId, sportsVenueId); + if (sportsVenue != null) { + StripePaymentLink stripePaymentLink = StripePaymentLink.builder() + .amount(amount) + .url(paymentLinkUrl) + .build(); + + sportsVenue1.setStripePaymentLinks((List) stripePaymentLink); + List sportsVenuesList = new ArrayList<>(); + sportsVenuesList.add((SportsVenues) sportsVenue1.getStripePaymentLinks()); + sportsVenueRepository.saveAll(sportsVenuesList); + } + + return paymentLinkUrl; + } catch (StripeException e) { + log.error("Error creating Stripe payment link: " + e.getMessage()); + return null; + } + } private com.flickmatch.platform.graphql.type.Event mapEventToGQLType(Event.EventDetails eventDetails, String date, String localTimeZone) { String eventId = date + "-" + eventDetails.getIndex(); int players = eventDetails.getReservedPlayersCount() / 2; diff --git a/platform/src/main/java/com/flickmatch/platform/graphql/type/StripePaymentLink.java b/platform/src/main/java/com/flickmatch/platform/graphql/type/StripePaymentLink.java index 41d5ef90..592acf4d 100644 --- a/platform/src/main/java/com/flickmatch/platform/graphql/type/StripePaymentLink.java +++ b/platform/src/main/java/com/flickmatch/platform/graphql/type/StripePaymentLink.java @@ -1,11 +1,15 @@ package com.flickmatch.platform.graphql.type; +import com.flickmatch.platform.dynamodb.model.SportsVenues; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; @Builder @Getter -public class StripePaymentLink { +@AllArgsConstructor +public class StripePaymentLink{ Double amount; String url; }