Points now subtract from costs

This commit is contained in:
augmentedpotato
2026-04-15 02:44:14 -06:00
parent 404f162728
commit 914e0bceda
6 changed files with 192 additions and 7 deletions

View File

@@ -75,6 +75,16 @@ public class CartController {
return ResponseEntity.ok(cartService.applyCoupon(userId, storeId, request.getCouponCode()));
}
@PostMapping("/apply-points")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<CartResponse> applyPoints(
@RequestParam Long storeId,
@RequestParam Boolean useLoyaltyPoints) {
Long userId = AuthenticationHelper.getAuthenticatedUserId();
return ResponseEntity.ok(cartService.applyPoints(userId, storeId, useLoyaltyPoints));
}
@DeleteMapping("/coupon")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<CartResponse> removeCoupon(@RequestParam Long storeId) {

View File

@@ -257,6 +257,34 @@ public class CartService {
.setScale(0, RoundingMode.HALF_UP)
.longValue();
// Free checkout: total is $0.00, or points are applied and the remaining
// amount is below Stripe's $0.50 minimum (cannot be charged via card)
if (amountInCents == 0 || (amountInCents < 50 && Boolean.TRUE.equals(cart.getPointsApplied()))) {
SaleRequest saleRequest = new SaleRequest();
saleRequest.setStoreId(cart.getStore().getStoreId());
saleRequest.setCustomerId(cart.getUser().getId());
saleRequest.setCartId(cart.getCartId());
saleRequest.setCouponId(cart.getCoupon() != null ? cart.getCoupon().getCouponId() : null);
saleRequest.setPaymentMethod("Points");
saleRequest.setChannel("WEBSITE");
saleRequest.setItems(items.stream()
.map(item -> {
SaleItemRequest sir = new SaleItemRequest();
sir.setProdId(item.getProduct().getProdId());
sir.setQuantity(item.getQuantity());
return sir;
})
.toList());
saleService.createSale(saleRequest);
cart.setCartStatus("CHECKED_OUT");
cart.setCheckoutPending(false);
cartRepository.save(cart);
return new CheckoutResponse(cart.getCartId(), null, BigDecimal.ZERO, "succeeded");
}
if (amountInCents < 50) {
throw new BusinessException("Order total is too low to process payment");
}
@@ -489,10 +517,12 @@ public class CartService {
return BigDecimal.ZERO;
}
BigDecimal maxRedeemable = remainingAmount.setScale(0, RoundingMode.DOWN);
return BigDecimal.valueOf(wholeDollars)
.min(maxRedeemable)
.setScale(2, RoundingMode.HALF_UP);
BigDecimal maxDiscount = BigDecimal.valueOf(wholeDollars);
// If points can fully cover the remaining amount, discount the entire total to $0.00
if (maxDiscount.compareTo(remainingAmount) >= 0) {
return remainingAmount.setScale(2, RoundingMode.HALF_UP);
}
return maxDiscount.setScale(2, RoundingMode.HALF_UP);
}
private CartResponse toResponse(Cart cart) {