Skip to content

Commit b6b0ec5

Browse files
committed
+ Added functionality to handle ErrorResponse
1 parent 79f28c7 commit b6b0ec5

9 files changed

+205
-149
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<properties>
4-
<sdkVersion>1.9.7</sdkVersion>
4+
<sdkVersion>1.9.8-SNAPSHOT</sdkVersion>
55
</properties>
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>net.authorize.sample</groupId>
@@ -14,7 +14,7 @@
1414
<dependency>
1515
<groupId>net.authorize</groupId>
1616
<artifactId>anet-java-sdk</artifactId>
17-
<version>1.9.7</version>
17+
<version>1.9.8-SNAPSHOT</version>
1818
</dependency>
1919
<dependency>
2020
<groupId>junit</groupId>

src/main/java/net/authorize/sample/CustomerProfiles/CreateCustomerPaymentProfile.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
import net.authorize.api.contract.v1.MerchantAuthenticationType;
77
import net.authorize.api.controller.base.ApiOperationBase;
88

9-
import net.authorize.Transaction;
10-
import net.authorize.api.controller.CreateCustomerProfileController;
11-
import net.authorize.api.controller.CreateCustomerProfileFromTransactionController;
129
import net.authorize.api.controller.CreateCustomerPaymentProfileController;
13-
import net.authorize.api.controller.base.ApiOperationBase;
14-
import net.authorize.cim.Result;
15-
import net.authorize.cim.TransactionType;
16-
import net.authorize.cim.ValidationModeType;
1710

1811
//author @krgupta
1912
public class CreateCustomerPaymentProfile {
@@ -61,23 +54,30 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Stri
6154
CreateCustomerPaymentProfileController controller = new CreateCustomerPaymentProfileController(apiRequest);
6255
controller.execute();
6356

64-
CreateCustomerPaymentProfileResponse response = new CreateCustomerPaymentProfileResponse();
65-
response = controller.getApiResponse();
66-
if (response!=null) {
67-
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
68-
69-
System.out.println(response.getCustomerPaymentProfileId());
70-
System.out.println(response.getMessages().getMessage().get(0).getCode());
71-
System.out.println(response.getMessages().getMessage().get(0).getText());
72-
if (response.getValidationDirectResponse() != null)
73-
System.out.println(response.getValidationDirectResponse());
57+
ANetApiResponse apiResponse = controller.getApiResponse();
58+
if (apiResponse != null) {
59+
if (apiResponse instanceof CreateCustomerPaymentProfileResponse) {
60+
CreateCustomerPaymentProfileResponse response = (CreateCustomerPaymentProfileResponse) apiResponse;
61+
62+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
63+
System.out.println(response.getCustomerPaymentProfileId());
64+
System.out.println(response.getMessages().getMessage().get(0).getCode());
65+
System.out.println(response.getMessages().getMessage().get(0).getText());
66+
if (response.getValidationDirectResponse() != null)
67+
System.out.println(response.getValidationDirectResponse());
68+
}
69+
else if (response.getMessages().getResultCode() == MessageTypeEnum.ERROR) {
70+
System.out.println(response.getMessages().getMessage().get(0).getCode());
71+
System.out.println(response.getMessages().getMessage().get(0).getText());
72+
}
7473
}
75-
else
76-
{
77-
System.out.println("Failed to create customer payment profile: " + response.getMessages().getResultCode());
74+
else if (apiResponse instanceof ErrorResponse) {
75+
System.out.println(apiResponse.getMessages().getMessage().get(0).getCode());
76+
System.out.println(apiResponse.getMessages().getMessage().get(0).getText());
77+
System.out.println("Failed to create customer payment profile: " + apiResponse.getMessages().getResultCode());
7878
}
7979
}
8080

81-
return response;
81+
return apiResponse;
8282
}
8383
}

src/main/java/net/authorize/sample/CustomerProfiles/CreateCustomerProfile.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import net.authorize.Environment;
44
import net.authorize.api.contract.v1.*;
5-
import java.math.BigDecimal;
65
import net.authorize.api.controller.CreateCustomerProfileController;
76
import net.authorize.api.controller.base.ApiOperationBase;
87

@@ -47,37 +46,37 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Stri
4746
controller.execute();
4847

4948
// Get the response
50-
CreateCustomerProfileResponse response = new CreateCustomerProfileResponse();
51-
response = controller.getApiResponse();
49+
ANetApiResponse apiResponse = controller.getApiResponse();
5250

