Skip to content

Commit daa1497

Browse files
committed
Adding JSP Flex microform example
1 parent 00c29e6 commit daa1497

9 files changed

Lines changed: 484 additions & 0 deletions

File tree

java8/jsp-microform/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# JSP (Tomcat) example using hosted Flex microform
2+
3+
A minimalist Java JSP example integration using Flex-API tokenization and Flex microform embedded card capture.
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+
- [Tomcat 8 Web Server](http://tomcat.apache.org)
11+
12+
## Setup Instructions
13+
14+
1. Modify `./src/main/webapp/credentials.properties` with the Cybersource Gate Keeper credentials created through [EBC Portal](https://ebc2.cybersource.com/).
15+
16+
```
17+
merchantId=YOUR MERCHANT ID
18+
keyId=YOUR KEY ID
19+
sharedSecret=YOUR SHARED SECRET
20+
```
21+
22+
2. Build and run the application using maven
23+
```bash
24+
mvn clean install
25+
```
26+
27+
This will produce `.war` file that can be deployed to a Tomcat server instance.
28+
29+
## Tips
30+
31+
- If you are having issues, checkout the full [FLEX documentation]( REPLACE WITH URL ).
32+
33+
- 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).

java8/jsp-microform/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.examples.flex</groupId>
8+
<artifactId>jsp-microform</artifactId>
9+
<version>1.0</version> <!-- This example relates to Flex-API BETA v1 -->
10+
<packaging>war</packaging>
11+
12+
<name>Flex microform JSP Demo</name>
13+
<description>A mocked merchant checkout and payment pages using Flex-API and microform tokenization</description>
14+
<url>https://github.com/CyberSource/cybersource-flex-samples/java/jsp-microform</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-war-plugin</artifactId>
25+
<version>3.2.0</version>
26+
</plugin>
27+
</plugins>
28+
</build>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>javax.servlet-api</artifactId>
34+
<version>3.1.0</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.servlet.jsp</groupId>
39+
<artifactId>javax.servlet.jsp-api</artifactId>
40+
<version>2.3.1</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>com.cybersource</groupId>
46+
<artifactId>flex-server-sdk</artifactId>
47+
<version>0.3.0</version>
48+
</dependency>
49+
</dependencies>
50+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.example;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.io.Reader;
11+
import java.util.HashMap;
12+
13+
public class CharArrayProperties extends HashMap<String, char[]> {
14+
15+
public CharArrayProperties(InputStream inputStream) throws IOException {
16+
Reader reader = null;
17+
try {
18+
reader = new InputStreamReader(inputStream);
19+
20+
StringBuilder key = new StringBuilder();
21+
StringBuilder value = new StringBuilder();
22+
boolean keyRead = false;
23+
24+
boolean eof;
25+
int r;
26+
while (true) {
27+
eof = (r = reader.read()) < 0;
28+
char ch = eof ? '\0' : (char) r;
29+
if (ch == '=') {
30+
if (!keyRead) {
31+
keyRead = true;
32+
} else {
33+
value.append(ch);
34+
}
35+
} else if (ch == '\r') {
36+
continue;
37+
} else if (ch == '\n' || eof) {
38+
final int valueLength = value.length();
39+
char[] valueChars = new char[valueLength];
40+
value.getChars(0, valueLength, valueChars, 0);
41+
42+
put(key.toString(), valueChars);
43+
44+
key.setLength(0);
45+
value.setLength(0);
46+
keyRead = false;
47+
48+
if (eof) {
49+
break;
50+
}
51+
} else if (keyRead) {
52+
value.append(ch);
53+
} else {
54+
key.append(ch);
55+
}
56+
}
57+
} finally {
58+
if (reader != null) {
59+
try {
60+
reader.close();
61+
} catch (IOException e) {
62+
}
63+
}
64+
}
65+
}
66+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.example;
6+
7+
import com.cybersource.flex.sdk.FlexService;
8+
import com.cybersource.flex.sdk.FlexServiceFactory;
9+
import com.cybersource.flex.sdk.authentication.CGKCredentials;
10+
import com.cybersource.flex.sdk.exception.FlexException;
11+
import com.cybersource.flex.sdk.model.EncryptionType;
12+
import com.cybersource.flex.sdk.model.FlexPublicKey;
13+
import com.cybersource.flex.sdk.model.KeysRequestParameters;
14+
import com.cybersource.flex.sdk.repackaged.JSONObject;
15+
import javax.servlet.http.HttpSession;
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
19+
public class FlexKeyProvider {
20+
21+
private final FlexService flexService;
22+
23+
FlexKeyProvider(InputStream resource) {
24+
MerchantCredentials merchantCredentials = null;
25+
try {
26+
merchantCredentials = new MerchantCredentials(resource);
27+
28+
CGKCredentials credentials = new CGKCredentials();
29+
credentials.setEnvironment(CGKCredentials.Environment.CAS);
30+
credentials.setMid(merchantCredentials.getMerchantId());
31+
credentials.setKeyId(merchantCredentials.getKeyId());
32+
credentials.setSharedSecret(merchantCredentials.getSharedSecret());
33+
34+
flexService = FlexServiceFactory.createInstance(credentials);
35+
} catch (FlexException fe) {
36+
throw new RuntimeException(fe);
37+
} catch (IOException e) {
38+
throw new RuntimeException(e);
39+
} finally {
40+
if (merchantCredentials != null) {
41+
merchantCredentials.destroy();
42+
}
43+
}
44+
}
45+
46+
public String bindFlexKeyToSession(HttpSession session) throws FlexException {
47+
KeysRequestParameters parameters = new KeysRequestParameters(EncryptionType.RsaOaep256, "http://localhost:8080");
48+
FlexPublicKey flexPublicKey = flexService.createKey(parameters);
49+
session.setAttribute(FlexPublicKey.class.getName(), flexPublicKey);
50+
return new JSONObject(flexPublicKey.getJwk()).toString();
51+
}
52+
53+
public boolean verifyTokenResponse(HttpSession session, String flexResponse) throws FlexException {
54+
FlexPublicKey flexPublicKey = (FlexPublicKey) session.getAttribute(FlexPublicKey.class.getName());
55+
return flexService.verify(flexPublicKey, flexResponse);
56+
}
57+
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.example;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import javax.servlet.ServletContextEvent;
10+
import javax.servlet.ServletContextListener;
11+
import javax.servlet.annotation.WebListener;
12+
13+
@WebListener
14+
public class FlexServletContextListener implements ServletContextListener {
15+
16+
public void contextInitialized(ServletContextEvent sce) {
17+
InputStream configurationProperties = null;
18+
try {
19+
configurationProperties = sce.getServletContext().getResourceAsStream("/WEB-INF/credentials.properties");
20+
FlexKeyProvider fkp = new FlexKeyProvider(configurationProperties);
21+
sce.getServletContext().setAttribute(FlexKeyProvider.class.getName(), fkp);
22+
} finally {
23+
if (configurationProperties != null) {
24+
try {
25+
configurationProperties.close();
26+
} catch (IOException e) {
27+
}
28+
}
29+
}
30+
}
31+
32+
public void contextDestroyed(ServletContextEvent sce) {
33+
}
34+
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.example;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.Arrays;
10+
import java.util.Map;
11+
12+
public class MerchantCredentials {
13+
14+
private final Map<String, char[]> properties;
15+
16+
public MerchantCredentials(InputStream resource) throws IOException {
17+
properties = new CharArrayProperties(resource);
18+
}
19+
20+
public String getMerchantId() {
21+
char[] merchantId = properties.get("merchantId");
22+
return (merchantId != null) ? new String(merchantId) : null;
23+
}
24+
25+
public String getKeyId() {
26+
char[] keyId = properties.get("keyId");
27+
return (keyId != null) ? new String(keyId) : null;
28+
}
29+
30+
public char[] getSharedSecret() {
31+
return properties.get("sharedSecret");
32+
}
33+
34+
public void destroy() {
35+
for (char[] value : properties.values()) {
36+
Arrays.fill(value, '\0');
37+
}
38+
}
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
merchantId=YOUR MERCHANT ID
2+
keyId=YOUR KEY ID
3+
sharedSecret=YOUR SHARED SECRET

0 commit comments

Comments
 (0)