Skip to content

Commit

Permalink
Fix model/request bindings and js
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwok-he-Chu committed Jan 5, 2024
1 parent 5fa126d commit 1fc8bad
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class ApiController {
private final PaymentsApi paymentsApi;

public ApiController(ApplicationProperty applicationProperty) {

this.applicationProperty = applicationProperty;

if(applicationProperty.getApiKey() == null) {
Expand Down Expand Up @@ -111,14 +110,12 @@ public ResponseEntity<PaymentResponse> payments(@RequestHeader String host, @Req
var response = paymentsApi.payments(paymentRequest);

if (response.getResultCode() == PaymentResponse.ResultCodeEnum.AUTHORISED) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

var payment = new PaymentModel(response.getMerchantReference(),
response.getPspReference(),
response.getAmount().getValue(),
response.getAmount().getCurrency(),
LocalDateTime.now().format(formatter),
LocalDateTime.now().plusDays(28).format(formatter),
LocalDateTime.now(),
LocalDateTime.now().plusDays(28),
response.getPaymentMethod().getBrand(),
new ArrayList<>()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.adyen.checkout.model;

import java.time.LocalDateTime;

public class PaymentDetailsModel {
private String merchantReference;
private String pspReference;
private String originalReference;
private long amount;
private String currency;
private String dateTime;
private LocalDateTime dateTime;
private String eventCode;
private String refusalReason;
private String paymentMethodBrand;
private boolean success;

public PaymentDetailsModel(String merchantReference, String pspReference, String originalReference, long amount, String currency, String dateTime, String eventCode, String refusalReason, String paymentMethodBrand, boolean success) {
public PaymentDetailsModel(String merchantReference, String pspReference, String originalReference, long amount, String currency, LocalDateTime dateTime, String eventCode, String refusalReason, String paymentMethodBrand, boolean success) {
this.merchantReference = merchantReference;
this.pspReference = pspReference;
this.originalReference = originalReference;
Expand Down Expand Up @@ -65,11 +67,11 @@ public void setCurrency(String currency) {
this.currency = currency;
}

public String getDateTime() {
public LocalDateTime getDateTime() {
return dateTime;
}

public void setDateTime(String dateTime) {
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class PaymentModel {
private String pspReference;
private long amount;
private String currency;
private String bookingDate;
private String expiryDate;
private LocalDateTime bookingDate;
private LocalDateTime expiryDate;
private String paymentMethodBrand;
private List<PaymentDetailsModel> paymentDetailsModelList;

public PaymentModel(String merchantReference, String pspReference, long amount, String currency, String bookingDate, String expiryDate, String paymentMethodBrand, List<PaymentDetailsModel> paymentDetailsModelList) {
public PaymentModel(String merchantReference, String pspReference, long amount, String currency, LocalDateTime bookingDate, LocalDateTime expiryDate, String paymentMethodBrand, List<PaymentDetailsModel> paymentDetailsModelList) {
this.merchantReference = merchantReference;
this.pspReference = pspReference;
this.amount = amount;
Expand All @@ -28,8 +28,7 @@ public PaymentModel(String merchantReference, String pspReference, long amount,
}

public long getDaysUntilExpiry() {
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // For visual-purposes in this demo, we ignore the values after the time and show the date time
return ChronoUnit.DAYS.between(LocalDateTime.parse(bookingDate, formatter), LocalDateTime.parse(expiryDate, formatter));
return ChronoUnit.DAYS.between(bookingDate, expiryDate);
}

public String getMerchantReference() {
Expand Down Expand Up @@ -64,19 +63,19 @@ public void setCurrency(String currency) {
this.currency = currency;
}

public String getBookingDate() {
public LocalDateTime getBookingDate() {
return bookingDate;
}

public void setBookingDate(String bookingDate) {
public void setBookingDate(LocalDateTime bookingDate) {
this.bookingDate = bookingDate;
}

public String getExpiryDate() {
public LocalDateTime getExpiryDate() {
return expiryDate;
}

public void setExpiryDate(String expiryDate) {
public void setExpiryDate(LocalDateTime expiryDate) {
this.expiryDate = expiryDate;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.adyen.checkout.request;

public class CapturePaymentRequest {
public String reference;
public long amount;

public long getAmount() {
return amount;
}

public void setAmount(long amount) {
this.amount = amount;
}

public String getReference() {
return reference;
}

public void setReference(String reference) {
this.reference = reference;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.adyen.checkout.request;

public class ReversalPaymentRequest {
public String reference;

public String getReference() {
return reference;
}

public void setReference(String reference) {
this.reference = reference;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.adyen.checkout.request;

public class UpdatePaymentAmountRequest {
public String reference;
public long amount;

public long getAmount() {
return amount;
}

public void setAmount(long amount) {
this.amount = amount;
}

public String getReference() {
return reference;
}

public void setReference(String reference) {
this.reference = reference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.adyen.checkout.model.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -40,7 +41,7 @@ public static void addPaymentToHistory(PaymentDetailsModel paymentDetailsModel)
}
}

public static void updatePayment(String merchantReference, long amount, String expiryDate) {
public static void updatePayment(String merchantReference, long amount, LocalDateTime expiryDate) {
PaymentModel payment = findByMerchantReference(merchantReference);

if (payment != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.adyen.Client;
import com.adyen.checkout.ApplicationProperty;
import com.adyen.checkout.model.PaymentModel;
import com.adyen.checkout.request.CapturePaymentRequest;
import com.adyen.checkout.request.ReversalPaymentRequest;
import com.adyen.checkout.request.UpdatePaymentAmountRequest;
import com.adyen.checkout.util.Storage;
import com.adyen.enums.Environment;
import com.adyen.model.checkout.*;
Expand Down Expand Up @@ -73,12 +76,12 @@ public String details(@PathVariable String reference, Model model) {
}

@PostMapping("/admin/capture-payment")
public ResponseEntity<PaymentCaptureResponse> capturePayment(@RequestBody String reference) {
public ResponseEntity<PaymentCaptureResponse> capturePayment(@RequestBody CapturePaymentRequest request) {
try {
PaymentModel payment = Storage.findByMerchantReference(reference);
PaymentModel payment = Storage.findByMerchantReference(request.getReference());

if (payment == null) {
throw new Exception("Payment not found in storage - Reference: " + reference);
throw new Exception("Payment not found in storage - Reference: " + request.getReference());
}

var paymentCaptureRequest = new PaymentCaptureRequest();
Expand All @@ -102,23 +105,23 @@ public ResponseEntity<PaymentCaptureResponse> capturePayment(@RequestBody String
}

@PostMapping("/admin/update-payment-amount")
public ResponseEntity<PaymentAmountUpdateResponse> updatePaymentAmount(@RequestBody String reference) {
public ResponseEntity<PaymentAmountUpdateResponse> updatePaymentAmount(@RequestBody UpdatePaymentAmountRequest request) {
try {
PaymentModel payment = Storage.findByMerchantReference(reference);
PaymentModel payment = Storage.findByMerchantReference(request.getReference());

if (payment == null) {
throw new Exception("Payment not found in storage - Reference: " + reference);
throw new Exception("Payment not found in storage - Reference: " + request.getReference());
}

var paymentAmountUpdateRequest = new PaymentAmountUpdateRequest();
paymentAmountUpdateRequest.setMerchantAccount(applicationProperty.getMerchantAccount());
paymentAmountUpdateRequest.setReference(payment.getMerchantReference());
paymentAmountUpdateRequest.setIndustryUsage(PaymentAmountUpdateRequest.IndustryUsageEnum.DELAYEDCHARGE);

var amount = new Amount();
amount.setValue(payment.getAmount());
amount.setCurrency(payment.getCurrency());
paymentAmountUpdateRequest.setAmount(amount);
var a = new Amount();
a.setValue(request.getAmount());
a.setCurrency(payment.getCurrency());
paymentAmountUpdateRequest.setAmount(a);

var response = modificationsApi.updateAuthorisedAmount(payment.getPspReference(), paymentAmountUpdateRequest);

Expand All @@ -132,12 +135,12 @@ public ResponseEntity<PaymentAmountUpdateResponse> updatePaymentAmount(@RequestB
}

@PostMapping("/admin/reversal-payment")
public ResponseEntity<PaymentReversalResponse> reversalPayment(@RequestBody String reference) {
public ResponseEntity<PaymentReversalResponse> reversalPayment(@RequestBody ReversalPaymentRequest request) {
try {
PaymentModel payment = Storage.findByMerchantReference(reference);
PaymentModel payment = Storage.findByMerchantReference(request.getReference());

if (payment == null) {
throw new Exception("Payment not found in storage - Reference: " + reference);
throw new Exception("Payment not found in storage - Reference: " + request.getReference());
}

var paymentReversalRequest = new PaymentReversalRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function sendCapturePaymentRequest(reference) {
window.location.href = "admin/result/received/" + reference;
break;
default:
window.location.href = "admin/result/error" + reference;
window.location.href = "admin/result/error/" + reference;
break;
};
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function sendReversalPaymentRequest(reference) {
window.location.href = "admin/result/received/" + reference;
break;
default:
window.location.href = "admin/result/error" + reference;
window.location.href = "admin/result/error/" + reference;
break;
};
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/ Sends POST request to url
// Sends POST request to url
async function sendPostRequest(url, data) {
const res = await fetch(url, {
method: "POST",
Expand All @@ -21,7 +21,7 @@ async function sendUpdatePaymentAmountRequest(reference, amount) {
window.location.href = "admin/result/received/" + reference;
break;
default:
window.location.href = "admin/result/error" + reference;
window.location.href = "admin/result/error/" + reference;
break;
};
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ a:hover {
}

.admin-panel-token-container {
background-color: white;
padding: 16px;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>ADMIN PANEL</h1>
payment updates asynchronously. </p>
<th:block th:if="${data != null and data.size() > 0}">
<div class="admin-panel-token-container">
<th:block th:each="payment, index : ${data}">
<th:block th:each="payment, item : ${data}">
<ul class="adminList">
<li><b>Merchant Reference: &nbsp;</b> <a th:href="@{'/admin/details/' + ${payment.merchantReference}}"><span th:text="${payment.merchantReference}"></span></a></li>
<li><b>Pre-authorisation PspReference: &nbsp;</b><span th:text="${payment.pspReference}"></span></li>
Expand All @@ -25,16 +25,15 @@ <h1>ADMIN PANEL</h1>
<li><b>Expiry Date: &nbsp;</b><span th:text="${payment.expiryDate}"></span>
&nbsp; (<span th:text="${payment.getDaysUntilExpiry()}"> </span> &nbsp; days until&nbsp;<a href="https://docs.adyen.com/online-payments/classic-integrations/modify-payments/adjust-authorisation#validity">expiry</a>)
</li>

<li><b>Actions: &nbsp;&nbsp;&nbsp;</b>
<a href="#/" th:onclick="showForm(adjustmentForm + ${index})">Adjust</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#/" th:onclick="showForm(extendForm + ${index})">Extend</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#/" th:onclick="showForm('captureForm' + ${index})">Capture</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#/" th:onclick="showForm('reversalForm' + ${index})">Reversal</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<li>
<b>Actions:&nbsp;&nbsp;&nbsp;</b>
<a href="#/" th:onclick="|showForm('adjustmentForm${item.index}')|">Adjust</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#/" th:onclick="|showForm('extendForm${item.index}')|">Extend</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#/" th:onclick="|showForm('captureForm${item.index}')|">Capture</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#/" th:onclick="|showForm('reversalForm${item.index}')|">Reversal</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</li>

<li th:id="'adjustmentForm' + ${index}" class="paymentOperationForm"
th:style="${'display:none;'}">
<li th:id="'adjustmentForm' + ${item.index}" class="paymentOperationForm" hidden>
<div>
<form name="updatePaymentAmountForm" method="post">
<b>Adjust amount:</b> <input type="text" name="amount"
Expand All @@ -48,12 +47,11 @@ <h1>ADMIN PANEL</h1>
</div>
</li>

<li th:id="'extendForm' + ${index}" class="paymentOperationForm"
th:style="${'display:none;'}">
<li th:id="'extendForm' + ${item.index}" class="paymentOperationForm" hidden>
<div>
<form name="extendPaymentForm" method="post">
<input type="hidden" name="amount"
th:value="${#numbers.formatDecimal(payment.amount, 2, 'COMMA', 2, 'POINT')}"/>
th:value="${(payment.amount /100d)}"/>
<input type="hidden" name="reference"
th:value="${payment.merchantReference}"/>
<button type="submit" class="submitButton">Extend</button>
Expand All @@ -62,8 +60,7 @@ <h1>ADMIN PANEL</h1>
</div>
</li>

<li th:id="'captureForm' + ${index}" class="paymentOperationForm"
th:style="${'display:none;'}">
<li th:id="'captureForm' + ${item.index}" class="paymentOperationForm" hidden>
<div>
<form name="capturePaymentForm" method="post">
<input type="hidden" name="reference"
Expand All @@ -74,8 +71,7 @@ <h1>ADMIN PANEL</h1>
</div>
</li>

<li th:id="'reversalForm' + ${index}" class="paymentOperationForm"
th:style="${'display:none;'}">
<li th:id="'reversalForm' + ${item.index}" class="paymentOperationForm" hidden>
<div>
<form name="reversalPaymentForm" method="post">
<input type="hidden" name="reference"
Expand All @@ -101,18 +97,19 @@ <h1>ADMIN PANEL</h1>
</div>
</th:block>
</div>
</div>

<script type="text/javascript" src="/adminpanel-scripts.js"></script>
<script src="/adminpanel-scripts.js"></script>

<!-- Binds all submit form buttons for `/update-payment-amount` endpoint -->
<script src="/adminpanel-updatePaymentAmount-bindings.js"></script>
<!-- Binds all submit form buttons for `/update-payment-amount` endpoint -->
<script src="/adminpanel-updatePaymentAmount-bindings.js"></script>

<!-- Binds all submit form buttons for `/capture-payment` endpoint -->
<script src="/adminpanel-capturePayment-bindings.js"></script>
<!-- Binds all submit form buttons for `/capture-payment` endpoint -->
<script src="/adminpanel-capturePayment-bindings.js"></script>

<!-- Binds all submit form buttons for `/reversal-payment` endpoint -->
<script src="/adminpanel-reversalPayment-bindings.js"></script>
<!-- Binds all submit form buttons for `/reversal-payment` endpoint -->
<script src="/adminpanel-reversalPayment-bindings.js"></script>

</div>

</body>
</html>
Loading

0 comments on commit 1fc8bad

Please sign in to comment.