Snippets
Created by
Ruben Fernandez
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.easypaymentsgateway.exceptions.EncryptionException;
import com.easypaymentsgateway.exceptions.EncryptionException.ErrorCode;
import com.easypaymentsgateway.utils.EncryptionUtils;
public class EPGJavaIntegration {
private static final String ENDPOINT = "http://staging.easypaymentgateway.com/EPGCheckout/rest/online/tokenize";
private static final String MID = "XXXXX";
private static final String PARAMETERS = "YYYY";
private static final String MERCHANT_PASSWORD = ""; // Merchant password or MD5 merchant password
private static final String MD5_MERCHANT_PASSWORD = ""; // Merchant password or MD5 merchant password
private static final String AES = "AES";
private static final String MD5 = "MD5";
private static Logger logger = LoggerFactory.getLogger(API.class);
public static void main(String[] args) {
logger.info("URL: " + new API(MID,
PARAMETERS,
MERCHANT_PASSWORD,MD5_MERCHANT_PASSWORD).tokenizer());
}
private String mid;
private String params;
private String passwordkey;
private String mD5passwordkey;
public EPGJavaIntegration(String mid, String params, String passwordkey, String mD5passwordkey) {
this.mid = mid;
this.params = params;
this.passwordkey = passwordkey;
this.mD5passwordkey = mD5passwordkey;
}
public String tokenizer(){
String urlDestination = "";
try {
urlDestination = retrieveUrlToRedirect(
encrypt(params, passwordkey==""?mD5passwordkey:hashMessage(passwordkey, MD5)),
EncryptionUtils.hashMessage(params, EncryptionUtils.SHA256),
mid);
} catch (IOException e) {
logger.error(
"Error when trying to encrypt params" + e.getMessage(), e);
}
return urlDestination;
}
/**
* Encrypts using AES algorithm
*
* @param input
* @param key
* @return
*/
public String encrypt(String input, String key) {
byte[] crypted = null;
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), AES);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
} catch (Exception e) {
logger.error(e.toString());
}
return new String(Base64.encodeBase64(crypted));
}
/**
* Decrypts using AES algorithm
*
* @param input
* @param key
* @return
*/
public String decrypt(String input, String key) {
byte[] output = null;
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), AES);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal(Base64.decodeBase64(input));
} catch (Exception e) {
logger.error(e.toString());
}
return new String(output);
}
/**
* method used to retrieve the session token supplied byEPG
*/
private String retrieveUrlToRedirect(String encryptedParams,
String integrityCheck, String mid) throws IOException {
String urlToRedirect;
String url = ENDPOINT;
// Build request body
params = "encrypted=" + URLEncoder.encode(encryptedParams, "UTF-8")
+ "&integrityCheck="
+ URLEncoder.encode(integrityCheck, "UTF-8") + "&merchantId="
+ URLEncoder.encode(mid, "UTF-8");
urlToRedirect = httpCall(url);
return urlToRedirect;
}
/**
* method used to make a http call
*/
private String httpCall(String destination) throws IOException {
// Create connection
URL url = new URL(destination);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
// Create I/O streams
OutputStreamWriter outStream = new OutputStreamWriter(
urlConnection.getOutputStream());
// Send request
outStream.write(params);
outStream.flush();
// Get Response
BufferedReader rd = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String urlToRedirect = rd.readLine();
outStream.close();
return urlToRedirect;
}
public String hashMessage(String message, String algorithm) {
logger.debug("init - hashMessage");
byte[] digest = null;
byte[] buffer = message.getBytes();
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.reset();
messageDigest.update(buffer);
digest = messageDigest.digest();
} catch (NoSuchAlgorithmException e) {
logger.error("Internal error found");
throw new EncryptionException(
ErrorCode.UNABLE_TO_FIND_HASH_ALGORITHM,
"Internal error found");
}
logger.debug("end - hashMessage");
return toHexadecimal(digest);
}
private String toHexadecimal(byte[] digest) {
logger.debug("init - toHexadecimal");
String hash = "";
for (byte aux : digest) {
int b = aux & 0xff;
if (Integer.toHexString(b).length() == 1)
hash += "0";
hash += Integer.toHexString(b);
}
logger.debug("end - toHexadecimal");
return hash;
}
}
|
Comments (2)
You can clone a snippet to your computer for local editing. Learn more.
This is useful, but may I suggest these improvements:
It isn't a big deal to correct these myself, but it would be nice if this example could have these tweaks to make it working code.
Hi Daniel,
Firstly thanks for your message. We will note internally and we will update the snippets with the details that your mentioned. We created this link internally but at the end we shared with some clients.
Regards, Ruben