5351
// Parse the response to determine results
54-
if (response!=null) {
52+
if (apiResponse != null) {
53+
if (apiResponse instanceof CreateCustomerProfileResponse) {
5554
// If API Response is OK, go ahead and check the transaction response
56-
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
57-
System.out.println(response.getCustomerProfileId());
58-
if (!response.getCustomerPaymentProfileIdList().getNumericString().isEmpty()) {
59-
System.out.println(response.getCustomerPaymentProfileIdList().getNumericString().get(0));
60-
}
61-
if (!response.getCustomerShippingAddressIdList().getNumericString().isEmpty()) {
62-
System.out.println(response.getCustomerShippingAddressIdList().getNumericString().get(0));
63-
}
64-
if (!response.getValidationDirectResponseList().getString().isEmpty()) {
65-
System.out.println(response.getValidationDirectResponseList().getString().get(0));
66-
}
55+
CreateCustomerProfileResponse response = (CreateCustomerProfileResponse) apiResponse;
56+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
57+
System.out.println(response.getCustomerProfileId());
58+
if (!response.getCustomerPaymentProfileIdList().getNumericString().isEmpty()) {
59+
System.out.println(response.getCustomerPaymentProfileIdList().getNumericString().get(0));
60+
}
61+
if (!response.getCustomerShippingAddressIdList().getNumericString().isEmpty()) {
62+
System.out.println(response.getCustomerShippingAddressIdList().getNumericString().get(0));
63+
}
64+
if (!response.getValidationDirectResponseList().getString().isEmpty()) {
65+
System.out.println(response.getValidationDirectResponseList().getString().get(0));
66+
}
67+
}
68+
else if (response.getMessages().getResultCode() == MessageTypeEnum.ERROR) {
69+
System.out.println(response.getMessages().getMessage().get(0).getCode());
70+
System.out.println(response.getMessages().getMessage().get(0).getText());
71+
}
72+
}
73+
else if (apiResponse instanceof ErrorResponse) {
74+
System.out.println(apiResponse.getMessages().getMessage().get(0).getCode());
75+
System.out.println(apiResponse.getMessages().getMessage().get(0).getText());
76+
System.out.println("Failed to create customer profile: " + apiResponse.getMessages().getResultCode());
6777
}
68-
else
69-
{
70-
System.out.println("Failed to create customer profile: " + response.getMessages().getResultCode());
71-
}
72-
} else {
73-
// Display the error code and message when response is null
74-
ANetApiResponse errorResponse = controller.getErrorResponse();
75-
System.out.println("Failed to get response");
76-
if (!errorResponse.getMessages().getMessage().isEmpty()) {
77-
System.out.println("Error: "+errorResponse.getMessages().getMessage().get(0).getCode()+" \n"+ errorResponse.getMessages().getMessage().get(0).getText());
78-
}
79-
}
80-
return response;
78+
}
79+
return apiResponse;
8180

8281
}
8382
}

src/main/java/net/authorize/sample/CustomerProfiles/CreateCustomerProfileFromTransaction.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import net.authorize.api.contract.v1.CreditCardType;
1313
import net.authorize.api.contract.v1.CustomerDataType;
1414
import net.authorize.api.contract.v1.CustomerProfileBaseType;
15+
import net.authorize.api.contract.v1.ErrorResponse;
1516
import net.authorize.api.contract.v1.MerchantAuthenticationType;
17+
import net.authorize.api.contract.v1.MessageTypeEnum;
1618
import net.authorize.api.contract.v1.PaymentType;
1719
import net.authorize.api.contract.v1.TransactionRequestType;
1820
import net.authorize.api.controller.CreateCustomerProfileFromTransactionController;
@@ -46,34 +48,72 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Doub
4648
customer.setEmail(email);
4749
requestInternal.setCustomer(customer);
4850

49-
CreateTransactionRequest request = new CreateTransactionRequest();
50-
request.setTransactionRequest(requestInternal);
51+
CreateTransactionRequest transactionRequest = new CreateTransactionRequest();
52+
transactionRequest.setTransactionRequest(requestInternal);
5153

52-
CreateTransactionController controller = new CreateTransactionController(request);
54+
CreateTransactionController controller = new CreateTransactionController(transactionRequest);
5355
controller.execute();
5456

