Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public ResponseEntity<PaymentStatus> getPaymentStatus(@PathVariable Long orderId
}

@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/{paymentId}/capture")
public ResponseEntity<String> capturePayment(@PathVariable Long paymentId, @RequestBody CapturePaymentRequest request) {
@PostMapping("/{orderId}/capture")
public ResponseEntity<String> capturePayment(@PathVariable Long orderId, @RequestBody CapturePaymentRequest request) {
try {
mobilePayService.capturePayment(paymentId, request.captureAmount(), request.isPartialCapture());
mobilePayService.capturePayment(orderId, request.captureAmount(), request.isPartialCapture());
return ResponseEntity.ok("Payment captured successfully");
} catch (Exception e) {
log.error("Error capturing payment", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.zenfulcode.commercify.commercify.PaymentStatus;
import com.zenfulcode.commercify.commercify.api.requests.PaymentRequest;
import com.zenfulcode.commercify.commercify.api.requests.WebhookPayload;
import com.zenfulcode.commercify.commercify.api.requests.products.PriceRequest;
import com.zenfulcode.commercify.commercify.api.responses.PaymentResponse;
import com.zenfulcode.commercify.commercify.dto.OrderDTO;
import com.zenfulcode.commercify.commercify.dto.OrderDetailsDTO;
Expand Down Expand Up @@ -78,9 +77,9 @@ public MobilePayService(PaymentRepository paymentRepository, EmailService emailS
}

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

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

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

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

// Capture payment
if (payment.getMobilePayReference() != null) {
capturePayment(payment.getMobilePayReference(), priceRequest);
capturePayment(payment.getMobilePayReference(), Math.round(capturingAmount), order.getOrder().getCurrency());
}

// Update payment status
Expand Down Expand Up @@ -409,14 +409,14 @@ protected CompletableFuture<String> getWebhookSecret() {
}
}

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

HttpHeaders headers = mobilePayRequestHeaders();

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

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

Expand Down
Loading