Skip to content

Commit 55f13db

Browse files
committed
Files for testing rest api reference
1 parent 0a72248 commit 55f13db

File tree

3 files changed

+334
-0
lines changed

3 files changed

+334
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.authorize.sample.PaymentTransactions;
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.CreateTransactionRequest;
9+
import net.authorize.api.contract.v1.CreateTransactionResponse;
10+
import net.authorize.api.contract.v1.CreditCardType;
11+
import net.authorize.api.contract.v1.MerchantAuthenticationType;
12+
import net.authorize.api.contract.v1.MessageTypeEnum;
13+
import net.authorize.api.contract.v1.PaymentType;
14+
import net.authorize.api.contract.v1.TransactionRequestType;
15+
import net.authorize.api.contract.v1.TransactionResponse;
16+
import net.authorize.api.contract.v1.TransactionTypeEnum;
17+
import net.authorize.api.controller.CreateTransactionController;
18+
import net.authorize.api.controller.base.ApiOperationBase;
19+
20+
public class AuthorizeCreditCard {
21+
22+
//
23+
// Run this sample from command line with:
24+
// java -jar target/AuthorizeCreditCard-jar-with-dependencies.jar
25+
//
26+
public static ANetApiResponse run(String apiLoginId, String transactionKey, Double amount) {
27+
28+
// Set the request to operate in either the sandbox or production environment
29+
ApiOperationBase.setEnvironment(Environment.SANDBOX);
30+
31+
// Create object with merchant authentication details
32+
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
33+
merchantAuthenticationType.setName(apiLoginId);
34+
merchantAuthenticationType.setTransactionKey(transactionKey);
35+
36+
// Populate the payment data
37+
PaymentType paymentType = new PaymentType();
38+
CreditCardType creditCard = new CreditCardType();
39+
creditCard.setCardNumber("4242424242424242");
40+
creditCard.setExpirationDate("0822");
41+
paymentType.setCreditCard(creditCard);
42+
43+
// Create the payment transaction object
44+
TransactionRequestType txnRequest = new TransactionRequestType();
45+
txnRequest.setTransactionType(TransactionTypeEnum.AUTH_ONLY_TRANSACTION.value());
46+
txnRequest.setPayment(paymentType);
47+
txnRequest.setAmount(new BigDecimal(amount).setScale(2, RoundingMode.CEILING));
48+
49+
// Create the API request and set the parameters for this specific request
50+
CreateTransactionRequest apiRequest = new CreateTransactionRequest();
51+
apiRequest.setMerchantAuthentication(merchantAuthenticationType);
52+
apiRequest.setTransactionRequest(txnRequest);
53+
54+
// Call the controller
55+
CreateTransactionController controller = new CreateTransactionController(apiRequest);
56+
controller.execute();
57+
58+
// Get the response
59+
CreateTransactionResponse response = new CreateTransactionResponse();
60+
response = controller.getApiResponse();
61+
62+
// Parse the response to determine results
63+
if (response!=null) {
64+
// If API Response is OK, go ahead and check the transaction response
65+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
66+
TransactionResponse result = response.getTransactionResponse();
67+
if (result.getMessages() != null) {
68+
System.out.println("Successfully created auth-only transaction with Transaction ID: " + result.getTransId());
69+
System.out.println("Response Code: " + result.getResponseCode());
70+
System.out.println("Message Code: " + result.getMessages().getMessage().get(0).getCode());
71+
System.out.println("Description: " + result.getMessages().getMessage().get(0).getDescription());
72+
System.out.println("Auth Code: " + result.getAuthCode());
73+
} else {
74+
System.out.println("Failed Transaction.");
75+
if (response.getTransactionResponse().getErrors() != null) {
76+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
77+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
78+
}
79+
}
80+
} else {
81+
System.out.println("Failed Transaction.");
82+
if (response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null) {
83+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
84+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
85+
} else {
86+
System.out.println("Error Code: " + response.getMessages().getMessage().get(0).getCode());
87+
System.out.println("Error message: " + response.getMessages().getMessage().get(0).getText());
88+
}
89+
}
90+
} else {
91+
// Display the error code and message when response is null
92+
ANetApiResponse errorResponse = controller.getErrorResponse();
93+
System.out.println("Failed to get response");
94+
if (!errorResponse.getMessages().getMessage().isEmpty()) {
95+
System.out.println("Error: "+errorResponse.getMessages().getMessage().get(0).getCode()+" \n"+ errorResponse.getMessages().getMessage().get(0).getText());
96+
}
97+
}
98+
99+
return response;
100+
}
101+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
use net\authorize\api\contract\v1 as AnetAPI;
5+
use net\authorize\api\controller as AnetController;
6+
7+
define("AUTHORIZENET_LOG_FILE", "phplog");
8+
9+
function chargeCreditCard($amount)
10+
{
11+
/* Create a merchantAuthenticationType object with authentication details
12+
retrieved from the constants file */
13+
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
14+
$merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID);
15+
$merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY);
16+
17+
// Set the transaction's refId
18+
$refId = 'ref' . time();
19+
20+
// Create the payment data for a credit card
21+
$creditCard = new AnetAPI\CreditCardType();
22+
$creditCard->setCardNumber("4111111111111111");
23+
$creditCard->setExpirationDate("2038-12");
24+
$creditCard->setCardCode("123");
25+
26+
// Add the payment data to a paymentType object
27+
$paymentOne = new AnetAPI\PaymentType();
28+
$paymentOne->setCreditCard($creditCard);
29+
30+
// Create order information
31+
$order = new AnetAPI\OrderType();
32+
$order->setInvoiceNumber("10101");
33+
$order->setDescription("Golf Shirts");
34+
35+
// Set the customer's Bill To address
36+
$customerAddress = new AnetAPI\CustomerAddressType();
37+
$customerAddress->setFirstName("Ellen");
38+
$customerAddress->setLastName("Johnson");
39+
$customerAddress->setCompany("Souveniropolis");
40+
$customerAddress->setAddress("14 Main Street");
41+
$customerAddress->setCity("Pecan Springs");
42+
$customerAddress->setState("TX");
43+
$customerAddress->setZip("44628");
44+
$customerAddress->setCountry("USA");
45+
46+
// Set the customer's identifying information
47+
$customerData = new AnetAPI\CustomerDataType();
48+
$customerData->setType("individual");
49+
$customerData->setId("99999456654");
50+
$customerData->setEmail("EllenJohnson@example.com");
51+
52+
// Add values for transaction settings
53+
$duplicateWindowSetting = new AnetAPI\SettingType();
54+
$duplicateWindowSetting->setSettingName("duplicateWindow");
55+
$duplicateWindowSetting->setSettingValue("60");
56+
57+
// Add some merchant defined fields. These fields won't be stored with the transaction,
58+
// but will be echoed back in the response.
59+
$merchantDefinedField1 = new AnetAPI\UserFieldType();
60+
$merchantDefinedField1->setName("customerLoyaltyNum");
61+
$merchantDefinedField1->setValue("1128836273");
62+
63+
$merchantDefinedField2 = new AnetAPI\UserFieldType();
64+
$merchantDefinedField2->setName("favoriteColor");
65+
$merchantDefinedField2->setValue("blue");
66+
67+
// Create a TransactionRequestType object and add the previous objects to it
68+
$transactionRequestType = new AnetAPI\TransactionRequestType();
69+
$transactionRequestType->setTransactionType("authCaptureTransaction");
70+
$transactionRequestType->setAmount($amount);
71+
$transactionRequestType->setOrder($order);
72+
$transactionRequestType->setPayment($paymentOne);
73+
$transactionRequestType->setBillTo($customerAddress);
74+
$transactionRequestType->setCustomer($customerData);
75+
$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);
76+
$transactionRequestType->addToUserFields($merchantDefinedField1);
77+
$transactionRequestType->addToUserFields($merchantDefinedField2);
78+
79+
// Assemble the complete transaction request
80+
$request = new AnetAPI\CreateTransactionRequest();
81+
$request->setMerchantAuthentication($merchantAuthentication);
82+
$request->setRefId($refId);
83+
$request->setTransactionRequest($transactionRequestType);
84+
85+
// Create the controller and get the response
86+
$controller = new AnetController\CreateTransactionController($request);
87+
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
88+
89+
90+
if ($response != null) {
91+
// Check to see if the API request was successfully received and acted upon
92+
if ($response->getMessages()->getResultCode() == \SampleCode\Constants::RESPONSE_OK) {
93+
// Since the API request was successful, look for a transaction response
94+
// and parse it to display the results of authorizing the card
95+
$tresponse = $response->getTransactionResponse();
96+
97+
if ($tresponse != null && $tresponse->getMessages() != null) {
98+
echo " Successfully created transaction with Transaction ID: " . $tresponse->getTransId() . "\n";
99+
echo " Transaction Response Code: " . $tresponse->getResponseCode() . "\n";
100+
echo " Message Code: " . $tresponse->getMessages()[0]->getCode() . "\n";
101+
echo " Auth Code: " . $tresponse->getAuthCode() . "\n";
102+
echo " Description: " . $tresponse->getMessages()[0]->getDescription() . "\n";
103+
} else {
104+
echo "Transaction Failed \n";
105+
if ($tresponse->getErrors() != null) {
106+
echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
107+
echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
108+
}
109+
}
110+
// Or, print errors if the API request wasn't successful
111+
} else {
112+
echo "Transaction Failed \n";
113+
$tresponse = $response->getTransactionResponse();
114+
115+
if ($tresponse != null && $tresponse->getErrors() != null) {
116+
echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
117+
echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
118+
} else {
119+
echo " Error Code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
120+
echo " Error Message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
121+
}
122+
}
123+
} else {
124+
echo "No response returned \n";
125+
}
126+
127+
return $response;
128+
}
129+
130+
if (!defined('DONT_RUN_SAMPLES')) {
131+
chargeCreditCard(\SampleCode\Constants::SAMPLE_AMOUNT);
132+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.authorize.sample.PaymentTransactions;
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.CreateTransactionRequest;
9+
import net.authorize.api.contract.v1.CreateTransactionResponse;
10+
import net.authorize.api.contract.v1.CreditCardType;
11+
import net.authorize.api.contract.v1.MerchantAuthenticationType;
12+
import net.authorize.api.contract.v1.MessageTypeEnum;
13+
import net.authorize.api.contract.v1.PaymentType;
14+
import net.authorize.api.contract.v1.TransactionRequestType;
15+
import net.authorize.api.contract.v1.TransactionResponse;
16+
import net.authorize.api.contract.v1.TransactionTypeEnum;
17+
import net.authorize.api.controller.CreateTransactionController;
18+
import net.authorize.api.controller.base.ApiOperationBase;
19+
20+
public class AuthorizeCreditCard {
21+
22+
//
23+
// Run this sample from command line with:
24+
// java -jar target/AuthorizeCreditCard-jar-with-dependencies.jar
25+
//
26+
public static ANetApiResponse run(String apiLoginId, String transactionKey, Double amount) {
27+
28+
// Set the request to operate in either the sandbox or production environment
29+
ApiOperationBase.setEnvironment(Environment.SANDBOX);
30+
31+
// Create object with merchant authentication details
32+
MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ;
33+
merchantAuthenticationType.setName(apiLoginId);
34+
merchantAuthenticationType.setTransactionKey(transactionKey);
35+
36+
// Populate the payment data
37+
PaymentType paymentType = new PaymentType();
38+
CreditCardType creditCard = new CreditCardType();
39+
creditCard.setCardNumber("4242424242424242");
40+
creditCard.setExpirationDate("0822");
41+
paymentType.setCreditCard(creditCard);
42+
43+
// Create the payment transaction object
44+
TransactionRequestType txnRequest = new TransactionRequestType();
45+
txnRequest.setTransactionType(TransactionTypeEnum.AUTH_ONLY_TRANSACTION.value());
46+
txnRequest.setPayment(paymentType);
47+
txnRequest.setAmount(new BigDecimal(amount).setScale(2, RoundingMode.CEILING));
48+
49+
// Create the API request and set the parameters for this specific request
50+
CreateTransactionRequest apiRequest = new CreateTransactionRequest();
51+
apiRequest.setMerchantAuthentication(merchantAuthenticationType);
52+
apiRequest.setTransactionRequest(txnRequest);
53+
54+
// Call the controller
55+
CreateTransactionController controller = new CreateTransactionController(apiRequest);
56+
controller.execute();
57+
58+
// Get the response
59+
CreateTransactionResponse response = new CreateTransactionResponse();
60+
response = controller.getApiResponse();
61+
62+
// Parse the response to determine results
63+
if (response!=null) {
64+
// If API Response is OK, go ahead and check the transaction response
65+
if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
66+
TransactionResponse result = response.getTransactionResponse();
67+
if (result.getMessages() != null) {
68+
System.out.println("Successfully created auth-only transaction with Transaction ID: " + result.getTransId());
69+
System.out.println("Response Code: " + result.getResponseCode());
70+
System.out.println("Message Code: " + result.getMessages().getMessage().get(0).getCode());
71+
System.out.println("Description: " + result.getMessages().getMessage().get(0).getDescription());
72+
System.out.println("Auth Code: " + result.getAuthCode());
73+
} else {
74+
System.out.println("Failed Transaction.");
75+
if (response.getTransactionResponse().getErrors() != null) {
76+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
77+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
78+
}
79+
}
80+
} else {
81+
System.out.println("Failed Transaction.");
82+
if (response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null) {
83+
System.out.println("Error Code: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
84+
System.out.println("Error message: " + response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
85+
} else {
86+
System.out.println("Error Code: " + response.getMessages().getMessage().get(0).getCode());
87+
System.out.println("Error message: " + response.getMessages().getMessage().get(0).getText());
88+
}
89+
}
90+
} else {
91+
// Display the error code and message when response is null
92+
ANetApiResponse errorResponse = controller.getErrorResponse();
93+
System.out.println("Failed to get response");
94+
if (!errorResponse.getMessages().getMessage().isEmpty()) {
95+
System.out.println("Error: "+errorResponse.getMessages().getMessage().get(0).getCode()+" \n"+ errorResponse.getMessages().getMessage().get(0).getText());
96+
}
97+
}
98+
99+
return response;
100+
}
101+
}

0 commit comments

Comments
 (0)