mirror of
https://github.com/jlengrand/adyen-java-spark-online-payments.git
synced 2026-03-10 08:01:24 +00:00
optimize and cleanup
This commit is contained in:
@@ -2,24 +2,9 @@ package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.adyen.model.checkout.CheckoutPaymentsAction;
|
||||
import com.adyen.model.checkout.PaymentsDetailsRequest;
|
||||
import com.adyen.model.checkout.PaymentsRequest;
|
||||
import com.adyen.model.checkout.PaymentsResponse;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class FrontendParser {
|
||||
private static final Gson gson = new GsonBuilder().create();
|
||||
|
||||
// Deserialize payment information passed from frontend. Requires TypeAdapter for PaymentMethodDetails interface
|
||||
public static PaymentsRequest parsePayment(String body) {
|
||||
return gson.fromJson(body, PaymentsRequest.class);
|
||||
}
|
||||
|
||||
// Deserialize PaymentDetails generated by component
|
||||
public static PaymentsDetailsRequest parseDetails(String body) {
|
||||
return gson.fromJson(body, PaymentsDetailsRequest.class);
|
||||
}
|
||||
|
||||
// Format response being passed back to frontend. Only leave resultCode and action. Don't need to pass back
|
||||
// The rest of the information
|
||||
|
||||
@@ -8,9 +8,14 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import com.adyen.Client;
|
||||
import com.adyen.enums.Environment;
|
||||
import com.adyen.model.checkout.PaymentMethodsResponse;
|
||||
import com.adyen.model.checkout.PaymentsDetailsRequest;
|
||||
import com.adyen.model.checkout.PaymentsRequest;
|
||||
import com.adyen.model.checkout.PaymentsResponse;
|
||||
import com.adyen.service.Checkout;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import model.PaymentMethods;
|
||||
import model.Payments;
|
||||
@@ -30,18 +35,20 @@ public class Main {
|
||||
|
||||
private static final File FAVICON_PATH = new File("src/main/resources/static/img/favicon.ico");
|
||||
private static final String configFile = "config.properties";
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private static String apiKey = "";
|
||||
private static String clientKey = "";
|
||||
|
||||
public static String merchantAccount = "";
|
||||
public static String apiKey = "";
|
||||
public static String clientKey = "";
|
||||
|
||||
public static final Map<String, String> paymentDataStore = new HashMap<>();
|
||||
public static Checkout checkout;
|
||||
public static Map<String, String> paymentDataStore = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
port(8080);
|
||||
staticFiles.location("/static");
|
||||
readConfigFile();
|
||||
|
||||
checkout = new Checkout(new Client(apiKey, Environment.TEST));
|
||||
// Routes
|
||||
get("/", (req, res) -> {
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
@@ -67,17 +74,23 @@ public class Main {
|
||||
return RenderUtil.render(context, "templates/component.html");
|
||||
});
|
||||
|
||||
post("/api/getPaymentMethods", (req, res) -> PaymentMethods.getPaymentMethods(""));
|
||||
post("/api/getPaymentMethods", (req, res) -> {
|
||||
PaymentMethodsResponse response = PaymentMethods.getPaymentMethods("");
|
||||
return gson.toJson(response);
|
||||
});
|
||||
|
||||
post("/api/initiatePayment", (req, res) -> {
|
||||
System.out.println("Response received from client:\n" + req.body());
|
||||
PaymentsRequest request = FrontendParser.parsePayment(req.body());
|
||||
return Payments.makePayment(request);
|
||||
PaymentsRequest request = gson.fromJson(req.body(), PaymentsRequest.class);
|
||||
PaymentsResponse response = Payments.makePayment(request);
|
||||
return gson.toJson(response);
|
||||
|
||||
});
|
||||
|
||||
post("/api/submitAdditionalDetails", (req, res) -> {
|
||||
PaymentsDetailsRequest details = FrontendParser.parseDetails(req.body());
|
||||
return PaymentsDetails.getPaymentsDetails(details);
|
||||
PaymentsDetailsRequest details = gson.fromJson(req.body(), PaymentsDetailsRequest.class);
|
||||
PaymentsResponse paymentsDetails = PaymentsDetails.getPaymentsDetails(details);
|
||||
return gson.toJson(paymentsDetails);
|
||||
});
|
||||
|
||||
get("/api/handleShopperRedirect", (req, res) -> {
|
||||
@@ -94,7 +107,7 @@ public class Main {
|
||||
}
|
||||
detailsRequest.setPaymentData(paymentDataStore.get(queryMap.value("orderRef")));
|
||||
|
||||
PaymentsResponse response = PaymentsDetails.getPaymentsDetailsObject(detailsRequest);
|
||||
PaymentsResponse response = PaymentsDetails.getPaymentsDetails(detailsRequest);
|
||||
PaymentsResponse.ResultCodeEnum result = response.getResultCode();
|
||||
|
||||
setRedirect(result, res);
|
||||
@@ -113,7 +126,7 @@ public class Main {
|
||||
detailsRequest.setDetails(details);
|
||||
detailsRequest.setPaymentData(paymentDataStore.get(queryMap.value("orderRef")));
|
||||
|
||||
PaymentsResponse response = PaymentsDetails.getPaymentsDetailsObject(detailsRequest);
|
||||
PaymentsResponse response = PaymentsDetails.getPaymentsDetails(detailsRequest);
|
||||
PaymentsResponse.ResultCodeEnum result = response.getResultCode();
|
||||
|
||||
setRedirect(result, res);
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
package model;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.adyen.Client;
|
||||
import com.adyen.enums.Environment;
|
||||
import com.adyen.model.checkout.PaymentMethodsRequest;
|
||||
import com.adyen.model.checkout.PaymentMethodsResponse;
|
||||
import com.adyen.service.Checkout;
|
||||
import com.adyen.service.exception.ApiException;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import controller.Main;
|
||||
|
||||
public class PaymentMethods {
|
||||
|
||||
public static String getPaymentMethods(String type) {
|
||||
Client client = new Client(Main.apiKey, Environment.TEST);
|
||||
Checkout checkout = new Checkout(client);
|
||||
|
||||
public static PaymentMethodsResponse getPaymentMethods(String type) throws IOException, ApiException {
|
||||
PaymentMethodsRequest paymentMethodsRequest = new PaymentMethodsRequest();
|
||||
paymentMethodsRequest.setMerchantAccount(Main.merchantAccount);
|
||||
|
||||
@@ -25,14 +17,8 @@ public class PaymentMethods {
|
||||
paymentMethodsRequest.setShopperReference("SparkJava Checkout Shopper");
|
||||
System.out.println("/paymentMethods context:\n" + paymentMethodsRequest.toString());
|
||||
|
||||
try {
|
||||
PaymentMethodsResponse response = checkout.paymentMethods(paymentMethodsRequest);
|
||||
Gson gson = new GsonBuilder().create();
|
||||
String paymentMethodsResponseStringified = gson.toJson(response);
|
||||
System.out.println("/paymentMethods response:\n" + paymentMethodsResponseStringified);
|
||||
return paymentMethodsResponseStringified;
|
||||
} catch (ApiException | IOException e) {
|
||||
return e.toString();
|
||||
}
|
||||
PaymentMethodsResponse response = Main.checkout.paymentMethods(paymentMethodsRequest);
|
||||
System.out.println("/paymentMethods response:\n" + response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,10 @@ package model;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import com.adyen.Client;
|
||||
import com.adyen.enums.Environment;
|
||||
import com.adyen.model.Amount;
|
||||
import com.adyen.model.checkout.LineItem;
|
||||
import com.adyen.model.checkout.PaymentsRequest;
|
||||
import com.adyen.model.checkout.PaymentsResponse;
|
||||
import com.adyen.service.Checkout;
|
||||
import com.adyen.service.exception.ApiException;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
@@ -16,131 +13,117 @@ import com.google.gson.GsonBuilder;
|
||||
import controller.FrontendParser;
|
||||
import controller.Main;
|
||||
|
||||
import static controller.Main.paymentDataStore;
|
||||
|
||||
public class Payments {
|
||||
public static PaymentsResponse makePayment(PaymentsRequest paymentsRequest) throws IOException, ApiException {
|
||||
String type = paymentsRequest.getPaymentMethod().getType();
|
||||
|
||||
setAmount(paymentsRequest, type);
|
||||
paymentsRequest.setChannel(PaymentsRequest.ChannelEnum.WEB);
|
||||
paymentsRequest.setMerchantAccount(Main.merchantAccount);
|
||||
|
||||
String orderRef = UUID.randomUUID().toString();
|
||||
paymentsRequest.setReference(orderRef);
|
||||
paymentsRequest.setReturnUrl("http://localhost:8080/api/handleShopperRedirect?orderRef=" + orderRef);
|
||||
|
||||
paymentsRequest.setShopperReference("Java Checkout Shopper");
|
||||
|
||||
paymentsRequest.setCountryCode("NL");
|
||||
|
||||
if (type.equals("alipay")) {
|
||||
paymentsRequest.setCountryCode("CN");
|
||||
|
||||
} else if (type.contains("klarna")) {
|
||||
paymentsRequest.setShopperEmail("myEmail@adyen.com");
|
||||
paymentsRequest.setShopperLocale("en_US");
|
||||
|
||||
addLineItems(paymentsRequest);
|
||||
|
||||
} else if (type.equals("directEbanking") || type.equals("giropay")) {
|
||||
paymentsRequest.countryCode("DE");
|
||||
|
||||
} else if (type.equals("dotpay")) {
|
||||
paymentsRequest.countryCode("PL");
|
||||
paymentsRequest.getAmount().setCurrency("PLN");
|
||||
|
||||
} else if (type.equals("scheme")) {
|
||||
paymentsRequest.setOrigin("http://localhost:8080");
|
||||
paymentsRequest.putAdditionalDataItem("allow3DS2", "true");
|
||||
paymentsRequest.setShopperIP("0.0.0.1");
|
||||
|
||||
} else if (type.equals("ach") || type.equals("paypal")) {
|
||||
paymentsRequest.countryCode("US");
|
||||
}
|
||||
|
||||
System.out.println("/payments request:\n" + paymentsRequest.toString());
|
||||
|
||||
PaymentsResponse response = Main.checkout.payments(paymentsRequest);
|
||||
PaymentsResponse formattedResponse = FrontendParser.formatResponseForFrontend(response);
|
||||
|
||||
if (response.getAction() != null && !response.getAction().getPaymentData().isEmpty()) {
|
||||
// Set paymentData in local store for /details call after redirect
|
||||
Main.paymentDataStore.put(orderRef, response.getAction().getPaymentData());
|
||||
}
|
||||
|
||||
System.out.println("/payments response:\n" + formattedResponse);
|
||||
return formattedResponse;
|
||||
}
|
||||
|
||||
|
||||
public static String makePayment(PaymentsRequest paymentsRequest) {
|
||||
Client client = new Client(Main.apiKey, Environment.TEST);
|
||||
Checkout checkout = new Checkout(client);
|
||||
private static void setAmount(PaymentsRequest paymentsRequest, String type) {
|
||||
Amount amount = new Amount();
|
||||
|
||||
String type = paymentsRequest.getPaymentMethod().getType();
|
||||
String currency;
|
||||
|
||||
setAmount(paymentsRequest, type);
|
||||
paymentsRequest.setChannel(PaymentsRequest.ChannelEnum.WEB);
|
||||
paymentsRequest.setMerchantAccount(Main.merchantAccount);
|
||||
switch (type) {
|
||||
case "alipay":
|
||||
currency = "CNY";
|
||||
break;
|
||||
case "dotpay":
|
||||
currency = "PLN";
|
||||
break;
|
||||
case "boletobancario":
|
||||
currency = "BRL";
|
||||
break;
|
||||
case "ach":
|
||||
case "paypal":
|
||||
currency = "USD";
|
||||
break;
|
||||
default:
|
||||
currency = "EUR";
|
||||
}
|
||||
|
||||
String orderRef = UUID.randomUUID().toString();
|
||||
paymentsRequest.setReference(orderRef);
|
||||
paymentsRequest.setReturnUrl("http://localhost:8080/api/handleShopperRedirect?orderRef=" + orderRef);
|
||||
amount.setCurrency(currency);
|
||||
amount.setValue(1000L);
|
||||
paymentsRequest.setAmount(amount);
|
||||
}
|
||||
|
||||
paymentsRequest.setShopperReference("Java Checkout Shopper");
|
||||
private static void addLineItems(PaymentsRequest paymentsRequest) {
|
||||
String item1 = "{\n" +
|
||||
" \"quantity\": \"1\",\n" +
|
||||
" \"amountExcludingTax\": \"450\",\n" +
|
||||
" \"taxPercentage\": \"1111\",\n" +
|
||||
" \"description\": \"Sunglasses\",\n" +
|
||||
" \"id\": \"Item #1\",\n" +
|
||||
" \"taxAmount\": \"50\",\n" +
|
||||
" \"amountIncludingTax\": \"500\",\n" +
|
||||
" \"taxCategory\": \"High\"\n" +
|
||||
" }";
|
||||
String item2 = "{\n" +
|
||||
" \"quantity\": \"1\",\n" +
|
||||
" \"amountExcludingTax\": \"450\",\n" +
|
||||
" \"taxPercentage\": \"1111\",\n" +
|
||||
" \"description\": \"Headphones\",\n" +
|
||||
" \"id\": \"Item #2\",\n" +
|
||||
" \"taxAmount\": \"50\",\n" +
|
||||
" \"amountIncludingTax\": \"500\",\n" +
|
||||
" \"taxCategory\": \"High\"\n" +
|
||||
" }";
|
||||
|
||||
paymentsRequest.setCountryCode("NL");
|
||||
Gson gson = new GsonBuilder().create();
|
||||
LineItem lineItem1 = gson.fromJson(item1, LineItem.class);
|
||||
LineItem lineItem2 = gson.fromJson(item2, LineItem.class);
|
||||
|
||||
if (type.equals("alipay")) {
|
||||
paymentsRequest.setCountryCode("CN");
|
||||
|
||||
} else if (type.contains("klarna")) {
|
||||
paymentsRequest.setShopperEmail("myEmail@adyen.com");
|
||||
paymentsRequest.setShopperLocale("en_US");
|
||||
|
||||
addLineItems(paymentsRequest);
|
||||
|
||||
} else if (type.equals("directEbanking") || type.equals("giropay")) {
|
||||
paymentsRequest.countryCode("DE");
|
||||
|
||||
} else if (type.equals("dotpay")) {
|
||||
paymentsRequest.countryCode("PL");
|
||||
paymentsRequest.getAmount().setCurrency("PLN");
|
||||
|
||||
} else if (type.equals("scheme")) {
|
||||
paymentsRequest.setOrigin("http://localhost:8080");
|
||||
paymentsRequest.putAdditionalDataItem("allow3DS2", "true");
|
||||
paymentsRequest.setShopperIP("0.0.0.1");
|
||||
|
||||
} else if (type.equals("ach") || type.equals("paypal")) {
|
||||
paymentsRequest.countryCode("US");
|
||||
}
|
||||
|
||||
System.out.println("/payments request:\n" + paymentsRequest.toString());
|
||||
|
||||
try {
|
||||
PaymentsResponse response = checkout.payments(paymentsRequest);
|
||||
PaymentsResponse formattedResponse = FrontendParser.formatResponseForFrontend(response);
|
||||
|
||||
if (response.getAction() != null && !response.getAction().getPaymentData().isEmpty()) {
|
||||
// Set paymentData in local store for /details call after redirect
|
||||
paymentDataStore.put(orderRef, response.getAction().getPaymentData());
|
||||
}
|
||||
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
Gson gson = builder.create();
|
||||
String paymentsResponse = gson.toJson(formattedResponse);
|
||||
System.out.println("/payments response:\n" + paymentsResponse);
|
||||
return paymentsResponse;
|
||||
} catch (ApiException | IOException e) {
|
||||
return e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void setAmount(PaymentsRequest paymentsRequest, String type) {
|
||||
Amount amount = new Amount();
|
||||
|
||||
String currency;
|
||||
|
||||
switch (type) {
|
||||
case "alipay":
|
||||
currency = "CNY";
|
||||
break;
|
||||
case "dotpay":
|
||||
currency = "PLN";
|
||||
break;
|
||||
case "boletobancario":
|
||||
currency = "BRL";
|
||||
break;
|
||||
case "ach":
|
||||
case "paypal":
|
||||
currency = "USD";
|
||||
break;
|
||||
default:
|
||||
currency = "EUR";
|
||||
}
|
||||
|
||||
amount.setCurrency(currency);
|
||||
amount.setValue(1000L);
|
||||
paymentsRequest.setAmount(amount);
|
||||
}
|
||||
|
||||
private static void addLineItems(PaymentsRequest paymentsRequest) {
|
||||
String item1 = "{\n" +
|
||||
" \"quantity\": \"1\",\n" +
|
||||
" \"amountExcludingTax\": \"450\",\n" +
|
||||
" \"taxPercentage\": \"1111\",\n" +
|
||||
" \"description\": \"Sunglasses\",\n" +
|
||||
" \"id\": \"Item #1\",\n" +
|
||||
" \"taxAmount\": \"50\",\n" +
|
||||
" \"amountIncludingTax\": \"500\",\n" +
|
||||
" \"taxCategory\": \"High\"\n" +
|
||||
" }";
|
||||
String item2 = "{\n" +
|
||||
" \"quantity\": \"1\",\n" +
|
||||
" \"amountExcludingTax\": \"450\",\n" +
|
||||
" \"taxPercentage\": \"1111\",\n" +
|
||||
" \"description\": \"Headphones\",\n" +
|
||||
" \"id\": \"Item #2\",\n" +
|
||||
" \"taxAmount\": \"50\",\n" +
|
||||
" \"amountIncludingTax\": \"500\",\n" +
|
||||
" \"taxCategory\": \"High\"\n" +
|
||||
" }";
|
||||
|
||||
Gson gson = new GsonBuilder().create();
|
||||
LineItem lineItem1 = gson.fromJson(item1, LineItem.class);
|
||||
LineItem lineItem2 = gson.fromJson(item2, LineItem.class);
|
||||
|
||||
paymentsRequest.addLineItemsItem(lineItem1);
|
||||
paymentsRequest.addLineItemsItem(lineItem2);
|
||||
}
|
||||
paymentsRequest.addLineItemsItem(lineItem1);
|
||||
paymentsRequest.addLineItemsItem(lineItem2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +1,19 @@
|
||||
package model;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.adyen.Client;
|
||||
import com.adyen.enums.Environment;
|
||||
import com.adyen.model.checkout.PaymentsDetailsRequest;
|
||||
import com.adyen.model.checkout.PaymentsResponse;
|
||||
import com.adyen.service.Checkout;
|
||||
import com.adyen.service.exception.ApiException;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import controller.Main;
|
||||
|
||||
public class PaymentsDetails {
|
||||
|
||||
public static String getPaymentsDetails(PaymentsDetailsRequest paymentsDetailsRequest) {
|
||||
|
||||
PaymentsResponse paymentsDetailsResponse = makePaymentDetailsRequest(paymentsDetailsRequest);
|
||||
|
||||
Gson gson = new GsonBuilder().create();
|
||||
return gson.toJson(paymentsDetailsResponse);
|
||||
}
|
||||
|
||||
|
||||
public static PaymentsResponse getPaymentsDetailsObject(PaymentsDetailsRequest paymentsDetailsRequest) {
|
||||
|
||||
return makePaymentDetailsRequest(paymentsDetailsRequest);
|
||||
}
|
||||
|
||||
|
||||
private static PaymentsResponse makePaymentDetailsRequest(PaymentsDetailsRequest paymentsDetailsRequest) {
|
||||
Client client = new Client(Main.apiKey, Environment.TEST);
|
||||
Checkout checkout = new Checkout(client);
|
||||
|
||||
public static PaymentsResponse getPaymentsDetails(PaymentsDetailsRequest paymentsDetailsRequest) throws IOException, ApiException {
|
||||
System.out.println("/paymentsDetails request:" + paymentsDetailsRequest.toString());
|
||||
PaymentsResponse paymentsDetailsResponse = null;
|
||||
try {
|
||||
paymentsDetailsResponse = checkout.paymentsDetails(paymentsDetailsRequest);
|
||||
|
||||
} catch (ApiException | IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (paymentsDetailsResponse != null) {
|
||||
System.out.println("paymentsDetails response:\n" + paymentsDetailsResponse.toString());
|
||||
}
|
||||
}
|
||||
paymentsDetailsResponse = Main.checkout.paymentsDetails(paymentsDetailsRequest);
|
||||
System.out.println("paymentsDetails response:\n" + paymentsDetailsResponse.toString());
|
||||
return paymentsDetailsResponse;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user