-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathStandaloneOAuth.java
210 lines (165 loc) · 8.2 KB
/
StandaloneOAuth.java
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
207
208
209
210
package samples.authentication.AuthSampleCode;
import java.lang.invoke.MethodHandles;
import Api.OAuthApi;
import Api.PaymentsApi;
import Data.Configuration;
import Invokers.ApiClient;
import Model.*;
import com.cybersource.authsdk.core.MerchantConfig;
import java.util.Properties;
public class StandaloneOAuth {
private static String responseCode = null;
private static String status = null;
private static String code;
private static String grantType;
private static String refreshToken;
private static String accessToken;
private static Properties merchantProp;
public static MerchantConfig merchantConfig;
public static boolean createUsingAuthCode = true;
public static void WriteLogAudit(int status) {
String filename = MethodHandles.lookup().lookupClass().getSimpleName();
System.out.println("[Sample Code Testing] [" + filename + "] " + status);
}
public static Properties getMerchantDetails() {
Properties props = new Properties();
props.setProperty("runEnvironment", "api-matest.cybersource.com");
// OAuth related properties.
props.setProperty("enableClientCert", "true");
props.setProperty("clientCertDirectory", "src/main/resources");
props.setProperty("clientCertFile", "");
props.setProperty("clientCertPassword", "");
props.setProperty("clientId", "");
props.setProperty("clientSecret", "");
return props;
}
public static void main(String args[]) throws Exception {
AccessTokenResponse result;
if(createUsingAuthCode)
{
// Create Access Token using Auth Code
code = "";
grantType = "authorization_code";
result = postAccessTokenFromAuthCode();
}
else {
// Create Access Token using Refresh Token
grantType = "refresh_token";
refreshToken = "";
result = postAccessTokenFromRefreshToken();
}
if(result != null && responseCode.equals("200")) {
refreshToken = result.getRefreshToken();
accessToken = result.getAccessToken();
// Save accessToken and refreshToken before making API calls
merchantConfig.setAccessToken(accessToken);
merchantConfig.setRefreshToken(refreshToken);
// Set Authentication to OAuth
merchantConfig.setAuthenticationType("OAuth");
//Call Payments SampleCode using OAuth, Set Authentication to OAuth in Sample Code Configuration
SimpleAuthorizationInternet();
}
}
public static AccessTokenResponse postAccessTokenFromAuthCode() {
CreateAccessTokenRequest requestObj = new CreateAccessTokenRequest();
AccessTokenResponse result = null;
try {
merchantProp = getMerchantDetails();
// Set Authentication to MutualAuth to call OAuth API
merchantProp.setProperty("authenticationType","MutualAuth");
ApiClient apiClient = new ApiClient();
merchantConfig = new MerchantConfig(merchantProp);
apiClient.merchantConfig = merchantConfig;
requestObj.code(code);
requestObj.grantType(grantType);
requestObj.clientSecret(merchantConfig.getClientSecret());
requestObj.clientId(merchantConfig.getClientId());
OAuthApi apiInstance = new OAuthApi(apiClient);
result = apiInstance.postAccessTokenFromAuthCode(requestObj);
responseCode = apiClient.responseCode;
status = apiClient.status;
System.out.println("ResponseCode :" + responseCode);
System.out.println("ResponseMessage :" + status);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static AccessTokenResponse postAccessTokenFromRefreshToken() {
CreateAccessTokenRequest requestObj = new CreateAccessTokenRequest();
AccessTokenResponse result = null;
try {
merchantProp = getMerchantDetails();
// Set Authentication to MutualAuth to call OAuth API
merchantProp.setProperty("authenticationType","MutualAuth");
ApiClient apiClient = new ApiClient();
merchantConfig = new MerchantConfig(merchantProp);
apiClient.merchantConfig = merchantConfig;
requestObj.refreshToken(refreshToken);
requestObj.grantType(grantType);
requestObj.clientSecret(merchantConfig.getClientSecret());
requestObj.clientId(merchantConfig.getClientId());
OAuthApi apiInstance = new OAuthApi(apiClient);
result = apiInstance.postAccessTokenFromAuthCode(requestObj);
responseCode = apiClient.responseCode;
status = apiClient.status;
System.out.println("ResponseCode :" + responseCode);
System.out.println("ResponseMessage :" + status);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static PtsV2PaymentsPost201Response SimpleAuthorizationInternet() {
CreatePaymentRequest requestObj = new CreatePaymentRequest();
Ptsv2paymentsClientReferenceInformation clientReferenceInformation = new Ptsv2paymentsClientReferenceInformation();
clientReferenceInformation.code("TC50171_3");
requestObj.clientReferenceInformation(clientReferenceInformation);
Ptsv2paymentsProcessingInformation processingInformation = new Ptsv2paymentsProcessingInformation();
processingInformation.capture(false);
requestObj.processingInformation(processingInformation);
Ptsv2paymentsPaymentInformation paymentInformation = new Ptsv2paymentsPaymentInformation();
Ptsv2paymentsPaymentInformationCard paymentInformationCard = new Ptsv2paymentsPaymentInformationCard();
paymentInformationCard.number("4111111111111111");
paymentInformationCard.expirationMonth("12");
paymentInformationCard.expirationYear("2031");
paymentInformation.card(paymentInformationCard);
requestObj.paymentInformation(paymentInformation);
Ptsv2paymentsOrderInformation orderInformation = new Ptsv2paymentsOrderInformation();
Ptsv2paymentsOrderInformationAmountDetails orderInformationAmountDetails = new Ptsv2paymentsOrderInformationAmountDetails();
orderInformationAmountDetails.totalAmount("102.21");
orderInformationAmountDetails.currency("USD");
orderInformation.amountDetails(orderInformationAmountDetails);
Ptsv2paymentsOrderInformationBillTo orderInformationBillTo = new Ptsv2paymentsOrderInformationBillTo();
orderInformationBillTo.firstName("John");
orderInformationBillTo.lastName("Doe");
orderInformationBillTo.address1("1 Market St");
orderInformationBillTo.locality("san francisco");
orderInformationBillTo.administrativeArea("CA");
orderInformationBillTo.postalCode("94105");
orderInformationBillTo.country("US");
orderInformationBillTo.email("[email protected]");
orderInformationBillTo.phoneNumber("4158880000");
orderInformation.billTo(orderInformationBillTo);
requestObj.orderInformation(orderInformation);
PtsV2PaymentsPost201Response result = null;
try {
ApiClient apiClient = new ApiClient();
apiClient.merchantConfig = merchantConfig;
PaymentsApi apiInstance = new PaymentsApi(apiClient);
result = apiInstance.createPayment(requestObj);
responseCode = apiClient.responseCode;
status = apiClient.status;
System.out.println("ResponseCode :" + responseCode);
System.out.println("ResponseMessage :" + status);
System.out.println(result);
WriteLogAudit(Integer.parseInt(responseCode));
} catch (Exception e) {
e.printStackTrace();
WriteLogAudit(400);
}
return result;
}
}