Skip to content

Commit 7748cea

Browse files
committed
adding loader runner sample
1 parent db7ed36 commit 7748cea

2 files changed

Lines changed: 327 additions & 81 deletions

File tree

samples/nvp/src/main/java/com/cybersource/sample/RunSample.java

Lines changed: 165 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,45 @@
22

33
import java.io.FileInputStream;
44
import java.io.IOException;
5+
import java.math.BigInteger;
6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
58
import java.util.Arrays;
69
import java.util.Enumeration;
710
import java.util.HashMap;
811
import java.util.Map;
912
import java.util.Iterator;
1013
import java.util.Properties;
14+
import java.util.concurrent.atomic.AtomicLong;
1115

1216
import com.cybersource.ws.client.*;
17+
import org.apache.commons.lang3.StringUtils;
18+
19+
import static com.cybersource.ws.client.Utility.MERCHANT_TRANSACTION_IDENTIFIER;
1320

1421

1522
/**
1623
* Sample class that demonstrates how to call Credit Card Authorization.
1724
*/
1825
public class RunSample {
1926
public enum sample{ auth, capture, emv_auth, credit, auth_reversal, sale;
20-
public static int enum_exist(String str) {
21-
for (sample me : sample.values()) {
22-
if (me.name().equalsIgnoreCase(str))
23-
return me.ordinal();
24-
}
25-
return -1;
27+
public static int enum_exist(String str) {
28+
for (sample me : sample.values()) {
29+
if (me.name().equalsIgnoreCase(str))
30+
return me.ordinal();
31+
}
32+
return -1;
2633
}
2734

28-
}
35+
}
2936
Properties cybsProps = new Properties();
3037
private static final String PROPERTIES="cybs.properties";
3138
static String authDecision;
3239
static String captureDecision;
3340

3441
/**
3542
* Entry point.
36-
*
43+
*
3744
* @param args
3845
* command-line arguments. The name of the property file may be
3946
* passed as a command-line argument. If not passed, it will look
@@ -44,61 +51,61 @@ public static void main(String[] args) {
4451
String argument=args[0];
4552
Properties props = readProperty(argument + ".properties");
4653
@SuppressWarnings("unchecked")
47-
54+
4855
String requestID;
4956
String emvrequestID;
5057

5158
Properties cybProperties = new Properties();
5259
cybProperties = readCybsProperty(PROPERTIES);
5360
int choice =sample.enum_exist(argument);
5461
switch (choice) {
55-
56-
case 0:
57-
requestID = runAuth(cybProperties);
58-
break;
59-
60-
case 1:
61-
requestID = runAuth(cybProperties);
62-
System.out.println("Request ID is " + requestID);
63-
if (!("null".equals(requestID))) {
64-
requestID = runCapture(cybProperties, requestID, argument);
65-
}
66-
break;
67-
68-
case 2:
69-
emvrequestID = runAuthEMV(cybProperties,argument);
70-
break;
71-
72-
case 3:
73-
requestID = runAuth(cybProperties );
74-
System.out.println("Request ID is " + requestID);
75-
if (!("null".equals(requestID))) {
76-
requestID = runCapture(cybProperties, requestID, argument);
77-
if (!("null".equals(requestID)))
78-
runCredit(cybProperties, requestID, argument);
79-
}
80-
break;
8162

82-
case 4:
83-
requestID = runAuth(cybProperties);
84-
if (!("null".equals(requestID))) {
85-
runAuthReversal(cybProperties, requestID, argument);
86-
}
87-
break;
88-
case 5:
89-
runSale(cybProperties);
90-
break;
91-
case -1:
92-
System.out.println(" \t\t Enter the correct Service_name\n\n "+
93-
"\t\t while running the Script enter the service_name \n\n"+
94-
"\t\t for example to run a auth transaction enter the following command: \n"+
95-
"\t\t\t for windows : runSample <service_name> \n "+
96-
"\t\t\t for linux : ./runSample.sh <service_name> \n"+
97-
"\t\t service name argument can be auth,sale,credit,authreversal,capture,emv_auth \n"+
98-
"\t\t NOTE: if no argument is entered the script will terminate the program");
99-
break;
100-
default:
101-
break;
63+
case 0:
64+
requestID = runAuth(cybProperties);
65+
break;
66+
67+
case 1:
68+
requestID = runAuth(cybProperties);
69+
System.out.println("Request ID is " + requestID);
70+
if (!("null".equals(requestID))) {
71+
requestID = runCapture(cybProperties, requestID, argument);
72+
}
73+
break;
74+
75+
case 2:
76+
emvrequestID = runAuthEMV(cybProperties,argument);
77+
break;
78+
79+
case 3:
80+
requestID = runAuth(cybProperties );
81+
System.out.println("Request ID is " + requestID);
82+
if (!("null".equals(requestID))) {
83+
requestID = runCapture(cybProperties, requestID, argument);
84+
if (!("null".equals(requestID)))
85+
runCredit(cybProperties, requestID, argument);
86+
}
87+
break;
88+
89+
case 4:
90+
requestID = runAuth(cybProperties);
91+
if (!("null".equals(requestID))) {
92+
runAuthReversal(cybProperties, requestID, argument);
93+
}
94+
break;
95+
case 5:
96+
runSale(cybProperties);
97+
break;
98+
case -1:
99+
System.out.println(" \t\t Enter the correct Service_name\n\n "+
100+
"\t\t while running the Script enter the service_name \n\n"+
101+
"\t\t for example to run a auth transaction enter the following command: \n"+
102+
"\t\t\t for windows : runSample <service_name> \n "+
103+
"\t\t\t for linux : ./runSample.sh <service_name> \n"+
104+
"\t\t service name argument can be auth,sale,credit,authreversal,capture,emv_auth \n"+
105+
"\t\t NOTE: if no argument is entered the script will terminate the program");
106+
break;
107+
default:
108+
break;
102109

103110
}
104111
}
@@ -113,8 +120,8 @@ public static String runSale(Properties props) {
113120
// run transaction now
114121
Map<String, String> reply = Client.runTransaction(request, props);
115122
displayMap("CREDIT CARD SALE REPLY:", reply);
116-
requestID = (String) reply.get("requestID");
117-
System.out.println("Sale completed " +requestID);
123+
requestID = (String) reply.get("requestID");
124+
System.out.println("Sale completed " +requestID);
118125

119126
} catch (ClientException e) {
120127
System.out.println(e.getMessage());
@@ -132,16 +139,18 @@ public static String runSale(Properties props) {
132139
}
133140
/**
134141
* Runs Credit Card Authorization.
135-
*
142+
*
136143
* @param props
137144
* Properties object.
138145
* @return the requestID.
139146
*/
140-
147+
141148
public static String runAuth(Properties props) {
149+
long startTime = System.currentTimeMillis();
142150
String requestID = null;
143151
Properties authProps = new Properties();
144152
authProps = readProperty("auth.properties");
153+
authProps.put(MERCHANT_TRANSACTION_IDENTIFIER,UniquIDGenerator.getInstance().createUniqueID());
145154
HashMap<String, String> request = new HashMap<String, String>(
146155
(Map) authProps);
147156
try {
@@ -151,29 +160,29 @@ public static String runAuth(Properties props) {
151160
displayMap("CREDIT CARD AUTHORIZATION REPLY:", reply);
152161
// if the authorization was successful, obtain the request id
153162
// for the follow-on capture later.
154-
String decision = (String) reply.get("decision");
155-
if ("ACCEPT".equalsIgnoreCase(decision)) {
156-
requestID = (String) reply.get("requestID");
163+
String decision = reply.get("decision");
164+
if (StringUtils.isNotBlank(decision)) {
165+
requestID = reply.get("requestID");
157166
}
158-
159-
} catch (ClientException e) {
160-
System.out.println(e.getMessage());
161-
if (e.isCritical()) {
162-
handleCriticalException(e, request);
167+
if (requestID == null) {
168+
System.out.println("Request id null >>" + reply);
163169
}
170+
} catch (ClientException e) {
171+
String stackTrace = Utility.getStackTrace(e.getInnerException() != null? e.getInnerException(): e);
172+
System.out.println("client exception >>> " + stackTrace);
164173
} catch (FaultException e) {
165-
System.out.println(e.getMessage());
166-
if (e.isCritical()) {
167-
handleCriticalException(e, request);
168-
}
174+
System.out.println("fault exception >>>" + e);
175+
} catch (Exception e) {
176+
System.out.println("exception >>>" + e.getMessage());
169177
}
170178

179+
System.out.println("Time taken for request id : " + requestID + " " +(System.currentTimeMillis()-startTime));
171180
return (requestID);
172181
}
173182

174183
/**
175184
* Runs Credit Card Capture.
176-
*
185+
*
177186
* @param props
178187
* Properties object.
179188
* @param authRequestID
@@ -184,15 +193,15 @@ public static String runCapture(Properties props, String authRequestID,String pr
184193
captureProps = readProperty("capture.properties");
185194
HashMap<String, String> request = new HashMap<String, String>(
186195
(Map) captureProps);
187-
196+
188197
request.put("ccCaptureService_authRequestID", authRequestID);
189198
try {
190199
displayMap("FOLLOW-ON CAPTURE REQUEST:", request);
191200
// run transaction now
192201
Map<String, String> reply = Client.runTransaction(request, props);
193-
202+
194203
displayMap("FOLLOW-ON CAPTURE REPLY:", reply);
195-
204+
196205
} catch (ClientException e) {
197206
System.out.println(e.getMessage());
198207
if (e.isCritical()) {
@@ -210,7 +219,7 @@ public static String runCapture(Properties props, String authRequestID,String pr
210219
/**
211220
* @param props
212221
* @param authRequestID
213-
* @param request
222+
* @param propFileName
214223
*/
215224
public static void runAuthReversal(Properties props, String authRequestID, String propFileName) {
216225
Properties authReversalProps = new Properties();
@@ -245,8 +254,6 @@ public static String runAuthEMV(Properties props, String propFileName) {
245254
authEMVProps = readProperty(propFileName+".properties");
246255
HashMap<String, String> request = new HashMap<String, String>(
247256
(Map) authEMVProps);
248-
249-
250257
try {
251258
displayMap("CREDIT CARD EMVAUTHORIZATION REQUEST:", request);
252259
// run transaction now
@@ -301,7 +308,7 @@ public static void runCredit(Properties props, String RequestID, String propFile
301308

302309
/**
303310
* Displays the content of the Map object.
304-
*
311+
*
305312
* @param header
306313
* Header text.
307314
* @param map
@@ -353,7 +360,7 @@ private static void handleCriticalException(ClientException e, Map request) {
353360

354361
/**
355362
* See header comment in the other version of handleCriticalException above.
356-
*
363+
*
357364
* @param e
358365
* Critical ClientException object.
359366
* @param request
@@ -393,4 +400,81 @@ public static Properties readCybsProperty(String file) {
393400
return (cybsProps);
394401
}
395402

396-
}
403+
public static class UniquIDGenerator {
404+
private AtomicLong serialNumber = new AtomicLong(1);
405+
private String ipAddress = generateIPAddress();
406+
private static UniquIDGenerator SELF = new UniquIDGenerator();
407+
public static UniquIDGenerator getInstance(){
408+
return SELF;
409+
}
410+
private String generateIPAddress() {
411+
InetAddress addr = null;
412+
for (int tries = 1; ; tries++) {
413+
try {
414+
addr = InetAddress.getLocalHost();
415+
String hostID = getHostID();
416+
if (hostID == null) {
417+
addr = InetAddress.getLocalHost();
418+
}
419+
else {
420+
addr = InetAddress.getByName( hostID );
421+
}
422+
validateIPAddress(addr);
423+
break;
424+
} catch (UnknownHostException e) {
425+
if (tries >= 3) {
426+
break;
427+
} else {
428+
try {
429+
Thread.sleep(500);
430+
} catch (InterruptedException ignored) {
431+
// ignored
432+
}
433+
}
434+
}
435+
}
436+
if(addr == null){
437+
return null;
438+
}
439+
BigInteger ip = new BigInteger(1, addr.getAddress());
440+
// pad the ip address string to 10 characters
441+
String ipString = ip.toString();
442+
if (ipString.length() < 10) {
443+
ipString = "0000000000".substring(ipString.length()) + ipString;
444+
}
445+
// trim leading characters in case it's > 10 digits long
446+
if (ipString.length() > 10) {
447+
ipString = ipString.substring(ipString.length() - 10);
448+
}
449+
return ipString;
450+
}
451+
private String getHostID() {
452+
return System.getProperty("host_id");
453+
}
454+
public String createUniqueID(){
455+
long time = System.nanoTime();
456+
long serial = serialNumber.getAndIncrement();
457+
if(ipAddress == null){
458+
return null;
459+
}
460+
return createUniqueID(ipAddress, time, serial);
461+
}
462+
private String createUniqueID(String ipAddress, long timeInNano, long serial) {
463+
return String.format("%015d%05d%10s",
464+
timeInNano,
465+
serial % 100000,
466+
ipAddress);
467+
}
468+
/**
469+
* Validates the IP address. The only validation currenly
470+
* being made is the check against 127.0.0.1.
471+
**/
472+
private static void validateIPAddress(InetAddress addr)
473+
throws UnknownHostException {
474+
if (addr.equals(InetAddress.getByName("127.0.0.1"))) {
475+
throw new UnknownHostException(
476+
"127.0.0.1 is not allowed. Use a different IP address.");
477+
}
478+
}
479+
}
480+
}

0 commit comments

Comments
 (0)