Skip to content

payload-code/payload-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Payload Java Library

A Java library for integrating Payload.

Installation

Maven Dependency

<dependency>
  <groupId>co.payload</groupId>
  <artifactId>payload</artifactId>
  <version>0.2.0</version>
</dependency>

Manual Installation

1) Download

Download the latest version from GitHub.

2) Include in Project

Include the Payload folder in your Android Studio or Eclipse project.

Get Started

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.

API Authentication

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");

Creating an Object

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");
    }});
}});

Accessing Object Attributes

Object attributes are accessible through getStr, getInt, and getFloat.

System.out.print(customer.getStr("email"));

Updating an Object

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"));

Get an Object by ID

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");

Selecting Objects

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();

Testing the Payload Java Library

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

Documentation

To get further information on Payload's Java library and API capabilities, visit the unabridged Payload Documentation.