sdk-java icon indicating copy to clipboard operation
sdk-java copied to clipboard

How to use sdk-java in android.

Open patelbansari opened this issue 4 years ago • 2 comments

I'm using the below dependency to use sdk-java in android.

    implementation 'net.authorize:anet-java-sdk:2.0.1'
    implementation('javax.xml.bind:jaxb-api:2.3.0')
    implementation('javax.activation:activation:1.1')
    implementation('org.glassfish.jaxb:jaxb-runtime:2.3.0')
    implementation 'org.apache.logging.log4j:log4j-api:2.3'

After a successful build, I tried to run the below code with sandbox details.

    ApiOperationBase.setEnvironment(Environment.SANDBOX)

    // Create object with merchant authentication details

    // Create object with merchant authentication details
    val merchantAuthenticationType = MerchantAuthenticationType()
    merchantAuthenticationType.setName("my_login_id")
    merchantAuthenticationType.setTransactionKey("my_trans_key")
    // Set the request to operate in either the sandbox or production environment

    // Populate the payment data

    // Populate the payment data
    val creditCard = CreditCardType()
    creditCard.setCardNumber("4111111111111111")
    creditCard.setExpirationDate("1220")
    val paymentType = PaymentType()
    paymentType.setCreditCard(creditCard)

    // Set payment profile data

    // Set payment profile data
    val customerPaymentProfileType = CustomerPaymentProfileType()
    customerPaymentProfileType.setCustomerType(CustomerTypeEnum.INDIVIDUAL)
    customerPaymentProfileType.setPayment(paymentType)

    // Set customer profile data

    // Set customer profile data
    val customerProfileType = CustomerProfileType()
    customerProfileType.setMerchantCustomerId("[email protected]")
    customerProfileType.setDescription("Profile description for [email protected]")
    customerProfileType.setEmail("[email protected]")
    customerProfileType.getPaymentProfiles().add(customerPaymentProfileType)

    // Create the API request and set the parameters for this specific request

    // Create the API request and set the parameters for this specific request
    val apiRequest = CreateCustomerProfileRequest()
    apiRequest.setMerchantAuthentication(merchantAuthenticationType)
    apiRequest.setProfile(customerProfileType)
    apiRequest.setValidationMode(ValidationModeEnum.TEST_MODE)

    // Call the controller

    // Call the controller
    val controller =
        CreateCustomerProfileController(apiRequest)
    controller.execute()

    // Get the response

    // Get the response
    var response: CreateCustomerProfileResponse
    response = controller.getApiResponse()
    Log.e("",""+controller.getErrorResponse())
    // Parse the response to determine results

    // 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) {
            Log.e("",response.getCustomerProfileId())
            if (!response.getCustomerPaymentProfileIdList().getNumericString().isEmpty()) {
                Log.e("",
                    response.getCustomerPaymentProfileIdList().getNumericString().get(0)
                )
            }
            if (!response.customerShippingAddressIdList.getNumericString().isEmpty()) {
                Log.e("",
                    response.getCustomerShippingAddressIdList().getNumericString().get(0)
                )
            }
            if (!response.getValidationDirectResponseList().getString().isEmpty()) {
                Log.e("",
                    response.getValidationDirectResponseList().getString().get(0)
                )
            }
        } else {
            Log.e("",
                "Failed to create customer profile:  " + response.getMessages().getResultCode()
            )
        }
    } else {
        // Display the error code and message when response is null
        val errorResponse: ANetApiResponse = controller.getErrorResponse()
        Log.e("","Failed to get response")
        if (!errorResponse.getMessages().getMessage().isEmpty()) {
            Log.e("",
                "Error: " + errorResponse.getMessages().getMessage().get(0).getCode()
                    .toString() + " \n" + errorResponse.getMessages().getMessage().get(0)
                    .getText()
            )
        }
    }`

after run this code my app is getting crash with controller.getApiResponse() must not be null. So i debugged the code and found one exception that is java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Image; in HttpUtility.java class with postData method.

Does anyone have an idea about this?

patelbansari avatar Oct 19 '20 10:10 patelbansari

Just wanted to comment here and say I've run into this too. TLDR: I don't have a solution that works with mobile android. I'm going to implement an endpoint on a server (sdk-php) and shoot over requests to do auth.net

Longer explanation. I ran into the same issue. My understanding from what i've researched is that error is related to swing? Java FX? Basically, the desktop version.

I thought of compiling the jar file from sdk-java and including that in android.

  • With a caveat of building all the dependencies INTO the jar, so I wouldn't have to add any "implementation" lines to the gradle file.

Welp, I finally got it compiled. But it still doesn't work. Same error about missing libraries.

I'll post my pom.xml file, in case you want to try it out.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.sonatype.oss</groupId>
		<artifactId>oss-parent</artifactId>
		<version>7</version>
	</parent>
	<groupId>net.authorize</groupId>
	<artifactId>anet-java-sdk</artifactId>
	<packaging>jar</packaging>
	<version>2.0.3-SNAPSHOT</version>
	<name>Authorize.Net Java SDK</name>
	<description>Authorize.Net SDK includes standard payments, recurring billing, and customer profiles.</description>
	<url>http://developer.authorize.net</url>
	<licenses>
		<license>
			<name>SDK License Agreement</name>
			<url>https://github.com/AuthorizeNet/sdk-java/blob/master/LICENSE.txt</url>
			<distribution>repo</distribution>
		</license>
	</licenses>
	<scm>
		<connection>scm:git:https://github.com/AuthorizeNet/sdk-java.git</connection>
		<developerConnection>scm:git:https://github.com/AuthorizeNet/sdk-java.git</developerConnection>
		<url>https://github.com/AuthorizeNet/sdk-java.git</url>
	</scm>
	<developers>
		<developer>
			<id>authorizenet</id>
			<name>Authorize.Net Developer</name>
			<email>[email protected]</email>
		</developer>
	</developers>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.3</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.6</version>
			<scope>compile</scope>
		</dependency>
<!-- 		Log4j Dependencies -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>2.13.3</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-jcl</artifactId>
			<version>2.13.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.13.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.13.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-1.2-api</artifactId>
			<version>2.13.3</version>
		</dependency>
<!-- 		Log4j Dependencies -->
		<dependency>
			<groupId>org.jmock</groupId>
			<artifactId>jmock</artifactId>
			<version>2.6.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest-core</artifactId>
			<version>1.3</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest-library</artifactId>
			<version>1.3</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compile.source>1.5</maven.compile.source>
		<maven.compile.target>1.5</maven.compile.target>
		<maven.compile.optimize>true</maven.compile.optimize>
		<maven.compile.deprecation>true</maven.compile.deprecation>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>properties-maven-plugin</artifactId>
				<version>1.0-alpha-2</version>
				<!-- <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> 
					</goals> <configuration> <files> <file>${user.home}/anet-java-sdk.properties</file> 
					</files> </configuration> </execution> </executions> -->
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<includes>
						<include> **/mocktest/**.java</include>
					</includes>
					<skipTests>false</skipTests>
					<forkCount>1</forkCount>
					<systemPropertyVariables>
						<API_LOGIN_ID>${api.login.id}</API_LOGIN_ID>
						<TRANSACTION_KEY>${transaction.key}</TRANSACTION_KEY>
					</systemPropertyVariables>
					<environmentVariables>
						<API_LOGIN_ID>API_LOGIN_ID</API_LOGIN_ID>
						<TRANSACTION_KEY>TRANSACTION_KEY</TRANSACTION_KEY>
					</environmentVariables>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>2.10.4</version>
				<configuration>
					<additionalparam>-Xdoclint:none</additionalparam>
				</configuration>
				<executions>
					<execution>
						<id>attach-javadocs</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>net.authorize.api.controller.base.ApiOperationBase</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
			</plugin>

		</plugins>
		<resources>
			<resource>
				<directory>resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/AuthorizedNetSensitiveTagsConfig.json</include>
				</includes>
			</resource>
			<resource>
				<directory>resources</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*.*</include>
				</includes>
				<excludes>
					<exclude>log4j2.xml</exclude>
				</excludes>
			</resource>
		</resources>
	</build>
</project>

Note you have to run mvn assembly:single

aleksandrmelnikov avatar Nov 12 '20 06:11 aleksandrmelnikov

Hi @patelbansari , I'm facing same problem. Have you found any solution?

MOhsain avatar Apr 05 '21 16:04 MOhsain