55-
CreateTransactionResponse response = controller.getApiResponse();
57+
ANetApiResponse apiResponseForTransaction = controller.getApiResponse();
58+
59+
if (apiResponseForTransaction != null) {
60+
if (!(apiResponseForTransaction instanceof CreateTransactionResponse) && !(apiResponseForTransaction instanceof ErrorResponse)) {
61+
System.out.println(apiResponseForTransaction.getMessages().getMessage().get(0).getCode());
62+
System.out.println(apiResponseForTransaction.getMessages().getMessage().get(0).getText());
63+
System.out.println("Failed to create transaction: " + apiResponseForTransaction.getMessages().getResultCode());
64+
65+
return apiResponseForTransaction;
66+
}
67+
}
68+
69+
CreateTransactionResponse transactionResponse = (CreateTransactionResponse) apiResponseForTransaction;
5670

5771
CustomerProfileBaseType customerProfile = new CustomerProfileBaseType();
5872
customerProfile.setMerchantCustomerId("123213");
5973
customerProfile.setEmail("johnsnow@castleblack.com");
6074
customerProfile.setDescription("This is a sample customer profile");
6175

62-
CreateCustomerProfileFromTransactionRequest transaction_request = new CreateCustomerProfileFromTransactionRequest();
63-
transaction_request.setTransId(response.getTransactionResponse().getTransId());
76+
CreateCustomerProfileFromTransactionRequest request = new CreateCustomerProfileFromTransactionRequest();
77+
request.setTransId(transactionResponse.getTransactionResponse().getTransId());
6478
// You can either specify the customer information in form of customerProfileBaseType object
65-
transaction_request.setCustomer(customerProfile);
79+
request.setCustomer(customerProfile);
6680
// OR
6781
// You can just provide the customer Profile ID
6882
// transaction_request.setCustomerProfileId("1232132");
6983

70-
CreateCustomerProfileFromTransactionController createProfileController = new CreateCustomerProfileFromTransactionController(transaction_request);
84+
CreateCustomerProfileFromTransactionController createProfileController = new CreateCustomerProfileFromTransactionController(request);
7185
createProfileController.execute();
72-
CreateCustomerProfileResponse customer_response = createProfileController.getApiResponse();
86+
ANetApiResponse apiResponse = createProfileController.getApiResponse();
7387

74-
if (customer_response != null) {
75-
System.out.println(transaction_request.getTransId());
76-
}
77-
return customer_response;
88+
if (apiResponse != null) {
89+
if (apiResponse instanceof CreateCustomerProfileResponse) {
90+
CreateCustomerProfileResponse response = (CreateCustomerProfileResponse) apiResponse;
91+
92+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
93+
System.out.println("Transaction ID : " + transactionResponse.getTransactionResponse().getTransId());
94+
System.out.println("Customer Profile Created : " + response.getCustomerProfileId());
95+
if (!response.getCustomerPaymentProfileIdList().getNumericString().isEmpty()) {
96+
System.out.println(response.getCustomerPaymentProfileIdList().getNumericString().get(0));
97+
}
98+
if (!response.getCustomerShippingAddressIdList().getNumericString().isEmpty()) {
99+
System.out.println(response.getCustomerShippingAddressIdList().getNumericString().get(0));
100+
}
101+
if (!response.getValidationDirectResponseList().getString().isEmpty()) {
102+
System.out.println(response.getValidationDirectResponseList().getString().get(0));
103+
}
104+
}
105+
else if (response.getMessages().getResultCode() == MessageTypeEnum.ERROR) {
106+
System.out.println(response.getMessages().getMessage().get(0).getCode());
107+
System.out.println(response.getMessages().getMessage().get(0).getText());
108+
}
109+
}
110+
else if (apiResponse instanceof ErrorResponse) {
111+
System.out.println(apiResponse.getMessages().getMessage().get(0).getCode());
112+
System.out.println(apiResponse.getMessages().getMessage().get(0).getText());
113+
System.out.println("Failed to create customer payment profile: " + apiResponse.getMessages().getResultCode());
114+
}
115+
}
116+
117+
return apiResponse;
78118
}
79119
}

src/main/java/net/authorize/sample/CustomerProfiles/CreateCustomerShippingAddress.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import net.authorize.Environment;
44
import net.authorize.api.contract.v1.*;
5-
import java.math.BigDecimal;
65
import net.authorize.api.controller.CreateCustomerShippingAddressController;
76
import net.authorize.api.controller.base.ApiOperationBase;
87

