mirror of
https://github.com/jlengrand/adyen-java-spark-online-payments.git
synced 2026-03-10 08:01:24 +00:00
update to latest libs
This commit is contained in:
@@ -12,7 +12,7 @@ Project name: ${project.name}
|
||||
"""
|
||||
|
||||
application {
|
||||
mainClassName = 'controller.Main'
|
||||
mainClassName = 'checkout.Application'
|
||||
}
|
||||
|
||||
repositories {
|
||||
@@ -24,5 +24,5 @@ dependencies {
|
||||
implementation 'org.slf4j:slf4j-simple:1.7.25'
|
||||
implementation 'com.sparkjava:spark-template-jinjava:2.7.1'
|
||||
implementation 'org.apache.httpcomponents:httpclient:4.5.11'
|
||||
implementation 'com.adyen:adyen-java-api-library:11.0.1'
|
||||
implementation 'com.adyen:adyen-java-api-library:13.0.0'
|
||||
}
|
||||
|
||||
@@ -1,25 +1,15 @@
|
||||
package controller;
|
||||
package checkout;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
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.adyen.model.checkout.*;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import model.PaymentMethods;
|
||||
import model.Payments;
|
||||
import model.PaymentsDetails;
|
||||
import spark.QueryParamsMap;
|
||||
import spark.Response;
|
||||
import view.RenderUtil;
|
||||
@@ -31,25 +21,20 @@ import static spark.Spark.post;
|
||||
import static spark.Spark.staticFiles;
|
||||
|
||||
|
||||
public class Main {
|
||||
public class Application {
|
||||
|
||||
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 String CONFIG_FILE = "config.properties";
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private static String apiKey = "";
|
||||
private static String clientKey = "";
|
||||
|
||||
public static String merchantAccount = "";
|
||||
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));
|
||||
Properties prop = readConfigFile();
|
||||
String clientKey = prop.getProperty("clientKey");
|
||||
CheckoutService checkoutService = new CheckoutService(prop);
|
||||
|
||||
// Routes
|
||||
|
||||
get("/", (req, res) -> {
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
return RenderUtil.render(context, "templates/home.html");
|
||||
@@ -74,66 +59,6 @@ public class Main {
|
||||
return RenderUtil.render(context, "templates/component.html");
|
||||
});
|
||||
|
||||
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 = gson.fromJson(req.body(), PaymentsRequest.class);
|
||||
PaymentsResponse response = Payments.makePayment(request);
|
||||
return gson.toJson(response);
|
||||
|
||||
});
|
||||
|
||||
post("/api/submitAdditionalDetails", (req, res) -> {
|
||||
PaymentsDetailsRequest details = gson.fromJson(req.body(), PaymentsDetailsRequest.class);
|
||||
PaymentsResponse paymentsDetails = PaymentsDetails.getPaymentsDetails(details);
|
||||
return gson.toJson(paymentsDetails);
|
||||
});
|
||||
|
||||
get("/api/handleShopperRedirect", (req, res) -> {
|
||||
System.out.println("GET redirect handler");
|
||||
|
||||
PaymentsDetailsRequest detailsRequest = new PaymentsDetailsRequest();
|
||||
QueryParamsMap queryMap = req.queryMap();
|
||||
|
||||
if (queryMap.hasKey("redirectResult")) {
|
||||
detailsRequest.setDetails(Collections.singletonMap("redirectResult", queryMap.value("redirectResult")));
|
||||
|
||||
} else if (queryMap.hasKey("payload")) {
|
||||
detailsRequest.setDetails(Collections.singletonMap("payload", queryMap.value("payload")));
|
||||
}
|
||||
detailsRequest.setPaymentData(paymentDataStore.get(queryMap.value("orderRef")));
|
||||
|
||||
PaymentsResponse response = PaymentsDetails.getPaymentsDetails(detailsRequest);
|
||||
PaymentsResponse.ResultCodeEnum result = response.getResultCode();
|
||||
|
||||
setRedirect(result, res);
|
||||
return res;
|
||||
});
|
||||
|
||||
|
||||
post("/api/handleShopperRedirect", (req, res) -> {
|
||||
System.out.println("POST redirect handler");
|
||||
QueryParamsMap queryMap = req.queryMap();
|
||||
|
||||
PaymentsDetailsRequest detailsRequest = new PaymentsDetailsRequest();
|
||||
HashMap<String, String> details = new HashMap<>();
|
||||
details.put("MD", queryMap.value("MD"));
|
||||
details.put("PaRes", queryMap.value("PaRes"));
|
||||
detailsRequest.setDetails(details);
|
||||
detailsRequest.setPaymentData(paymentDataStore.get(queryMap.value("orderRef")));
|
||||
|
||||
PaymentsResponse response = PaymentsDetails.getPaymentsDetails(detailsRequest);
|
||||
PaymentsResponse.ResultCodeEnum result = response.getResultCode();
|
||||
|
||||
setRedirect(result, res);
|
||||
return res;
|
||||
});
|
||||
|
||||
|
||||
path("/result", () -> {
|
||||
get("/success", (req, res) -> {
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
@@ -156,6 +81,51 @@ public class Main {
|
||||
});
|
||||
});
|
||||
|
||||
// APIs
|
||||
|
||||
post("/api/getPaymentMethods", (req, res) -> {
|
||||
PaymentMethodsResponse response = checkoutService.getPaymentMethods();
|
||||
return gson.toJson(response);
|
||||
});
|
||||
|
||||
post("/api/initiatePayment", (req, res) -> {
|
||||
System.out.println("Response received from client:\n" + req.body());
|
||||
PaymentsRequest request = gson.fromJson(req.body(), PaymentsRequest.class);
|
||||
PaymentsResponse response = checkoutService.makePayment(request);
|
||||
return gson.toJson(response);
|
||||
|
||||
});
|
||||
|
||||
post("/api/submitAdditionalDetails", (req, res) -> {
|
||||
PaymentsDetailsRequest details = gson.fromJson(req.body(), PaymentsDetailsRequest.class);
|
||||
PaymentsDetailsResponse paymentsDetails = checkoutService.submitPaymentsDetails(details);
|
||||
return gson.toJson(paymentsDetails);
|
||||
});
|
||||
|
||||
get("/api/handleShopperRedirect", (req, res) -> {
|
||||
System.out.println("GET redirect handler");
|
||||
|
||||
PaymentsDetailsRequest detailsRequest = new PaymentsDetailsRequest();
|
||||
QueryParamsMap queryMap = req.queryMap();
|
||||
|
||||
if (queryMap.hasKey("redirectResult")) {
|
||||
detailsRequest.setDetails(Collections.singletonMap("redirectResult", queryMap.value("redirectResult")));
|
||||
|
||||
} else if (queryMap.hasKey("payload")) {
|
||||
detailsRequest.setDetails(Collections.singletonMap("payload", queryMap.value("payload")));
|
||||
}
|
||||
|
||||
PaymentsDetailsResponse response = checkoutService.submitPaymentsDetails(detailsRequest);
|
||||
PaymentsResponse.ResultCodeEnum result = response.getResultCode();
|
||||
|
||||
setRedirect(result, res);
|
||||
return res;
|
||||
});
|
||||
|
||||
System.out.println("\n----------------------------------------------------------\n\t" +
|
||||
"Application is running! Access URLs:\n\t" +
|
||||
"Local: \t\thttp://localhost:8080\n\t" +
|
||||
"\n----------------------------------------------------------");
|
||||
}
|
||||
|
||||
private static void setRedirect(PaymentsResponse.ResultCodeEnum result, Response res) {
|
||||
@@ -172,18 +142,15 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
private static void readConfigFile() {
|
||||
private static Properties readConfigFile() {
|
||||
|
||||
Properties prop = new Properties();
|
||||
|
||||
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(configFile));) {
|
||||
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(CONFIG_FILE))) {
|
||||
prop.load(in);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
merchantAccount = prop.getProperty("merchantAccount");
|
||||
apiKey = prop.getProperty("apiKey");
|
||||
clientKey = prop.getProperty("clientKey");
|
||||
return prop;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,46 @@
|
||||
package model;
|
||||
package checkout;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
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.model.checkout.*;
|
||||
import com.adyen.service.Checkout;
|
||||
import com.adyen.service.exception.ApiException;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import controller.FrontendParser;
|
||||
import controller.Main;
|
||||
public class CheckoutService {
|
||||
|
||||
public class Payments {
|
||||
public static PaymentsResponse makePayment(PaymentsRequest paymentsRequest) throws IOException, ApiException {
|
||||
private final Checkout checkout;
|
||||
private final String merchantAccount;
|
||||
|
||||
public CheckoutService(final Properties prop) {
|
||||
merchantAccount = prop.getProperty("merchantAccount");
|
||||
checkout = new Checkout(new Client(prop.getProperty("apiKey"), Environment.TEST));
|
||||
}
|
||||
|
||||
public PaymentMethodsResponse getPaymentMethods() throws IOException, ApiException {
|
||||
PaymentMethodsRequest paymentMethodsRequest = new PaymentMethodsRequest();
|
||||
paymentMethodsRequest.setMerchantAccount(merchantAccount);
|
||||
|
||||
paymentMethodsRequest.setChannel(PaymentMethodsRequest.ChannelEnum.WEB);
|
||||
paymentMethodsRequest.setShopperReference("SparkJava Checkout Shopper");
|
||||
System.out.println("/paymentMethods context:\n" + paymentMethodsRequest.toString());
|
||||
|
||||
PaymentMethodsResponse response = checkout.paymentMethods(paymentMethodsRequest);
|
||||
System.out.println("/paymentMethods response:\n" + response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public PaymentsResponse makePayment(PaymentsRequest paymentsRequest) throws IOException, ApiException {
|
||||
String type = paymentsRequest.getPaymentMethod().getType();
|
||||
|
||||
setAmount(paymentsRequest, type);
|
||||
paymentsRequest.setAmount(getAmount(type));
|
||||
paymentsRequest.setChannel(PaymentsRequest.ChannelEnum.WEB);
|
||||
paymentsRequest.setMerchantAccount(Main.merchantAccount);
|
||||
paymentsRequest.setMerchantAccount(merchantAccount);
|
||||
|
||||
String orderRef = UUID.randomUUID().toString();
|
||||
paymentsRequest.setReference(orderRef);
|
||||
@@ -56,21 +77,21 @@ public class Payments {
|
||||
|
||||
System.out.println("/payments request:\n" + paymentsRequest.toString());
|
||||
|
||||
PaymentsResponse response = Main.checkout.payments(paymentsRequest);
|
||||
PaymentsResponse formattedResponse = FrontendParser.formatResponseForFrontend(response);
|
||||
PaymentsResponse response = checkout.payments(paymentsRequest);
|
||||
|
||||
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" + response);
|
||||
return response;
|
||||
}
|
||||
|
||||
System.out.println("/payments response:\n" + formattedResponse);
|
||||
return formattedResponse;
|
||||
public PaymentsDetailsResponse submitPaymentsDetails(PaymentsDetailsRequest paymentsDetailsRequest) throws IOException, ApiException {
|
||||
System.out.println("/paymentsDetails request:" + paymentsDetailsRequest.toString());
|
||||
PaymentsDetailsResponse paymentsDetailsResponse = checkout.paymentsDetails(paymentsDetailsRequest);
|
||||
System.out.println("paymentsDetails response:\n" + paymentsDetailsResponse.toString());
|
||||
return paymentsDetailsResponse;
|
||||
}
|
||||
|
||||
|
||||
private static void setAmount(PaymentsRequest paymentsRequest, String type) {
|
||||
Amount amount = new Amount();
|
||||
private Amount getAmount(String type) {
|
||||
|
||||
String currency;
|
||||
|
||||
@@ -91,10 +112,11 @@ public class Payments {
|
||||
default:
|
||||
currency = "EUR";
|
||||
}
|
||||
Amount amount = new Amount();
|
||||
|
||||
amount.setCurrency(currency);
|
||||
amount.setValue(1000L);
|
||||
paymentsRequest.setAmount(amount);
|
||||
return amount;
|
||||
}
|
||||
|
||||
private static void addLineItems(PaymentsRequest paymentsRequest) {
|
||||
@@ -1,27 +0,0 @@
|
||||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.adyen.model.checkout.CheckoutPaymentsAction;
|
||||
import com.adyen.model.checkout.PaymentsResponse;
|
||||
|
||||
public class FrontendParser {
|
||||
|
||||
// Format response being passed back to frontend. Only leave resultCode and action. Don't need to pass back
|
||||
// The rest of the information
|
||||
public static PaymentsResponse formatResponseForFrontend(PaymentsResponse unformattedResponse) throws IOException {
|
||||
|
||||
PaymentsResponse.ResultCodeEnum resultCode = unformattedResponse.getResultCode();
|
||||
if (resultCode != null) {
|
||||
PaymentsResponse newPaymentsResponse = new PaymentsResponse();
|
||||
newPaymentsResponse.setResultCode(resultCode);
|
||||
|
||||
CheckoutPaymentsAction action = unformattedResponse.getAction();
|
||||
if (action != null) {
|
||||
newPaymentsResponse.setAction(action);
|
||||
}
|
||||
return newPaymentsResponse;
|
||||
} else {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package model;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.adyen.model.checkout.PaymentMethodsRequest;
|
||||
import com.adyen.model.checkout.PaymentMethodsResponse;
|
||||
import com.adyen.service.exception.ApiException;
|
||||
|
||||
import controller.Main;
|
||||
|
||||
public class PaymentMethods {
|
||||
|
||||
public static PaymentMethodsResponse getPaymentMethods(String type) throws IOException, ApiException {
|
||||
PaymentMethodsRequest paymentMethodsRequest = new PaymentMethodsRequest();
|
||||
paymentMethodsRequest.setMerchantAccount(Main.merchantAccount);
|
||||
|
||||
paymentMethodsRequest.setChannel(PaymentMethodsRequest.ChannelEnum.WEB);
|
||||
paymentMethodsRequest.setShopperReference("SparkJava Checkout Shopper");
|
||||
System.out.println("/paymentMethods context:\n" + paymentMethodsRequest.toString());
|
||||
|
||||
PaymentMethodsResponse response = Main.checkout.paymentMethods(paymentMethodsRequest);
|
||||
System.out.println("/paymentMethods response:\n" + response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package model;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.adyen.model.checkout.PaymentsDetailsRequest;
|
||||
import com.adyen.model.checkout.PaymentsResponse;
|
||||
import com.adyen.service.exception.ApiException;
|
||||
|
||||
import controller.Main;
|
||||
|
||||
public class PaymentsDetails {
|
||||
|
||||
public static PaymentsResponse getPaymentsDetails(PaymentsDetailsRequest paymentsDetailsRequest) throws IOException, ApiException {
|
||||
System.out.println("/paymentsDetails request:" + paymentsDetailsRequest.toString());
|
||||
PaymentsResponse paymentsDetailsResponse = null;
|
||||
paymentsDetailsResponse = Main.checkout.paymentsDetails(paymentsDetailsRequest);
|
||||
System.out.println("paymentsDetails response:\n" + paymentsDetailsResponse.toString());
|
||||
return paymentsDetailsResponse;
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,10 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<link rel="stylesheet" href="https://checkoutshopper-test.adyen.com/checkoutshopper/sdk/3.20.0/adyen.css"
|
||||
integrity="sha384-jXPa2gofpP6MkO/994dAS/W4pn9MgcK9IOebe5jKpnRmzMAvnxBspvqQVpU3jjIH"
|
||||
crossorigin="anonymous">
|
||||
<link rel="stylesheet"
|
||||
href="https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/4.1.0/adyen.css"
|
||||
integrity="sha384-+CPzBNZVkBXu4uXDECnVuVQ24Kl8vWrR61UzuuuUj5IBEP//BQ0G0KDNfz2iPcvJ"
|
||||
crossorigin="anonymous">
|
||||
|
||||
<div id="payment-page">
|
||||
<div class="container">
|
||||
@@ -19,10 +20,9 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<script src="https://checkoutshopper-test.adyen.com/checkoutshopper/sdk/3.20.0/adyen.js"
|
||||
integrity="sha384-UNqekQqUbhwebnism+MhmqRqTuLtMCz7O/dMCjBuMZEoj61mhjM84R+jZDB2BIEg"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/4.1.0/adyen.js"
|
||||
integrity="sha384-3tEepwhhMcyxgIbL3HBe3I59BpSMNyKoNrbKWARYH1tJ7K7K6NdTDqOltKlwiVsH"
|
||||
crossorigin="anonymous"></script>
|
||||
|
||||
<script id="client-key" type="application/json">{{ clientKey }}</script>
|
||||
<script id="integration-type" type="application/json">{{ integrationType }}</script>
|
||||
|
||||
Reference in New Issue
Block a user