A Java library for integrating Payload.
<dependency>
<groupId>co.payload</groupId>
<artifactId>payload</artifactId>
<version>0.2.0</version>
</dependency>
Download the latest version from GitHub.
Include the Payload folder in your Android Studio or Eclipse project.
Once you've included the Payload Java library in your project,
include the co.payload.pl
namespace to get started.
All Payload objects and methods are accessible using the pl
static class.
To authenticate with the Payload API, you'll need a live or test API key. API keys are accessible from within the Payload dashboard.
import co.payload.Session;
Session sess = Session("secret_key_3bW9JMZtPVDOfFNzwRdfE");
Interfacing with the Payload API is done primarily through Payload Objects. Below is an example of
creating a customer using the pl.Customer
object.
// Create a Customer
pl.Customer customer = sess.create(new pl.Customer(){{
set("email", "matt.perez@example.com");
set("full_name", "Matt Perez");
}});
// Create a Payment
pl.Payment payment = sess.create(new pl.Payment(){{
set("amount", 100.0);
set("payment_method", new pl.Card(){{
set("card_number", "4242 4242 4242 4242");
}});
}});
Object attributes are accessible through getStr
, getInt
, and getFloat
.
System.out.print(customer.getStr("email"));
Updating an object is a simple call to the update
object method.
// Updating a customer's email
customer.update(pl.attr("email", "matt.perez@newwork.com"));
Grab an existing object by its ID using the get
method.
// Get a customer by id
pl.Customer customer = sess.select(pl.Customer.class).get("cust_id");
Objects can be selected using any of their attributes.
// Select a customer by email
List<pl.Customer> customers = sess.select(pl.Customer.class).filter_by(
"email", "matt.perez@example.com"
);
Use the pl.attr
attribute helper
interface to write powerful queries with a little extra syntax sugar.
List<pl.Payment> payments = sess.select(pl.Payment.class).filter_by(
pl.attr("amount").gt(100),
pl.attr("amount").lt(200),
pl.attr("description").contains("Test"),
pl.attr("created_at").gt(LocalDate.of(2019, 02, 20))
).all();
Tests are contained within the src/tests
directory. To run tests use the following command in terminal:
API_KEY=your_test_secret_key mvn test
To get further information on Payload's Java library and API capabilities, visit the unabridged Payload Documentation.