@@ -34,21 +33,28 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Stri
3433
CreateCustomerShippingAddressController controller = new CreateCustomerShippingAddressController(apiRequest);
3534
controller.execute();
3635

37-
CreateCustomerShippingAddressResponse response = controller.getApiResponse();
38-
if (response!=null) {
39-
40-
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
41-
42-
System.out.println(response.getCustomerAddressId());
43-
System.out.println(response.getMessages().getMessage().get(0).getCode());
44-
System.out.println(response.getMessages().getMessage().get(0).getText());
45-
}
46-
else
47-
{
48-
System.out.println("Failed to create customer shipping address: " + response.getMessages().getResultCode());
36+
ANetApiResponse apiResponse = controller.getApiResponse();
37+
if (apiResponse != null) {
38+
if (apiResponse instanceof CreateCustomerShippingAddressResponse) {
39+
CreateCustomerShippingAddressResponse response = (CreateCustomerShippingAddressResponse) apiResponse;
40+
41+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
42+
System.out.println(response.getCustomerAddressId());
43+
System.out.println(response.getMessages().getMessage().get(0).getCode());
44+
System.out.println(response.getMessages().getMessage().get(0).getText());
45+
}
46+
else if (response.getMessages().getResultCode() == MessageTypeEnum.ERROR) {
47+
System.out.println(response.getMessages().getMessage().get(0).getCode());
48+
System.out.println(response.getMessages().getMessage().get(0).getText());
49+
}
50+
}
51+
else if (apiResponse instanceof ErrorResponse) {
52+
System.out.println(apiResponse.getMessages().getMessage().get(0).getCode());
53+
System.out.println(apiResponse.getMessages().getMessage().get(0).getText());
54+
System.out.println("Failed to create customer shipping address: " + apiResponse.getMessages().getResultCode());
4955
}
5056
}
51-
return response;
57+
return apiResponse;
5258

5359
}
5460
}

src/main/java/net/authorize/sample/CustomerProfiles/DeleteCustomerPaymentProfile.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import net.authorize.Environment;
44
import net.authorize.api.contract.v1.*;
5-
6-
import net.authorize.api.contract.v1.MerchantAuthenticationType;
75
import net.authorize.api.controller.base.ApiOperationBase;
86
import net.authorize.api.controller.DeleteCustomerPaymentProfileController;
9-
import net.authorize.api.controller.base.ApiOperationBase;
107

118
public class DeleteCustomerPaymentProfile {
129

@@ -27,21 +24,27 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Stri
2724
DeleteCustomerPaymentProfileController controller = new DeleteCustomerPaymentProfileController(apiRequest);
2825
controller.execute();
2926

30-
DeleteCustomerPaymentProfileResponse response = new DeleteCustomerPaymentProfileResponse();
31-
response = controller.getApiResponse();
32-
33-
if (response!=null) {
34-
35-
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
36-
37-
System.out.println(response.getMessages().getMessage().get(0).getCode());
38-
System.out.println(response.getMessages().getMessage().get(0).getText());
39-
}
40-
else
41-
{
42-
System.out.println("Failed to delete customer payment profile: " + response.getMessages().getResultCode());
27+
ANetApiResponse apiResponse = controller.getApiResponse();
28+
29+
if (apiResponse != null) {
30+
if (apiResponse instanceof DeleteCustomerPaymentProfileResponse) {
31+
DeleteCustomerPaymentProfileResponse response = (DeleteCustomerPaymentProfileResponse) apiResponse;
32+
33+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
34+
System.out.println(response.getMessages().getMessage().get(0).getCode());
35+
System.out.println(response.getMessages().getMessage().get(0).getText());
36+
}
37+
else if (response.getMessages().getResultCode() == MessageTypeEnum.ERROR) {
38+
System.out.println(response.getMessages().getMessage().get(0).getCode());
39+
System.out.println(response.getMessages().getMessage().get(0).getText());
40+
}
41+
}
42+
else if (apiResponse instanceof ErrorResponse) {
43+
System.out.println(apiResponse.getMessages().getMessage().get(0).getCode());
44+
System.out.println(apiResponse.getMessages().getMessage().get(0).getText());
45+
System.out.println("Failed to delete customer payment profile: " + apiResponse.getMessages().getResultCode());
4346
}
4447
}
45-
return response;
48+
return apiResponse;
4649
}
4750
}

0 commit comments

Comments
 (0)