Hello,
I'm Developing an Android App and I have the necessity of using Authorize.net
Recurring Payment, so, I have added the Java SDK of Authorize.Net in my
android app as a library.
But once I'm hitting the API I'm getting a null response, even if I have
regenerated the key again for the Test Mode.
Code for Subscribing is below:
public static ANetApiResponse createAuthorizeNetSubscription(String
apiLoginId, String transactionKey,intervalLength, Double amount, String cardNumber,
String monthYear, String fName, String lName) {
//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 up payment schedule
PaymentScheduleType schedule = new PaymentScheduleType();
PaymentScheduleType.Interval interval = new
PaymentScheduleType.Interval();
interval.setLength(intervalLength);
interval.setUnit(ARBSubscriptionUnitEnum.DAYS);
schedule.setInterval(interval);
try {
XMLGregorianCalendar startDate =
DatatypeFactory.newInstance().newXMLGregorianCalendar();
startDate.setDay(31);
startDate.setMonth(12);
startDate.setYear(2020);
schedule.setStartDate(startDate); //2020-08-30
}
catch(Exception e) {
Log.e(TAG,"XMLGregorianCalendar Exception -
"+e.getMessage());
}
schedule.setTotalOccurrences((short)12);
schedule.setTrialOccurrences((short)1);
// Populate the payment data
PaymentType paymentType = new PaymentType();
CreditCardType creditCard = new CreditCardType();
creditCard.setCardNumber(cardNumber);
creditCard.setExpirationDate(monthYear);
paymentType.setCreditCard(creditCard);
ARBSubscriptionType arbSubscriptionType = new
ARBSubscriptionType();
arbSubscriptionType.setPaymentSchedule(schedule);
arbSubscriptionType.setAmount(new BigDecimal(amount).setScale(2,
RoundingMode.CEILING));
arbSubscriptionType.setTrialAmount(new
BigDecimal(1.23).setScale(2, RoundingMode.CEILING));
arbSubscriptionType.setPayment(paymentType);
NameAndAddressType name = new NameAndAddressType();
name.setFirstName(fName);
name.setLastName(lName);
arbSubscriptionType.setBillTo(name);
// Make the API Request
ARBCreateSubscriptionRequest apiRequest = new
ARBCreateSubscriptionRequest();
apiRequest.setSubscription(arbSubscriptionType);
ARBCreateSubscriptionController controller = new
ARBCreateSubscriptionController(apiRequest);
controller.execute();
ARBCreateSubscriptionResponse response =
controller.getApiResponse();
if (response!=null) {
if (response.getMessages().getResultCode() ==
MessageTypeEnum.OK) {
Log.e(TAG,response.getSubscriptionId());
Log.e(TAG,response.getMessages().getMessage().get(0).getCode());
Log.e(TAG,response.getMessages().getMessage().get(0).getText());
}
else
{
Log.e(TAG,"Failed to create Subscription: " +
response.getMessages().getResultCode());
Log.e(TAG,response.getMessages().getMessage().get(0).getText());
}
}
return response;
}
Also, I have added the Credit Card Charge Code so that I could initiate
the charge from the user Credit Card, whose code is also below:
public static ANetApiResponse chargeCreditCardAuthorizeNet(String
apiLoginId, String transactionKey, Double amount, String cardNumber,
String monthYear, String email) {
// 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(cardNumber);
creditCard.setExpirationDate(monthYear);
paymentType.setCreditCard(creditCard);
// Set email address (optional)
CustomerDataType customer = new CustomerDataType();
customer.setEmail(email);
// 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) {
Log.e(TAG,"Successfully created transaction with
Transaction ID: " + result.getTransId());
Log.e(TAG,"Response Code: " +
result.getResponseCode());
Log.e(TAG,"Message Code: " +
result.getMessages().getMessage().get(0).getCode());
Log.e(TAG,"Description: " +
result.getMessages().getMessage().get(0).getDescription());
Log.e(TAG,"Auth Code: " + result.getAuthCode());
} else {
Log.e(TAG,"Failed Transaction.");
if (response.getTransactionResponse().getErrors() !=
null) {
Log.e(TAG,"Error Code: " +
response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
Log.e(TAG,"Error message: " +
response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
}
}
} else {
Log.e(TAG,"Failed Transaction.");
if (response.getTransactionResponse() != null &&
response.getTransactionResponse().getErrors() != null) {
Log.e(TAG,"Error Code: " +
response.getTransactionResponse().getErrors().getError().get(0).getErrorCode());
Log.e(TAG,"Error message: " +
response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
} else {
Log.e(TAG,"Error Code: " +
response.getMessages().getMessage().get(0).getCode());
Log.e(TAG,"Error message: " +
response.getMessages().getMessage().get(0).getText());
}
}
} else {
// Display the error code and message when response is null
ANetApiResponse errorResponse =
controller.getErrorResponse();
Log.e(TAG,"Failed to get response"+errorResponse);
if (!errorResponse.getMessages().getMessage().isEmpty()) {
Log.e(TAG,"Error:
"+errorResponse.getMessages().getMessage().get(0).getCode()+" \n"+
errorResponse.getMessages().getMessage().get(0).getText());
}
}
return response;
}
In both the response I'm getting null value.
Could you please help me out with this?
Thank You.
โ10-07-2020 10:13 PM - edited โ10-07-2020 10:28 PM