forked from AuthorizeNet/sample-code-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChargeCustomerProfile.java
More file actions
98 lines (82 loc) · 4.63 KB
/
Copy pathChargeCustomerProfile.java
File metadata and controls
98 lines (82 loc) · 4.63 KB
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
package net.authorize.sample.PaymentTransactions;
import java.math.BigDecimal;
import java.math.RoundingMode;
import net.authorize.Environment;
import net.authorize.api.contract.v1.ANetApiResponse;
import net.authorize.api.contract.v1.CreateTransactionRequest;
import net.authorize.api.contract.v1.CreateTransactionResponse;
import net.authorize.api.contract.v1.CustomerProfilePaymentType;
import net.authorize.api.contract.v1.MerchantAuthenticationType;
import net.authorize.api.contract.v1.MessageTypeEnum;
import net.authorize.api.contract.v1.PaymentProfile;
import net.authorize.api.contract.v1.TransactionRequestType;
import net.authorize.api.contract.v1.TransactionResponse;
import net.authorize.api.contract.v1.TransactionTypeEnum;
import net.authorize.api.controller.CreateTransactionController;
import net.authorize.api.controller.base.ApiOperationBase;
public class ChargeCustomerProfile {
//
// Run this sample from command line with:
// java -jar target/ChargeCreditCard-jar-with-dependencies.jar
//
public static ANetApiResponse run(String apiLoginId, String transactionKey, String customerProfileId,
String customerPaymentProfileId, Double amount) {
//Common code to set for all requests
ApiOperationBase.setEnvironment(Environment.SANDBOX);
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
merchantAuthenticationType.setName(apiLoginId);
merchantAuthenticationType.setTransactionKey(transactionKey);
ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType);
// Set the profile ID to charge
CustomerProfilePaymentType profileToCharge = new CustomerProfilePaymentType();
profileToCharge.setCustomerProfileId(customerProfileId);
PaymentProfile paymentProfile = new PaymentProfile();
paymentProfile.setPaymentProfileId(customerPaymentProfileId);
profileToCharge.setPaymentProfile(paymentProfile);
// Create the payment transaction request
TransactionRequestType txnRequest = new TransactionRequestType();
txnRequest.setTransactionType(TransactionTypeEnum.AUTH_CAPTURE_TRANSACTION.value());
txnRequest.setProfile(profileToCharge);
txnRequest.setAmount(new BigDecimal(amount).setScale(2, RoundingMode.CEILING));
CreateTransactionRequest apiRequest = new CreateTransactionRequest();
apiRequest.setTransactionRequest(txnRequest);
CreateTransactionController controller = new CreateTransactionController(apiRequest);
controller.execute();
CreateTransactionResponse response = controller.getApiResponse();
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 {
System.out.println("Null Response.");
}
return response;
}
}