From fe950d08f03fb9b155cce02c287e538da97a2727 Mon Sep 17 00:00:00 2001
From: GustavH
Date: Mon, 20 Jan 2025 12:04:18 +0100
Subject: [PATCH 1/2] adding mobilepay capture (not tested)
---
.../api/requests/CapturePaymentRequest.java | 4 +++
.../controller/PaymentController.java | 12 +++++++
.../mobilepay/MobilePayService.java | 33 +++++++++++++++++++
.../commercify/service/PaymentService.java | 23 +++++++++++++
4 files changed, 72 insertions(+)
create mode 100644 src/main/java/com/zenfulcode/commercify/commercify/api/requests/CapturePaymentRequest.java
diff --git a/src/main/java/com/zenfulcode/commercify/commercify/api/requests/CapturePaymentRequest.java b/src/main/java/com/zenfulcode/commercify/commercify/api/requests/CapturePaymentRequest.java
new file mode 100644
index 0000000..8ea9d3f
--- /dev/null
+++ b/src/main/java/com/zenfulcode/commercify/commercify/api/requests/CapturePaymentRequest.java
@@ -0,0 +1,4 @@
+package com.zenfulcode.commercify.commercify.api.requests;
+
+public record CapturePaymentRequest(double captureAmount, boolean isPartialCapture) {
+}
diff --git a/src/main/java/com/zenfulcode/commercify/commercify/controller/PaymentController.java b/src/main/java/com/zenfulcode/commercify/commercify/controller/PaymentController.java
index 3fb7f51..155c36a 100644
--- a/src/main/java/com/zenfulcode/commercify/commercify/controller/PaymentController.java
+++ b/src/main/java/com/zenfulcode/commercify/commercify/controller/PaymentController.java
@@ -1,6 +1,7 @@
package com.zenfulcode.commercify.commercify.controller;
import com.zenfulcode.commercify.commercify.PaymentStatus;
+import com.zenfulcode.commercify.commercify.api.requests.CapturePaymentRequest;
import com.zenfulcode.commercify.commercify.service.PaymentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -35,4 +36,15 @@ public ResponseEntity getPaymentStatus(@PathVariable Long orderId
PaymentStatus status = paymentService.getPaymentStatus(orderId);
return ResponseEntity.ok(status);
}
+
+ @PostMapping("/{paymentId}/capture")
+ public ResponseEntity capturePayment(@PathVariable Long paymentId, @RequestBody CapturePaymentRequest request) {
+ try {
+ paymentService.capturePayment(paymentId, request.captureAmount(), request.isPartialCapture());
+ return ResponseEntity.ok("Payment captured successfully");
+ } catch (Exception e) {
+ log.error("Error capturing payment", e);
+ return ResponseEntity.badRequest().body("Error capturing payment");
+ }
+ }
}
\ No newline at end of file
diff --git a/src/main/java/com/zenfulcode/commercify/commercify/integration/mobilepay/MobilePayService.java b/src/main/java/com/zenfulcode/commercify/commercify/integration/mobilepay/MobilePayService.java
index aafa1d2..858161c 100644
--- a/src/main/java/com/zenfulcode/commercify/commercify/integration/mobilepay/MobilePayService.java
+++ b/src/main/java/com/zenfulcode/commercify/commercify/integration/mobilepay/MobilePayService.java
@@ -4,6 +4,7 @@
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.entity.OrderEntity;
import com.zenfulcode.commercify.commercify.entity.PaymentEntity;
@@ -348,6 +349,38 @@ private String getWebhookSecret() {
.map(WebhookConfigEntity::getWebhookSecret)
.orElseThrow(() -> new PaymentProcessingException("Webhook secret not found", null));
}
+
+ public void capturePayment(String mobilePayReference, PriceRequest captureAmount) {
+ PaymentEntity payment = paymentRepository.findByMobilePayReference(mobilePayReference)
+ .orElseThrow(() -> new PaymentProcessingException("Payment not found", null));
+
+ HttpHeaders headers = mobilePayRequestHeaders();
+
+ Map request = new HashMap<>();
+ request.put("modificationAmount", new MobilePayPrice(Math.round(captureAmount.amount() * 100), captureAmount.currency()));
+
+ HttpEntity
View Order in Dashboard
Please process this order as soon as possible.
-
-Best regards,
Commercify System