Pull Request #2204
Add idempotent payment retry support
Introduces IdempotencyRecord to cache payment results keyed by a client-supplied token. Retried requests with the same key skip the gateway and return the cached result. Includes a scheduled cleanup job for expired records.
Urgent
service/PaymentService.java+17 additions
1
+ @Service
2
+ public class PaymentService {
3
+ @Autowired private IdempotencyRepository idempotencyRepo;
4
+ @Autowired private PaymentGateway gateway;
5
+
6
+ public PaymentResult processPayment(PaymentRequest req, String idempotencyKey) {
7
+ // Return cached result if this request was already processed.
8
+ Optional<IdempotencyRecord> existing = idempotencyRepo.findByKey(idempotencyKey);
9
+ if (existing.isPresent()) {
10
+ return existing.get().getCachedResult();
11
+ }
12
+ PaymentResult result = gateway.charge(req);
13
+ IdempotencyRecord record = new IdempotencyRecord(idempotencyKey, result);
14
+ idempotencyRepo.save(record);
15
+ return result;
16
+ }
17
+ }
Review comments · service/PaymentService.java