visit
If your business has ever considered adding a payment system for issuing cards—perhaps to customers or employees—then you may have avoided it because the technical hurdles seemed too difficult. In reality, getting a payment system up and running is quite straightforward. In this post, we’ll look at how to build payments into your Java application using the Core API from .
While there’s not an officially supported Java SDK for Marqeta, is quite straightforward, as the Core API is documented in both and . The OpenAPI documentation is in beta, but it is generated directly from the API source code. To get a Java client, all we need to do is drop the OpenAPI yaml file into , modify the servers section to use the //sandbox-api.marqeta.com/v3
as the URL, and tell it to generate a Java client.
Once you’ve downloaded the client, you can build the client in your local Maven repository, get a Maven app started, and include the client in your pom.xml
file.
Newer versions of Java may require adding a dependency to the client’s pom.xml
:
<!-- //mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
$ mvn clean install
$ mvn -B archetype:generate \
-DgroupId=com.mycompany.app \
-DartifactId=my-payment-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4
You’ll now have a folder called my-payment-app
with a pom.xml
into which you can add the generated client as a dependency:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-java-client</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
We can get started in the main App.java
file of the Maven-generated app by setting up our authentication and importing the classes we’ll use throughout the project.
package com.mycompany.app;
import io.swagger.client.*;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
public class App
{
public static void main( String[] args )
{
String username = "your-sandbox-application-token";
String password = "your-sandbox-admin-access-token";
ApiClient apiClient = new ApiClient();
apiClient.setUsername(username);
apiClient.setPassword(password);
}
}
Once we have this ApiClient
object ready to go, we can create a new user record with the UsersAPI
:
...
apiClient.setPassword(password);
UsersApi usersApiInstance = new UsersApi();
usersApiInstance.setApiClient(apiClient);
CardHolderModel cardHolderBody = new CardHolderModel();
cardHolderBody.setFirstName("Marqeta");
cardHolderBody.setLastName("User");
cardHolderBody.setAddress1("180 Grand Avenue");
cardHolderBody.setAddress2("6th Floor");
cardHolderBody.setCity("Oakland");
cardHolderBody.setState("CA");
cardHolderBody.setPostalCode("94612");
String userToken = "";
try {
UserCardHolderResponse cardHolderResult = usersApiInstance.postUsers(cardHolderBody);
System.out.println(cardHolderResult);
userToken = cardHolderResult.getToken();
System.out.println("USER TOKEN: " + userToken);
} catch (ApiException e) {
System.err.println("Exception when calling UsersApi#postUsers");
e.printStackTrace();
}
...
Note that we’ve set the user’s first and last name, and we’re storing the user token that gets returned so we can use it later. We also configured the usersApiInstance
to use the APIClient
that we defined previously. We’ll continue to do that for each new API endpoint we interact with.
...
CardProductsApi cardProductsApiInstance = new CardProductsApi();
cardProductsApiInstance.setApiClient(apiClient);
Integer count = 1;
Integer startIndex = 0;
String sortBy = "-createdTime";
String cardProductToken = "";
try {
CardProductListResponse cardProductListResult = cardProductsApiInstance.getCardproducts(count, startIndex, sortBy);
System.out.println(cardProductListResult);
cardProductToken = cardProductListResult.getData().get(0).getToken();
System.out.println("CARD PRODUCT TOKEN: " + cardProductToken);
} catch (ApiException e) {
System.err.println("Exception when calling CardProductsApi#getCardProducts");
e.printStackTrace();
}
...
At this point, we have everything we need—a user token and a card product token—to issue a new card. We make sure to tell the API not to send us the CVV or the PAN for the card upon generation, for greater security.
...
CardsApi cardsApiInstance = new CardsApi();
cardsApiInstance.setApiClient(apiClient);
CardRequest cardRequestBody = new CardRequest();
cardRequestBody.setUserToken(userToken);
cardRequestBody.setCardProductToken(cardProductToken);
Boolean showCvvNumber = false;
Boolean showPan = false;
try {
CardResponse cardResult = cardsApiInstance.postCards(cardRequestBody, showCvvNumber, showPan);
System.out.println(cardResult);
} catch (ApiException e) {
System.err.println("Exception when calling CardsApi#postCards");
e.printStackTrace();
}
...