44import com .zenfulcode .commercify .commercify .PaymentStatus ;
55import com .zenfulcode .commercify .commercify .api .requests .PaymentRequest ;
66import com .zenfulcode .commercify .commercify .api .requests .WebhookPayload ;
7- import com .zenfulcode .commercify .commercify .api .requests .products .PriceRequest ;
87import com .zenfulcode .commercify .commercify .api .responses .PaymentResponse ;
98import com .zenfulcode .commercify .commercify .dto .OrderDTO ;
109import 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