Skip to content

Commit

Permalink
Added all payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwok-he-Chu committed Nov 16, 2023
1 parent ce1e026 commit 9badbaa
Show file tree
Hide file tree
Showing 14 changed files with 537 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.adyen.ipp.api;

import com.adyen.Client;
import com.adyen.enums.Environment;
import com.adyen.model.nexo.*;
import com.adyen.ipp.ApplicationProperty;
import com.adyen.model.checkout.CreateCheckoutSessionResponse;
import com.adyen.ipp.model.*;
import com.adyen.ipp.request.*;
import com.adyen.ipp.response.*;
import com.adyen.ipp.service.*;
import com.adyen.ipp.util.IdUtility;
import com.adyen.service.exception.ApiException;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,22 +26,182 @@ public class InPersonPaymentsController {

private final ApplicationProperty applicationProperty;

@Autowired
private PosAbortService posAbortService;

@Autowired
private PosPaymentService posPaymentService;

@Autowired
private PosReversalService posReversalService;

@Autowired
private PosTransactionStatusService posTransactionStatusService;

@Autowired
private TableService tableService;

private String saleId;

private String poiId;

@Autowired
public InPersonPaymentsController(ApplicationProperty applicationProperty) {

this.applicationProperty = applicationProperty;
saleId = applicationProperty.getSaleId();
poiId = applicationProperty.getPoiId();
}

if(applicationProperty.getApiKey() == null) {
log.warn("ADYEN_KEY is UNDEFINED");
throw new RuntimeException("ADYEN_KEY is UNDEFINED");
@PostMapping("/create-payment")
public ResponseEntity<CreatePaymentResponse> createPayment(@RequestBody CreatePaymentRequest request) throws IOException, ApiException {
Table table = tableService.getTables().stream()
.filter(t -> t.getTableName().equals(request.getTableName()))
.findFirst()
.orElse(null);

if (table == null) {
return ResponseEntity
.status(404)
.body(new CreatePaymentResponse()
.result("failure")
.refusalReason("Table " + request.getTableName() + " not found"));
}

var client = new Client(applicationProperty.getApiKey(), Environment.TEST);
String serviceId = IdUtility.getRandomAlphanumericId(10);

table.getPaymentStatusDetails().setServiceId(serviceId);
table.setPaymentStatus(PaymentStatus.PaymentInProgress);

try {
var response = posPaymentService.sendPaymentRequestAsync(serviceId, poiId, saleId, request.getCurrency(), request.getAmount());

if (response == null || response.getSaleToPOIResponse() == null || response.getSaleToPOIResponse().getPaymentResponse() == null) {
table.setPaymentStatus(PaymentStatus.NotPaid);
return ResponseEntity
.badRequest()
.body(new CreatePaymentResponse()
.result("failure")
.refusalReason("Empty payment response"));
}

var paymentResponse = response.getSaleToPOIResponse().getPaymentResponse();

switch (paymentResponse.getResponse().getResult()) {
case SUCCESS:
table.setPaymentStatus(PaymentStatus.Paid);
table.getPaymentStatusDetails().setPoiTransactionId(paymentResponse.getPOIData().getPOITransactionID().getTransactionID());
table.getPaymentStatusDetails().setPoiTransactionTimeStamp(paymentResponse.getPOIData().getPOITransactionID().getTimeStamp());
table.getPaymentStatusDetails().setSaleTransactionId(paymentResponse.getSaleData().getSaleTransactionID().getTransactionID());
table.getPaymentStatusDetails().setSaleTransactionTimeStamp(paymentResponse.getSaleData().getSaleTransactionID().getTimeStamp());

return ResponseEntity
.ok()
.body(new CreatePaymentResponse()
.result("success"));
case FAILURE:
table.setPaymentStatus(PaymentStatus.NotPaid);
table.getPaymentStatusDetails().setRefusalReason("Payment terminal responded with: " + paymentResponse.getResponse().getErrorCondition());
table.getPaymentStatusDetails().setPoiTransactionId(paymentResponse.getPOIData().getPOITransactionID().getTransactionID());
table.getPaymentStatusDetails().setPoiTransactionTimeStamp(paymentResponse.getPOIData().getPOITransactionID().getTimeStamp());
table.getPaymentStatusDetails().setSaleTransactionId(paymentResponse.getSaleData().getSaleTransactionID().getTransactionID());
table.getPaymentStatusDetails().setSaleTransactionTimeStamp(paymentResponse.getSaleData().getSaleTransactionID().getTimeStamp());

return ResponseEntity
.ok()
.body(new CreatePaymentResponse()
.result("failure")
.refusalReason(table.getPaymentStatusDetails().getRefusalReason()));
default:
table.setPaymentStatus(PaymentStatus.NotPaid);

return ResponseEntity
.badRequest()
.body(new CreatePaymentResponse()
.result("failure")
.refusalReason("Could not reach payment terminal with POI ID " + poiId));
}

} catch (IOException | ApiException e) {
log.error(e.toString());
table.setPaymentStatus(PaymentStatus.NotPaid);
throw e;
}
}

@PostMapping("/create-reversal")
public ResponseEntity<CreateReversalResponse> createReversal(@RequestBody CreateReversalRequest request) throws IOException, ApiException {
Table table = tableService.getTables().stream()
.filter(t -> t.getTableName().equals(request.getTableName()))
.findFirst()
.orElse(null);

if (table == null) {
return ResponseEntity
.status(404)
.body(new CreateReversalResponse()
.result("failure")
.refusalReason("Table " + request.getTableName() + " not found"));
}

try {
var response = posReversalService.sendReversalRequestAsync(ReversalReasonType.MERCHANT_CANCEL, table.getPaymentStatusDetails().getSaleTransactionId(), table.getPaymentStatusDetails().getPoiTransactionId(), poiId, saleId);

if (response == null || response.getSaleToPOIResponse() == null || response.getSaleToPOIResponse().getReversalResponse() == null) {
return ResponseEntity
.badRequest()
.body(new CreateReversalResponse()
.result("failure")
.refusalReason("Empty reversal response"));
}

var reversalResponse = response.getSaleToPOIResponse().getReversalResponse();

switch (reversalResponse.getResponse().getResult()) {
case SUCCESS:
table.setPaymentStatus(PaymentStatus.RefundInProgress);
return ResponseEntity
.ok()
.body(new CreateReversalResponse()
.result("success"));
case FAILURE:
table.setPaymentStatus(PaymentStatus.RefundFailed);
return ResponseEntity
.ok()
.body(new CreateReversalResponse()
.result("failure")
.refusalReason("Payment terminal responded with: " + java.net.URLDecoder.decode(reversalResponse.getResponse().getAdditionalResponse())));
default:
return ResponseEntity
.badRequest()
.body(new CreateReversalResponse()
.result("failure")
.refusalReason("Could not reach payment terminal with POI ID " + poiId));
}
} catch (IOException | ApiException e) {
log.error(e.toString());
throw e;
}
}

@PostMapping("/ipp")
public ResponseEntity sessions(@RequestHeader String host, HttpServletRequest request) throws IOException, ApiException {
var response = "";
return ResponseEntity.ok().body(response);
@GetMapping("/abort/{tableName}")
public ResponseEntity abort(@PathVariable String tableName) throws IOException, ApiException {
try {
Table table = tableService.getTables().stream()
.filter(t -> t.getTableName().equals(tableName))
.findFirst()
.orElse(null);

if (table == null || table.getPaymentStatusDetails() == null || table.getPaymentStatusDetails().getServiceId() == null) {
return ResponseEntity.notFound().build();
}

var abortResponse = posAbortService.sendAbortRequestAsync(table.getPaymentStatusDetails().getServiceId(), poiId, saleId);

return ResponseEntity.ok().body(abortResponse);
} catch (IOException | ApiException e) {
log.error(e.toString());
throw e;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.adyen.ipp.model;

import java.time.LocalDateTime;
import javax.xml.datatype.XMLGregorianCalendar;

public class PaymentStatusDetails {
/**
Expand All @@ -23,7 +23,7 @@ public class PaymentStatusDetails {
/**
* Date of the POI transaction.
*/
private LocalDateTime poiTransactionTimeStamp;
private XMLGregorianCalendar poiTransactionTimeStamp;

/**
* The SaleTransactionId (SaleReferenceId), populated when a TableStatus is set to PaymentStatus.Paid.
Expand All @@ -34,7 +34,7 @@ public class PaymentStatusDetails {
/**
* Date of the Sale transaction.
*/
private LocalDateTime saleTransactionTimeStamp;
private XMLGregorianCalendar saleTransactionTimeStamp;

/**
* The unique ID of a message pair, which processes the transaction. The value is assigned when you initiate a payment transaction to the terminal, and used to cancel/abort the request.
Expand Down Expand Up @@ -66,11 +66,11 @@ public void setPoiTransactionId(String poiTransactionId) {
this.poiTransactionId = poiTransactionId;
}

public LocalDateTime getPoiTransactionTimeStamp() {
public XMLGregorianCalendar getPoiTransactionTimeStamp() {
return poiTransactionTimeStamp;
}

public void setPoiTransactionTimeStamp(LocalDateTime poiTransactionTimeStamp) {
public void setPoiTransactionTimeStamp(XMLGregorianCalendar poiTransactionTimeStamp) {
this.poiTransactionTimeStamp = poiTransactionTimeStamp;
}

Expand All @@ -82,11 +82,11 @@ public void setSaleTransactionId(String saleTransactionId) {
this.saleTransactionId = saleTransactionId;
}

public LocalDateTime getSaleTransactionTimeStamp() {
public XMLGregorianCalendar getSaleTransactionTimeStamp() {
return saleTransactionTimeStamp;
}

public void setSaleTransactionTimeStamp(LocalDateTime saleTransactionTimeStamp) {
public void setSaleTransactionTimeStamp(XMLGregorianCalendar saleTransactionTimeStamp) {
this.saleTransactionTimeStamp = saleTransactionTimeStamp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ public PaymentStatusDetails getPaymentStatusDetails() {
return paymentStatusDetails;
}

public void setPaymentStatusDetails(PaymentStatusDetails paymentStatusDetails) {
this.paymentStatusDetails = paymentStatusDetails;
}
public void setPaymentStatusDetails(PaymentStatusDetails paymentStatusDetails) { this.paymentStatusDetails = paymentStatusDetails; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public String getRefusalReason() {
public void setRefusalReason(String refusalReason) {
this.refusalReason = refusalReason;
}

public CreatePaymentResponse refusalReason(String refusalReason){
this.refusalReason = refusalReason;
return this;
}
public CreatePaymentResponse result(String result){
this.result = result;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.adyen.ipp.response;

public class CreateReversalResponse {
private String result;
private String refusalReason;
Expand All @@ -18,4 +19,13 @@ public String getRefusalReason() {
public void setRefusalReason(String refusalReason) {
this.refusalReason = refusalReason;
}

public CreateReversalResponse refusalReason(String refusalReason){
this.refusalReason = refusalReason;
return this;
}
public CreateReversalResponse result(String result){
this.result = result;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.adyen.ipp.service;

import com.adyen.ipp.util.IdUtility;
import com.adyen.service.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.adyen.model.nexo.*;
import com.adyen.model.terminal.*;

import java.io.IOException;

@Service
public class PosAbortService {
@Autowired
private TerminalCloudApiService terminalCloudAPIService;

public TerminalAPIResponse sendAbortRequestAsync(String serviceId, String poiId, String saleId) throws IOException, ApiException {
TerminalAPIRequest request = getAbortRequest(serviceId, poiId, saleId);
return terminalCloudAPIService.getTerminalCloudApi().sync(request);
}

private TerminalAPIRequest getAbortRequest(String serviceId, String poiId, String saleId) {
var messageHeader = new MessageHeader();
messageHeader.setMessageCategory(MessageCategoryType.ABORT);
messageHeader.setMessageClass(MessageClassType.SERVICE);
messageHeader.setMessageType(MessageType.REQUEST);
messageHeader.setPOIID(poiId);
messageHeader.setSaleID(saleId);
messageHeader.setServiceID(IdUtility.getRandomAlphanumericId(10));

var messageReference = new MessageReference();
messageReference.setMessageCategory(MessageCategoryType.PAYMENT);
messageReference.setServiceID(serviceId);
messageReference.setPOIID(poiId);
messageReference.setSaleID(saleId);

var abortRequest = new AbortRequest();
abortRequest.setAbortReason("MerchantAbort");
abortRequest.setMessageReference(messageReference);

var saleToPOIRequest = new SaleToPOIRequest();
saleToPOIRequest.setMessageHeader(messageHeader);
saleToPOIRequest.setAbortRequest(abortRequest);

var terminalAPIRequest = new TerminalAPIRequest();
terminalAPIRequest.setSaleToPOIRequest(saleToPOIRequest);

return terminalAPIRequest;
}
}
Loading

0 comments on commit 9badbaa

Please sign in to comment.