-
Notifications
You must be signed in to change notification settings - Fork 446
/
ChargeCreditCard.java
99 lines (86 loc) · 5.12 KB
/
ChargeCreditCard.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
package net.authorize.sample.PaymentTransactions;
import java.math.BigDecimal;
import java.math.RoundingMode;
import net.authorize.Environment;
import net.authorize.api.contract.v1.*;
import net.authorize.api.controller.CreateTransactionController;
import net.authorize.api.controller.base.ApiOperationBase;
public class ChargeCreditCard {
public static void main(String[] args) {
run("5KP3u95bQpv", "346HZ32z3fP4hTG2", 25.00);
}
//
// Run this sample from command line with:
// java -jar target/ChargeCreditCard-jar-with-dependencies.jar
//
public static ANetApiResponse run(String apiLoginId, String transactionKey, Double amount) {
// Set the request to operate in either the sandbox or production environment
ApiOperationBase.setEnvironment(Environment.SANDBOX);
// Create object with merchant authentication details
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
merchantAuthenticationType.setName(apiLoginId);
merchantAuthenticationType.setTransactionKey(transactionKey);
// Populate the payment data
PaymentType paymentType = new PaymentType();
CreditCardType creditCard = new CreditCardType();
creditCard.setCardNumber("4242424242424242");
creditCard.setExpirationDate("0835");
paymentType.setCreditCard(creditCard);
// Set email address (optional)
CustomerDataType customer = new CustomerDataType();
customer.setEmail("test@test.test");
// Create the payment transaction object
TransactionRequestType txnRequest = new TransactionRequestType();
txnRequest.setTransactionType(TransactionTypeEnum.AUTH_CAPTURE_TRANSACTION.value());
txnRequest.setPayment(paymentType);
txnRequest.setCustomer(customer);
txnRequest.setAmount(new BigDecimal(amount).setScale(2, RoundingMode.CEILING));
// Create the API request and set the parameters for this specific request
CreateTransactionRequest apiRequest = new CreateTransactionRequest();
apiRequest.setMerchantAuthentication(merchantAuthenticationType);
apiRequest.setTransactionRequest(txnRequest);
// Call the controller
CreateTransactionController controller = new CreateTransactionController(apiRequest);
controller.execute();
// Get the response
CreateTransactionResponse response = new CreateTransactionResponse();
response = controller.getApiResponse();
// Parse the response to determine results
if (response!=null) {
// If API Response is OK, go ahead and check the transaction response
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
TransactionResponse result = response.getTransactionResponse();
if (result.getMessages() != null) {
System.out.println("Successfully created transaction with Transaction ID: " + result.getTransId());
System.out.println("Response Code: " + result.getResponseCode());
System.out.println("Message Code: " + result.getMessages().getMessage().get(0).getCode());
System.out.println("Description: " + result.getMessages().getMessage().get(0).getDescription());
System.out.println("Auth Code: " + result.getAuthCode());
} else {
System.out.println("Failed Transaction.");
if (response.getTransactionResponse().getErrors() != null) {
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
}
}
} else {
System.out.println("Failed Transaction.");
if (response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null) {
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
} else {
System.out.println("Error Code: " + response.getMessages().getMessage().get(0).getCode());
System.out.println("Error message: " + response.getMessages().getMessage().get(0).getText());
}
}
} else {
// Display the error code and message when response is null
ANetApiResponse errorResponse = controller.getErrorResponse();
System.out.println("Failed to get response");
if (!errorResponse.getMessages().getMessage().isEmpty()) {
System.out.println("Error: "+errorResponse.getMessages().getMessage().get(0).getCode()+" \n"+ errorResponse.getMessages().getMessage().get(0).getText());
}
}
return response;
}
}