Commit 310349fc authored by Darrick Yong's avatar Darrick Yong

separated vendors, added global exception handling, logger, and mock JSON in resources

parent 9e8fe98d
......@@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
target/
### STS ###
.apt_generated
......
package com.nisum.paymentvendorsimulator.afterpay.controllers;
import com.nisum.paymentvendorsimulator.afterpay.services.AfterpayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/afterpay")
@CrossOrigin(origins = "*")
public class AfterpayController {
private static final Logger logger = LoggerFactory.getLogger(AfterpayController.class);
@Autowired
AfterpayService apService;
@PostMapping(value = "/authorize", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void authorize() {
logger.info("this is AP controller");
apService.authorize();
}
}
package com.nisum.paymentvendorsimulator.afterpay.services;
import com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardRequest;
import com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class AfterpayService {
private static final Logger logger = LoggerFactory.getLogger(AfterpayService.class);
public void authorize() {
logger.info("AP Service");
}
}
package com.nisum.paymentvendorsimulator.creditcard.controllers;
import com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardRequest;
import com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardResponse;
import com.nisum.paymentvendorsimulator.creditcard.services.CreditCardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/cc")
@CrossOrigin(origins = "*")
public class CreditCardController {
private static final Logger logger = LoggerFactory.getLogger(CreditCardController.class);
@Autowired
CreditCardService ccService;
@PostMapping(value = "/authorize", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public CreditCardResponse authorize(@RequestBody CreditCardRequest ccRequest) {
logger.info("this is controller");
return ccService.authorize(ccRequest);
}
}
package com.nisum.paymentvendorsimulator.creditcard.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class CreditCardAmount {
String value;
String currency;
public CreditCardAmount() {
}
}
package com.nisum.paymentvendorsimulator.creditcard.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class CreditCardData {
String number;
String expiryMonth;
String expiryYear;
String cvc;
String holderName;
public CreditCardData() {
}
}
package com.nisum.paymentvendorsimulator.creditcard.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class CreditCardRequest {
CreditCardData card;
CreditCardAmount amount;
String reference;
String merchantAccount;
// String returnUrl; // for Adyen payment, not classic integration, do we need?
public CreditCardRequest() {
}
@Override
public String toString() {
return "CreditCardRequest{" +
"card=" + card +
", amount=" + amount +
", reference='" + reference + '\'' +
", merchantAccount='" + merchantAccount + '\'' +
'}';
}
}
package com.nisum.paymentvendorsimulator.creditcard.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class CreditCardResponse {
String pspReference;
String resultCode;
String authCode;
public CreditCardResponse() {
}
}
package com.nisum.paymentvendorsimulator.creditcard.services;
import com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardRequest;
import com.nisum.paymentvendorsimulator.creditcard.domain.dto.CreditCardResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class CreditCardService {
private static final Logger logger = LoggerFactory.getLogger(CreditCardService.class);
public CreditCardResponse authorize(CreditCardRequest ccRequest) {
logger.info(String.valueOf(ccRequest));
return new CreditCardResponse("12312313", "Authorised", "092467");
}
}
package com.nisum.paymentvendorsimulator.exceptions;
import org.springframework.stereotype.Controller;
@Controller
public abstract class BaseController {
}
package com.nisum.paymentvendorsimulator.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.time.LocalDateTime;
@ControllerAdvice
public class BaseControllerAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(PayPalTestException.class)
public ResponseEntity<PayPalExceptionResponse> handleException() {
PayPalExceptionResponse response = new PayPalExceptionResponse();
response.setDateTime(LocalDateTime.now());
response.setMessage("Id is not supposed to be 123");
ResponseEntity<PayPalExceptionResponse> entity = new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
return entity;
}
}
package com.nisum.paymentvendorsimulator.exceptions;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Getter
@Setter
public class PayPalExceptionResponse {
private String message;
private LocalDateTime dateTime;
}
package com.nisum.paymentvendorsimulator.exceptions;
public class PayPalTestException extends RuntimeException {
}
package com.nisum.paymentvendorsimulator.controllers;
package com.nisum.paymentvendorsimulator.paypal.controllers;
import com.nisum.paymentvendorsimulator.domain.dto.SimulatorResponse;
import com.nisum.paymentvendorsimulator.services.SimulatorService;
import com.nisum.paymentvendorsimulator.exceptions.BaseController;
import com.nisum.paymentvendorsimulator.exceptions.PayPalTestException;
import com.nisum.paymentvendorsimulator.paypal.domain.dto.PayPalResponse;
import com.nisum.paymentvendorsimulator.paypal.services.PayPalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
@RequestMapping("/api/paypal")
@CrossOrigin(origins = "*")
public class SimulatorController {
public class PayPalController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(SimulatorController.class);
private static final Logger logger = LoggerFactory.getLogger(PayPalController.class);
@Autowired
SimulatorService simService;
PayPalService ppService;
@PostMapping(value = "/authorize/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public SimulatorResponse authorize(@PathVariable(value = "id") String id) {
logger.info("this is controller");
return simService.authorize(id);
public PayPalResponse authorize(@PathVariable(value = "id") String id) {
logger.info("this is PayPal Controller");
if(id.equals("123")) throw new PayPalTestException();
return ppService.authorize(id);
}
}
package com.nisum.paymentvendorsimulator.domain.dto;
package com.nisum.paymentvendorsimulator.paypal.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
......@@ -7,11 +7,11 @@ import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class SimulatorAmount {
public class PayPalAmount {
String value;
String currency_code;
public SimulatorAmount() {
public PayPalAmount() {
}
}
package com.nisum.paymentvendorsimulator.domain.dto;
package com.nisum.paymentvendorsimulator.paypal.domain.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
......@@ -7,11 +7,11 @@ import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class SimulatorLink {
public class PayPalLink {
String rel;
String method;
String href;
public SimulatorLink() {
public PayPalLink() {
}
}
package com.nisum.paymentvendorsimulator.domain.dto;
package com.nisum.paymentvendorsimulator.paypal.domain.dto;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class SimulatorResponse {
public class PayPalResponse {
String id;
String status;
SimulatorAmount amount;
PayPalAmount amount;
String invoice_id;
SimulatorSellerProtection seller_protection;
PayPalSellerProtection seller_protection;
LocalDateTime expiration_time;
LocalDateTime create_time;
LocalDateTime update_time;
List<SimulatorLink> links;
List<PayPalLink> links;
public SimulatorResponse() {
public PayPalResponse() {
}
}
......
package com.nisum.paymentvendorsimulator.domain.dto;
package com.nisum.paymentvendorsimulator.paypal.domain.dto;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class SimulatorSellerProtection {
public class PayPalSellerProtection {
String status;
List<String> dispute_categories;
public SimulatorSellerProtection() {
public PayPalSellerProtection() {
}
}
package com.nisum.paymentvendorsimulator.services;
package com.nisum.paymentvendorsimulator.paypal.services;
import com.nisum.paymentvendorsimulator.domain.dto.SimulatorAmount;
import com.nisum.paymentvendorsimulator.domain.dto.SimulatorLink;
import com.nisum.paymentvendorsimulator.domain.dto.SimulatorResponse;
import com.nisum.paymentvendorsimulator.domain.dto.SimulatorSellerProtection;
import com.nisum.paymentvendorsimulator.paypal.domain.dto.PayPalAmount;
import com.nisum.paymentvendorsimulator.paypal.domain.dto.PayPalLink;
import com.nisum.paymentvendorsimulator.paypal.domain.dto.PayPalResponse;
import com.nisum.paymentvendorsimulator.paypal.domain.dto.PayPalSellerProtection;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.time.LocalDateTime;
import java.util.Arrays;
@Service
public class SimulatorService {
public class PayPalService {
public SimulatorResponse authorize(String id) {
public PayPalResponse authorize(String id) {
return new SimulatorResponse(
return new PayPalResponse(
id,
"CREATED",
new SimulatorAmount("10.99", "USD"),
new PayPalAmount("10.99", "USD"),
"INVOICE-123",
new SimulatorSellerProtection("ELIGIBLE", Arrays.asList("ITEM_NOT_RECEIVED", "UNAUTHORIZED_TRANSACTION")),
new PayPalSellerProtection("ELIGIBLE", Arrays.asList("ITEM_NOT_RECEIVED", "UNAUTHORIZED_TRANSACTION")),
LocalDateTime.of(2019, 10, 10, 23, 23, 45),
LocalDateTime.of(2017, 10, 10, 23, 23, 45),
LocalDateTime.of(2018, 10, 10, 23, 23, 45),
Arrays.asList(
new SimulatorLink("self","GET", "https://api-m.paypal.com/v2/payments/authorizations/" + id ),
new SimulatorLink("capture","POST", "https://api-m.paypal.com/v2/payments/authorizations/" + id + "/capture" )
new PayPalLink("self","GET", "https://api-m.paypal.com/v2/payments/authorizations/" + id ),
new PayPalLink("capture","POST", "https://api-m.paypal.com/v2/payments/authorizations/" + id + "/capture" )
)
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment