-
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.
- Loading branch information
1 parent
3458ccf
commit 3d0d029
Showing
19 changed files
with
652 additions
and
272 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
82 changes: 82 additions & 0 deletions
82
backend/app/src/main/java/eu/viandeendirect/domains/payment/StripeDirectPaymentManager.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,82 @@ | ||
package eu.viandeendirect.domains.payment; | ||
|
||
import com.stripe.exception.StripeException; | ||
import com.stripe.model.checkout.Session; | ||
import com.stripe.net.RequestOptions; | ||
import com.stripe.param.checkout.SessionCreateParams; | ||
import eu.viandeendirect.domains.production.PackageLotRepository; | ||
import eu.viandeendirect.model.Order; | ||
import eu.viandeendirect.model.OrderItem; | ||
import eu.viandeendirect.model.Producer; | ||
import eu.viandeendirect.model.StripePayment; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.TemporalUnit; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Manager Stripe payments following the Stripe pattern "direct charge". | ||
* To use when orders includes productions from the same producer. | ||
* | ||
* @see <a href="https://docs.stripe.com/connect/direct-charges">Stripe doc</a> | ||
*/ | ||
@Service | ||
@Qualifier("StripeDirectPaymentManager") | ||
public class StripeDirectPaymentManager implements StripePaymentManager { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(StripeDirectPaymentManager.class); | ||
|
||
@Value("${PRODUCER_FRONTEND_URL:http://localhost:3000}") | ||
String viandeendirectProducerFrontendUrl; | ||
|
||
@Autowired | ||
PackageLotRepository packageLotRepository; | ||
|
||
@Override | ||
public StripePayment createPayment(Order order) throws StripeException { | ||
SessionCreateParams.Builder builder = SessionCreateParams.builder(); | ||
order.getItems().forEach(item -> builder.addLineItem(getLineItem(item))); | ||
SessionCreateParams params = builder | ||
.setPaymentIntentData(SessionCreateParams.PaymentIntentData.builder() | ||
.setDescription(String.format("Commande viandeendirect.eu n° %s de %s %s", order.getId(), order.getCustomer().getUser().getFirstName(), order.getCustomer().getUser().getLastName())) | ||
.setApplicationFeeAmount(1L).build()) | ||
.setMode(SessionCreateParams.Mode.PAYMENT) | ||
.setCustomerEmail(order.getCustomer().getUser().getEmail()) | ||
.setSuccessUrl(viandeendirectProducerFrontendUrl + "/order/" + order.getId() + "/paymentSuccessful") | ||
.setCancelUrl(viandeendirectProducerFrontendUrl + "/order/" + order.getId() + "/paymentCancelled") | ||
.setExpiresAt(Instant.now().plusSeconds(30 * 60).getEpochSecond()) | ||
.build(); | ||
RequestOptions requestOptions = RequestOptions.builder().setStripeAccount(getProducerStripeAccount(order).getStripeAccount().getStripeId()).build(); | ||
Session session = Session.create(params, requestOptions); | ||
StripePayment stripePayment = new StripePayment(); | ||
stripePayment.setCheckoutSessionId(session.getId()); | ||
stripePayment.setPaymentUrl(session.getUrl()); | ||
return stripePayment; | ||
} | ||
|
||
private Producer getProducerStripeAccount(Order order) { | ||
List<Producer> orderProducers = order.getItems().stream() | ||
.map(OrderItem::getPackageLot) | ||
.map(lot -> packageLotRepository.findById(lot.getId()).get()) | ||
.map(lot -> lot.getProduction().getProducer()) | ||
.distinct() | ||
.toList(); | ||
if (orderProducers.size() > 1) { | ||
throw new IllegalStateException("StripeDirectPaymentManager can only be used with orders from the same producer"); | ||
} | ||
return orderProducers.stream().findFirst().get(); | ||
} | ||
|
||
@Override | ||
public void processPaymentValidation(Order order) { | ||
LOGGER.debug("processing payment validation for order {} : do nothing while payment manager is {}", order.getId(), this.getClass().getSimpleName()); | ||
} | ||
} |
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
Oops, something went wrong.