fix stripe payment flow

This commit is contained in:
2026-04-09 22:52:57 -06:00
parent 1010d57b79
commit 39e4a3896e
8 changed files with 66 additions and 40 deletions

View File

@@ -80,5 +80,15 @@ public class CartController {
public ResponseEntity<CheckoutResponse> checkout(@Valid @RequestBody CheckoutRequest request) {
Long userId = AuthenticationHelper.getAuthenticatedUserId();
return ResponseEntity.ok(cartService.checkout(userId, request.getStoreId(), request.getPaymentMethodId()));}
return ResponseEntity.ok(cartService.checkout(userId, request.getStoreId()));
}
@PostMapping("/checkout/complete")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<Void> completeCheckout(@RequestParam String paymentIntentId) {
Long userId = AuthenticationHelper.getAuthenticatedUserId();
cartService.completeCheckout(userId, paymentIntentId);
return ResponseEntity.noContent().build();
}
}

View File

@@ -1,6 +1,5 @@
package com.petshop.backend.dto.cart;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class CheckoutRequest {
@@ -8,17 +7,6 @@ public class CheckoutRequest {
@NotNull
private Long storeId;
@NotBlank
private String paymentMethodId;
public Long getStoreId() { return storeId; }
public void setStoreId(Long storeId) { this.storeId = storeId; }
public String getPaymentMethodId() {
return paymentMethodId;
}
public void setPaymentMethodId(String paymentMethodId) {
this.paymentMethodId = paymentMethodId;
}
}

View File

@@ -190,7 +190,7 @@ public class CartService {
}
@Transactional
public CheckoutResponse checkout(Long userId, Long storeId, String paymentMethodId) {
public CheckoutResponse checkout(Long userId, Long storeId) {
Cart cart = cartRepository
.findActiveCartByUserAndStore(userId, storeId, "ACTIVE")
.orElseThrow(() -> new BusinessException("No active cart found"));
@@ -213,21 +213,12 @@ public class CartService {
PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
.setAmount(amountInCents)
.setCurrency("usd")
.setPaymentMethod(paymentMethodId)
.setConfirm(true)
.setReturnUrl("http://localhost:3000/cart/confirmation")
.putMetadata("cartId", String.valueOf(cart.getCartId()))
.putMetadata("userId", String.valueOf(userId))
.build();
PaymentIntent intent = PaymentIntent.create(params);
if ("succeeded".equals(intent.getStatus())
|| "requires_action".equals(intent.getStatus())) {
cart.setCartStatus("CHECKED_OUT");
cartRepository.save(cart);
}
return new CheckoutResponse(
cart.getCartId(),
intent.getClientSecret(),
@@ -235,13 +226,38 @@ public class CartService {
intent.getStatus()
);
}
}
catch (StripeException e) {
throw new BusinessException("Payment processing failed: " + e.getMessage());
}
}
@Transactional
public void completeCheckout(Long userId, String paymentIntentId) {
try {
PaymentIntent intent = PaymentIntent.retrieve(paymentIntentId);
if (!"succeeded".equals(intent.getStatus())) {
throw new BusinessException("Payment has not been completed");
}
Long cartId = Long.parseLong(intent.getMetadata().get("cartId"));
Cart cart = cartRepository.findById(cartId)
.orElseThrow(() -> new BusinessException("Cart not found"));
if (!cart.getUser().getUserId().equals(userId)) {
throw new BusinessException("Unauthorized");
}
cart.setCartStatus("CHECKED_OUT");
cartRepository.save(cart);
} catch (StripeException e) {
throw new BusinessException("Payment verification failed: " + e.getMessage());
}
}
private void recalculate(Cart cart) {
List<CartItem> items = cartItemRepository.findByCartCartId(cart.getCartId());