Skip to content

Commit bb0c297

Browse files
authored
Merge pull request #1 from AuthorizeNet/master
updating sample code fork
2 parents 5b0a32d + 73191d9 commit bb0c297

38 files changed

+1497
-429
lines changed

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
# Sample Java Code for Authorize.Net
2-
[![Build Status](https://travis-ci.org/AuthorizeNet/sample-code-java.png?branch=master)]
3-
(https://travis-ci.org/AuthorizeNet/sample-code-java)
2+
[![Build Status](https://travis-ci.org/AuthorizeNet/sample-code-java.png?branch=master)](https://travis-ci.org/AuthorizeNet/sample-code-java)
43

54
This repository contains working code samples which demonstrate Java integration with the Authorize.Net Java SDK
65
The samples are organized just like our API, which you can also try out directly here: http://developer.authorize.net/api/reference
76

87

9-
##Using the Sample Code
8+
## Using the Sample Code
109

1110
The samples are all completely independent and self-contained so you can look at them to get a gist of how the method works, you can use the snippets to try in your own sample project, or you can run each sample from the command line.
1211

13-
##Running the Samples
14-
Clone this repository.
15-
Run "mvn package" in the root directory to create the SampleCode console app.
16-
Then run a sample directly by name:
17-
````
12+
## Running the Samples
13+
1. Clone this repository.
14+
2. Run "mvn package" in the root directory to create the SampleCode console app.
15+
3. Then run a sample directly by name:
16+
```
1817
> java -jar target/SampleCode.jar [CodeSampleName]
19-
````
18+
```
2019
e.g.
21-
````
20+
```
2221
> java -jar target/SampleCode.jar ChargeCreditCard
23-
````
22+
```
2423
Running SampleCode without a parameter will give you the list of sample names. Handy or what!
2524

25+
**NOTE : You can update to your Sandbox credentials in SampleCode.java**
26+
27+
**For using behind proxy**
28+
29+
Please set the JAVA environment proxy using a similar code :
30+
```
31+
System.setProperty("https.proxyUse", "true");
32+
System.setProperty("https.proxyHost", "127.0.0.1");
33+
System.setProperty("https.proxyPort", "3128");
34+
```
35+
2636
*PLEASE NOTE THIS PROJECT IS CURRENTLY UNDER DEVELOPMENT*

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.8.8</sdkVersion>
4+
<sdkVersion>1.9.3</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>${sdkVersion}</version>
17+
<version>1.9.3</version>
1818
</dependency>
1919
<dependency>
2020
<groupId>junit</groupId>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Stri
3030

3131
CustomerProfileType customerProfileType = new CustomerProfileType();
3232
customerProfileType.setMerchantCustomerId("M_" + eMail);
33-
customerProfileType.setDescription("Profile description here");
33+
customerProfileType.setDescription("Profile description for " + eMail);
3434
customerProfileType.setEmail(eMail);
3535
customerProfileType.getPaymentProfiles().add(customerPaymentProfileType);
3636

src/main/java/net/authorize/sample/CustomerProfiles/GetHostedProfilePage.java renamed to src/main/java/net/authorize/sample/CustomerProfiles/GetAcceptCustomerProfilePage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import net.authorize.api.controller.GetHostedProfilePageController;
99
import net.authorize.api.controller.base.ApiOperationBase;
1010

11-
public class GetHostedProfilePage {
11+
public class GetAcceptCustomerProfilePage {
1212

1313
public static ANetApiResponse run(String apiLoginId, String transactionKey, String customerProfileId) {
1414

@@ -52,4 +52,4 @@ public static ANetApiResponse run(String apiLoginId, String transactionKey, Stri
5252
}
5353
return response;
5454
}
55-
}
55+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package net.authorize.sample.FraudManagement;
2+
3+
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
5+
6+
import net.authorize.Environment;
7+
import net.authorize.api.contract.v1.ANetApiResponse;
8+
import net.authorize.api.contract.v1.AfdsTransactionEnum;
9+
import net.authorize.api.contract.v1.CreateTransactionRequest;
10+
import net.authorize.api.contract.v1.CreateTransactionResponse;
11+
import net.authorize.api.contract.v1.CreditCardType;
12+
import net.authorize.api.contract.v1.HeldTransactionRequestType;
13+
import net.authorize.api.contract.v1.MerchantAuthenticationType;
14+
import net.authorize.api.contract.v1.MessageTypeEnum;
15+
import net.authorize.api.contract.v1.PaymentType;
16+
import net.authorize.api.contract.v1.TransactionRequestType;
17+
import net.authorize.api.contract.v1.TransactionResponse;
18+
import net.authorize.api.contract.v1.TransactionTypeEnum;
19+
import net.authorize.api.contract.v1.UpdateHeldTransactionRequest;
20+
import net.authorize.api.contract.v1.UpdateHeldTransactionResponse;
21+
import net.authorize.api.controller.CreateTransactionController;
22+
import net.authorize.api.controller.UpdateHeldTransactionController;
23+
import net.authorize.api.controller.base.ApiOperationBase;
24+
25+
public class ApproveOrDeclineHeldTransaction {
26+
27+
public static ANetApiResponse run(String apiLoginId, String transactionKey, String transactionId) {
28+
29+
//Common code to set for all requests
30+
ApiOperationBase.setEnvironment(Environment.SANDBOX);
31+
32+
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
33+
merchantAuthenticationType.setName(apiLoginId);
34+
merchantAuthenticationType.setTransactionKey(transactionKey);
35+
ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType);
36+
37+
38+
// Create the payment transaction request
39+
HeldTransactionRequestType txnRequest = new HeldTransactionRequestType();
40+
txnRequest.setAction(AfdsTransactionEnum.APPROVE);
41+
txnRequest.setRefTransId("60012148613");
42+
43+
// Make the API Request
44+
UpdateHeldTransactionRequest apiRequest = new UpdateHeldTransactionRequest();
45+
apiRequest.setHeldTransactionRequest(txnRequest);
46+
UpdateHeldTransactionController controller = new UpdateHeldTransactionController(apiRequest);
47+
controller.execute();
48+
49+
50+
UpdateHeldTransactionResponse response = controller.getApiResponse();
51+
52+
if (response!=null) {
53+
// If API Response is ok, go ahead and check the transaction response
54+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
55+
TransactionResponse result = response.getTransactionResponse();
56+
if(result.getMessages() != null){
57+
System.out.println("Successfully updated transaction with Transaction ID: " + result.getTransId());
58+
System.out.println("Response Code: " + result.getResponseCode());
59+
System.out.println("Message Code: " + result.getMessages().getMessage().get(0).getCode());
60+
System.out.println("Description: " + result.getMessages().getMessage().get(0).getDescription());
61+
System.out.println("Auth Code: " + result.getAuthCode());
62+
}
63+
else {
64+
System.out.println("Failed while updating transaction.");
65+
if(response.getTransactionResponse().getErrors() != null){
66+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
67+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
68+
}
69+
}
70+
}
71+
else {
72+
System.out.println("Failed while updating transaction.");
73+
if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){
74+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
75+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
76+
}
77+
else {
78+
System.out.println("Error Code: " + response.getMessages().getMessage().get(0).getCode());
79+
System.out.println("Error message: " + response.getMessages().getMessage().get(0).getText());
80+
}
81+
}
82+
}
83+
else {
84+
System.out.println("Null Response.");
85+
}
86+
87+
return response;
88+
}
89+
90+
91+
92+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.authorize.sample.FraudManagement;
2+
3+
4+
import net.authorize.Environment;
5+
import net.authorize.api.contract.v1.*;
6+
import net.authorize.api.controller.GetSettledBatchListController;
7+
import net.authorize.api.controller.GetUnsettledTransactionListController;
8+
import net.authorize.api.controller.base.ApiOperationBase;
9+
10+
//author @krgupta
11+
public class GetHeldTransactionList{
12+
13+
public static ANetApiResponse run(String apiLoginId, String transactionKey) {
14+
ApiOperationBase.setEnvironment(Environment.SANDBOX);
15+
16+
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
17+
merchantAuthenticationType.setName(apiLoginId);
18+
merchantAuthenticationType.setTransactionKey(transactionKey);
19+
ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType);
20+
21+
GetUnsettledTransactionListRequest getRequest = new GetUnsettledTransactionListRequest();
22+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
23+
getRequest.setStatus(TransactionGroupStatusEnum.PENDING_APPROVAL);
24+
25+
Paging paging = new Paging();
26+
paging.setLimit(100);
27+
paging.setOffset(1);
28+
29+
getRequest.setPaging(paging);
30+
31+
TransactionListSorting sorting = new TransactionListSorting();
32+
sorting.setOrderBy(TransactionListOrderFieldEnum.ID);
33+
sorting.setOrderDescending(true);
34+
35+
getRequest.setSorting(sorting);
36+
37+
GetUnsettledTransactionListController controller = new GetUnsettledTransactionListController(getRequest);
38+
controller.execute();
39+
GetUnsettledTransactionListResponse getResponse = controller.getApiResponse();
40+
41+
if (getResponse!=null) {
42+
43+
if (getResponse.getMessages().getResultCode() == MessageTypeEnum.OK) {
44+
45+
System.out.println(getResponse.getMessages().getMessage().get(0).getCode());
46+
System.out.println(getResponse.getMessages().getMessage().get(0).getText());
47+
getResponse.getTransactions();
48+
49+
ArrayOfTransactionSummaryType txnList = getResponse.getTransactions();
50+
if (txnList != null) {
51+
System.out.println("List of Suspicious Transactions :");
52+
for (TransactionSummaryType txn : txnList.getTransaction()) {
53+
System.out.println(txn.getTransId());
54+
}
55+
}
56+
}
57+
else
58+
{
59+
System.out.println("Failed to get unsettled transaction list: " + getResponse.getMessages().getResultCode());
60+
}
61+
}
62+
return getResponse;
63+
64+
}
65+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package net.authorize.sample.MobileInappTransactions;
2+
3+
import java.math.BigDecimal;
4+
import net.authorize.Environment;
5+
import net.authorize.TransactionType;
6+
import net.authorize.api.contract.v1.*;
7+
import net.authorize.api.controller.CreateTransactionController;
8+
import net.authorize.api.controller.base.ApiOperationBase;
9+
10+
public class CreateAnAcceptTransaction
11+
{
12+
public static ANetApiResponse run(String apiLoginId, String transactionKey)
13+
{
14+
ApiOperationBase.setEnvironment(Environment.SANDBOX);
15+
16+
// Giving the merchant authentication information
17+
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
18+
merchantAuthenticationType.setName(apiLoginId);
19+
merchantAuthenticationType.setTransactionKey(transactionKey);
20+
ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType);
21+
// Setting the payment
22+
OpaqueDataType op = new OpaqueDataType();
23+
op.setDataDescriptor("COMMON.ACCEPT.INAPP.PAYMENT");
24+
op.setDataValue("9471471570959063005001");
25+
PaymentType paymentOne = new PaymentType();
26+
paymentOne.setOpaqueData(op);
27+
// Setting the transaction
28+
TransactionRequestType transactionRequest = new TransactionRequestType();
29+
transactionRequest.setAmount(new BigDecimal("131"));
30+
transactionRequest.setTransactionType(TransactionTypeEnum.AUTH_CAPTURE_TRANSACTION.value());
31+
transactionRequest.setPayment(paymentOne);
32+
// Making the api request
33+
CreateTransactionRequest apiRequest = new CreateTransactionRequest();
34+
apiRequest.setTransactionRequest(transactionRequest);
35+
// Creating the controller
36+
CreateTransactionController controller = new CreateTransactionController(apiRequest);
37+
controller.execute();
38+
// Getting the response
39+
CreateTransactionResponse response = controller.getApiResponse();
40+
41+
if (response!=null) {
42+
// If API Response is ok, go ahead and check the transaction response
43+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
44+
TransactionResponse result = response.getTransactionResponse();
45+
if(result.getMessages() != null){
46+
System.out.println("Successfully created transaction with Transaction ID: " + result.getTransId());
47+
System.out.println("Response Code: " + result.getResponseCode());
48+
System.out.println("Message Code: " + result.getMessages().getMessage().get(0).getCode());
49+
System.out.println("Description: " + result.getMessages().getMessage().get(0).getDescription());
50+
System.out.println("Auth code : " + result.getAuthCode());
51+
}
52+
else {
53+
System.out.println("Failed Transaction.");
54+
if(response.getTransactionResponse().getErrors() != null){
55+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
56+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
57+
}
58+
}
59+
}
60+
else {
61+
System.out.println("Failed Transaction.");
62+
if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){
63+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
64+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
65+
}
66+
else {
67+
System.out.println("Error Code: " + response.getMessages().getMessage().get(0).getCode());
68+
System.out.println("Error message: " + response.getMessages().getMessage().get(0).getText());
69+
}
70+
}
71+
}
72+
else {
73+
System.out.println("Null Response.");
74+
}
75+
76+
return response;
77+
}
78+
}

0 commit comments

Comments
 (0)