Skip to content

Commit 986380c

Browse files
committed
Java SpringBoot example that uses Server Side Java SDK
1 parent 9633c8f commit 986380c

14 files changed

Lines changed: 654 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Covers all spring-boot ignore files
2+
# Reference: https://github.com/spring-projects/spring-boot/blob/master/.gitignore
3+
4+
.gradle
5+
*.sw?
6+
.#*
7+
*#
8+
*~
9+
/build
10+
/code
11+
.classpath
12+
.project
13+
.settings
14+
.metadata
15+
.factorypath
16+
.recommenders
17+
bin
18+
build
19+
lib/
20+
target
21+
.factorypath
22+
.springBeans
23+
interpolated*.xml
24+
dependency-reduced-pom.xml
25+
build.log
26+
_site/
27+
.*.md.html
28+
manifest.yml
29+
MANIFEST.MF
30+
settings.xml
31+
activemq-data
32+
overridedb.*
33+
*.iml
34+
*.ipr
35+
*.iws
36+
.idea
37+
*.jar
38+
.DS_Store
39+
.factorypath
40+
dump.rdb
41+
42+
nbactions.xml
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Flex Java Example
2+
3+
A minimalist java/spring-boot example integration using Flex-API tokenization.
4+
5+
## Prerequisites
6+
7+
- [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
8+
- [JCE unlimited policy files](http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html)
9+
- [Maven](https://maven.apache.org/install.html)
10+
11+
## Setup Instructions
12+
13+
1. Modify `./src/main/resources/application.properties` with the credentials created through [VISA Developer Portal](https://developer.visa.com/).
14+
15+
```properties
16+
vdp.api-key=_YOUR_APPLICATION_SPECIFIC_API_KEY_
17+
vdp.shared-secret=_YOUR_APPLICATION_SPECIFIC_SHARED_SECRET_
18+
```
19+
20+
2. Build and run the application using maven
21+
```bash
22+
mvn clean spring-boot:run
23+
```
24+
This will serve the application from [https://localhost:8443](https://localhost:8443).
25+
26+
## Tips
27+
28+
- If you are having issues, checkout the full [FLEX documentation](https://developer.visa.com/products/cybersource/reference#cybersource__cybersource_flex_api).
29+
30+
- To change the port the application is served on, update `server.port=8443` in `application.properties`, replacing `8443` with your desired port number.
31+
32+
- If the application throws `java.security.InvalidKeyException: Illegal key size` you have probably not installed the [JCE unlimited policy files](http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html).
33+
34+
## Browser Support
35+
36+
- Chrome 37+
37+
- Firefox 34+
38+
- Edge 12+
39+
- Opera 24+
40+
41+
*NB: IE11 and Safari support could be achieved through the use of polyfills for promises and Web Crypto API such as [webcrypto-shim](https://github.com/vibornoff/webcrypto-shim) and [promiz](https://github.com/Zolmeister/promiz). However, these are not included in the examples.*
42+
43+
## Disclaimer
44+
45+
This respository is provided as a learning aid for merchants wishing to integrate with the Flex API. The code samples are not production ready and are intended for illustrative purposes only. As such, any use of these code samples in a production setting is strongly discouraged. Any usage of these code samples must comply with the license agreement as defined in `LICENSE.md` at the root level of this repository.

java/flex-sdk-spring-boot/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.cybersource.flex.application</groupId>
8+
<artifactId>spring-boot</artifactId>
9+
<version>1.0-BETA</version> <!-- This example relates to Flex-API BETA v1 -->
10+
<packaging>jar</packaging>
11+
12+
<name>Flex-API Spring-Boot Demo</name>
13+
<description>A mocked merchant checkout and payment pages using Flex-API tokenization</description>
14+
<url>https://github.com/CyberSource/cybersource-flex-samples/spring-boot</url>
15+
16+
<parent> <!-- infer reasonable defaults for dependency versions -->
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-parent</artifactId>
19+
<version>1.2.5.RELEASE</version>
20+
<relativePath/> <!-- lookup parent from repository -->
21+
</parent>
22+
23+
<properties>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<build>
29+
<plugins>
30+
<plugin> <!-- To facilitate jar/war packaging -->
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-maven-plugin</artifactId>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
<dependencies>
38+
<!-- Spring Boot dependencies: Tomcat and Thymeleaf -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-web</artifactId>
50+
</dependency>
51+
52+
<!-- Flex-API server side Java SDK -->
53+
<dependency>
54+
<groupId>com.cybersource</groupId>
55+
<artifactId>flex-server-sdk</artifactId>
56+
<version>0.1.0</version>
57+
</dependency>
58+
</dependencies>
59+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) 2017 by CyberSource
3+
* Governing licence: https://github.com/CyberSource/cybersource-flex-samples/blob/master/LICENSE.md
4+
*/
5+
package com.cybersource.flex.application;
6+
7+
import com.cybersource.flex.sdk.FlexService;
8+
import com.cybersource.flex.sdk.exception.FlexException;
9+
import com.cybersource.flex.sdk.model.FlexPublicKey;
10+
import java.util.Map;
11+
import javax.servlet.http.HttpSession;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.stereotype.Controller;
14+
import org.springframework.ui.Model;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestMethod;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
19+
@Controller
20+
public class CheckoutController {
21+
22+
@Autowired
23+
private FlexService flexService;
24+
25+
@RequestMapping("/")
26+
String redirect() {
27+
return "redirect:checkout";
28+
}
29+
30+
@RequestMapping("/checkout")
31+
String checkout(final HttpSession session, final Model model) throws FlexException {
32+
// retrieve one time use public RSA key from Flex to facilitate PAN encryption
33+
final FlexPublicKey key = flexService.createKey();
34+
session.setAttribute("flexPublicKey", key);
35+
36+
// Add JSON Web Keystore to the view model and return rendered "checkout" page
37+
model.addAttribute("jwk", key.getJwk());
38+
return "checkout";
39+
}
40+
41+
@RequestMapping(value = "/receipt", method = RequestMethod.POST)
42+
String receipt(@RequestParam final Map<String, Object> postParams, final HttpSession session, final Model model) throws FlexException {
43+
// Read in the public key to be used and remove it from the session
44+
final FlexPublicKey key = (FlexPublicKey) session.getAttribute("flexPublicKey");
45+
session.removeAttribute("flexPublicKey"); // no longer needed
46+
47+
// verify the token signiture using SDK
48+
postParams.put("verifyResult", flexService.verify(key, postParams));
49+
50+
/**
51+
*
52+
* The payment may now be completed using the received & validated
53+
* token.
54+
*
55+
* For demonstration purposes, all post parameters are added to the view
56+
* model to display data received from cardholder's browser.
57+
*/
58+
model.addAttribute("postParams", postParams);
59+
return "receipt";
60+
}
61+
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2016 by CyberSource
3+
* Governing licence: https://github.com/CyberSource/cybersource-flex-samples/blob/master/LICENSE.md
4+
*/
5+
package com.cybersource.flex.application;
6+
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
@SpringBootApplication
11+
public class EntryPoint {
12+
13+
public static void main(String... args) throws Exception {
14+
SpringApplication.run(EntryPoint.class, args);
15+
}
16+
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2017 by CyberSource
3+
* Governing licence: https://github.com/CyberSource/cybersource-flex-samples/blob/master/LICENSE.md
4+
*/
5+
package com.cybersource.flex.application;
6+
7+
import com.cybersource.flex.sdk.FlexService;
8+
import com.cybersource.flex.sdk.authentication.VDPCredentials;
9+
import com.cybersource.flex.sdk.impl.FlexKeyServiceImpl;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
14+
@Configuration
15+
public class FlexConfiguration {
16+
17+
@Value("${vdp.api-key}")
18+
private String apiKey;
19+
@Value("${vdp.shared-secret}")
20+
private char[] sharedSecret;
21+
22+
@Bean
23+
public FlexService flexService() {
24+
VDPCredentials vdpCredentials = new VDPCredentials(VDPCredentials.Environment.SANDBOX, apiKey, sharedSecret);
25+
return new FlexKeyServiceImpl(vdpCredentials);
26+
}
27+
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.p12
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Folder contents
2+
3+
| Folder / Filename | Purpose |
4+
|------------------------|----------------|
5+
| /public | Assets placed here are served publicly from the root of the webserver. In our example this contains all the scripts and css used. |
6+
| /templates | All [Thymeleaf templates](http://www.thymeleaf.org/) used by the application in rendering html pages. |
7+
| application.properties | The Spring-Boot application configuration. In our example this is used to enable HTTPS via embedded Tomcat as the WebCryptoAPI requires pages to be served via secure protocol only. Additionally, this is where all merchant credentials and configuration is stored (as per setup instructions step 1). |
8+
| embedded-tomcat.jks | Self signed X.509 certificate keystore generated with keytool command. This certificate is used to mock “merchant site” being served over HTTPS. In a real production environment the SSL certificate should not be part of source code and would typically be provisioned directly to production servers. |
9+
| .gitignore | Prevents any accidental commit of p12 keystore to the git repository. |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# VISA Developer Platform credentials - https://developer.visa.com/
2+
#
3+
# This property file is for demonstartion purposes only.
4+
# You MUST take proper precautions to protect your credentials, such as
5+
# NEVER storing your credentials unencrypted with application code.
6+
#
7+
vdp.api-key=_YOUR_APPLICATION_SPECIFIC_API_KEY_
8+
vdp.shared-secret=_YOUR_APPLICATION_SPECIFIC_SHARED_SECRET_
9+
10+
# Embedded Tomcat configuration properties
11+
#
12+
# The properties below enable HTTPS on TCP port 8443.
13+
server.port=8443
14+
# Please note that this example uses self-signed X.509 certificate with CN=localhost.
15+
# Keystore and private key passwords provided below are not encrypted.
16+
# While it is acceptable for demonstration and example purposes,
17+
# it must not be treated as production grade configuration.
18+
server.ssl.key-store=classpath:embedded-tomcat.jks
19+
server.ssl.key-store-password=changeit
20+
server.ssl.key-password=changeit
Binary file not shown.

0 commit comments

Comments
 (0)