Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 9fb2fd6

Browse files
authored
Merge pull request #97 from Zenfulcode/patch-capture-by-orderid
refactoring capturing method
2 parents 07a31ac + aa05167 commit 9fb2fd6

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/main/java/com/zenfulcode/commercify/commercify/controller/PaymentController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public ResponseEntity<PaymentStatus> getPaymentStatus(@PathVariable Long orderId
3838
}
3939

4040
@PreAuthorize("hasRole('ADMIN')")
41-
@PostMapping("/{paymentId}/capture")
42-
public ResponseEntity<String> capturePayment(@PathVariable Long paymentId, @RequestBody CapturePaymentRequest request) {
41+
@PostMapping("/{orderId}/capture")
42+
public ResponseEntity<String> capturePayment(@PathVariable Long orderId, @RequestBody CapturePaymentRequest request) {
4343
try {
44-
mobilePayService.capturePayment(paymentId, request.captureAmount(), request.isPartialCapture());
44+
mobilePayService.capturePayment(orderId, request.captureAmount(), request.isPartialCapture());
4545
return ResponseEntity.ok("Payment captured successfully");
4646
} catch (Exception e) {
4747
log.error("Error capturing payment", e);

src/main/java/com/zenfulcode/commercify/commercify/integration/mobilepay/MobilePayService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.zenfulcode.commercify.commercify.PaymentStatus;
55
import com.zenfulcode.commercify.commercify.api.requests.PaymentRequest;
66
import com.zenfulcode.commercify.commercify.api.requests.WebhookPayload;
7-
import com.zenfulcode.commercify.commercify.api.requests.products.PriceRequest;
87
import com.zenfulcode.commercify.commercify.api.responses.PaymentResponse;
98
import com.zenfulcode.commercify.commercify.dto.OrderDTO;
109
import com.zenfulcode.commercify.commercify.dto.OrderDetailsDTO;
@@ -78,9 +77,9 @@ public MobilePayService(PaymentRepository paymentRepository, EmailService emailS
7877
}
7978

8079
@Override
81-
public void capturePayment(Long paymentId, double captureAmount, boolean isPartialCapture) {
82-
PaymentEntity payment = paymentRepository.findById(paymentId)
83-
.orElseThrow(() -> new RuntimeException("Payment not found: " + paymentId));
80+
public void capturePayment(Long orderId, double captureAmount, boolean isPartialCapture) {
81+
PaymentEntity payment = paymentRepository.findByOrderId(orderId)
82+
.orElseThrow(() -> new RuntimeException("Payment not found for orderId: " + orderId));
8483

8584
if (payment.getStatus() != PaymentStatus.PAID) {
8685
throw new RuntimeException("Payment cannot captured");
@@ -89,12 +88,13 @@ public void capturePayment(Long paymentId, double captureAmount, boolean isParti
8988
OrderDetailsDTO order = orderService.getOrderById(payment.getOrderId());
9089

9190
double capturingAmount = isPartialCapture ? captureAmount : payment.getTotalAmount();
91+
capturingAmount *= 100; // Convert to minor units
9292

93-
PriceRequest priceRequest = new PriceRequest(order.getOrder().getCurrency(), capturingAmount);
93+
log.info("Capturing payment: orderId={}, amount={} formatted={}", orderId, capturingAmount, Math.round(capturingAmount));
9494

9595
// Capture payment
9696
if (payment.getMobilePayReference() != null) {
97-
capturePayment(payment.getMobilePayReference(), priceRequest);
97+
capturePayment(payment.getMobilePayReference(), Math.round(capturingAmount), order.getOrder().getCurrency());
9898
}
9999

100100
// Update payment status
@@ -409,14 +409,14 @@ protected CompletableFuture<String> getWebhookSecret() {
409409
}
410410
}
411411

412-
public void capturePayment(String mobilePayReference, PriceRequest captureAmount) {
412+
public void capturePayment(String mobilePayReference, long captureAmount, String currency) {
413413
paymentRepository.findByMobilePayReference(mobilePayReference)
414414
.orElseThrow(() -> new PaymentProcessingException("Payment not found", null));
415415

416416
HttpHeaders headers = mobilePayRequestHeaders();
417417

418418
Map<String, Object> request = new HashMap<>();
419-
request.put("modificationAmount", new MobilePayPrice(Math.round(captureAmount.amount() * 100), captureAmount.currency()));
419+
request.put("modificationAmount", new MobilePayPrice(captureAmount, currency));
420420

421421
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);
422422

0 commit comments

Comments
 (0)