To verify a Paystack payment in Spring Boot, use your PaystackService to GET the verify endpoint. Check status, amount, and currency against your JPA Order entity. Use a JPQL UPDATE with a WHERE clause on paid=false for idempotent fulfillment.
@Transactional
public boolean verifyAndFulfill(String reference) {
Order order = orderRepository.findByReference(reference);
if (order == null) return false;
if (order.isPaid()) return true;
Map<String, Object> result = verifyTransaction(reference);
if (!Boolean.TRUE.equals(result.get("status"))) return false;
Map<String, Object> data = (Map<String, Object>) result.get("data");
if (!"success".equals(data.get("status"))) return false;
int paystackAmount = ((Number) data.get("amount")).intValue();
if (paystackAmount != order.getAmountKobo()) return false;
// Atomic update
int updated = orderRepository.markAsPaid(reference);
return updated > 0;
}
// In OrderRepository:
@Modifying
@Query("UPDATE Order o SET o.paid = true, o.paidAt = CURRENT_TIMESTAMP WHERE o.reference = :ref AND o.paid = false")
int markAsPaid(@Param("ref") String reference);
@GetMapping("/verify")
public ResponseEntity<?> verify(@RequestParam String reference) {
boolean success = paystackService.verifyAndFulfill(reference);
if (success) {
return ResponseEntity.ok(Map.of("success", true));
}
return ResponseEntity.badRequest().body(Map.of("success", false));
}
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course